07ab2f285771f45078c15bdb5e778afa844e8d52
[blender-staging.git] / intern / cycles / kernel / svm / svm_image.h
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
17 CCL_NAMESPACE_BEGIN
18
19 /* Float textures on various devices. */
20 #if defined(__KERNEL_CPU__)
21   #define TEX_NUM_FLOAT_IMAGES  TEX_NUM_FLOAT_IMAGES_CPU
22 #elif defined(__KERNEL_CUDA__)
23   #define TEX_NUM_FLOAT_IMAGES  TEX_NUM_FLOAT_IMAGES_CUDA
24 #else
25   #define TEX_NUM_FLOAT_IMAGES  TEX_NUM_FLOAT_IMAGES_OPENCL
26 #endif
27
28 #ifdef __KERNEL_OPENCL__
29
30 /* For OpenCL all images are packed in a single array, and we do manual lookup
31  * and interpolation. */
32
33 ccl_device_inline float4 svm_image_texture_read(KernelGlobals *kg, int offset)
34 {
35         uchar4 r = kernel_tex_fetch(__tex_image_packed, offset);
36         float f = 1.0f/255.0f;
37         return make_float4(r.x*f, r.y*f, r.z*f, r.w*f);
38 }
39
40 ccl_device_inline int svm_image_texture_wrap_periodic(int x, int width)
41 {
42         x %= width;
43         if(x < 0)
44                 x += width;
45         return x;
46 }
47
48 ccl_device_inline int svm_image_texture_wrap_clamp(int x, int width)
49 {
50         return clamp(x, 0, width-1);
51 }
52
53 ccl_device_inline float svm_image_texture_frac(float x, int *ix)
54 {
55         int i = float_to_int(x) - ((x < 0.0f)? 1: 0);
56         *ix = i;
57         return x - (float)i;
58 }
59
60 ccl_device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, uint srgb, uint use_alpha)
61 {
62         uint4 info = kernel_tex_fetch(__tex_image_packed_info, id);
63         uint width = info.x;
64         uint height = info.y;
65         uint offset = info.z;
66         uint periodic = (info.w & 0x1);
67         uint interpolation = info.w >> 1;
68
69         float4 r;
70         int ix, iy, nix, niy;
71         if(interpolation == INTERPOLATION_CLOSEST) {
72                 svm_image_texture_frac(x*width, &ix);
73                 svm_image_texture_frac(y*height, &iy);
74
75                 if(periodic) {
76                         ix = svm_image_texture_wrap_periodic(ix, width);
77                         iy = svm_image_texture_wrap_periodic(iy, height);
78                 }
79                 else {
80                         ix = svm_image_texture_wrap_clamp(ix, width);
81                         iy = svm_image_texture_wrap_clamp(iy, height);
82
83                 }
84                 r = svm_image_texture_read(kg, offset + ix + iy*width);
85         }
86         else { /* We default to linear interpolation if it is not closest */
87                 float tx = svm_image_texture_frac(x*width, &ix);
88                 float ty = svm_image_texture_frac(y*height, &iy);
89
90                 if(periodic) {
91                         ix = svm_image_texture_wrap_periodic(ix, width);
92                         iy = svm_image_texture_wrap_periodic(iy, height);
93
94                         nix = svm_image_texture_wrap_periodic(ix+1, width);
95                         niy = svm_image_texture_wrap_periodic(iy+1, height);
96                 }
97                 else {
98                         ix = svm_image_texture_wrap_clamp(ix, width);
99                         iy = svm_image_texture_wrap_clamp(iy, height);
100
101                         nix = svm_image_texture_wrap_clamp(ix+1, width);
102                         niy = svm_image_texture_wrap_clamp(iy+1, height);
103                 }
104
105
106                 r = (1.0f - ty)*(1.0f - tx)*svm_image_texture_read(kg, offset + ix + iy*width);
107                 r += (1.0f - ty)*tx*svm_image_texture_read(kg, offset + nix + iy*width);
108                 r += ty*(1.0f - tx)*svm_image_texture_read(kg, offset + ix + niy*width);
109                 r += ty*tx*svm_image_texture_read(kg, offset + nix + niy*width);
110         }
111
112         if(use_alpha && r.w != 1.0f && r.w != 0.0f) {
113                 float invw = 1.0f/r.w;
114                 r.x *= invw;
115                 r.y *= invw;
116                 r.z *= invw;
117
118                 if(id >= TEX_NUM_FLOAT_IMAGES) {
119                         r.x = min(r.x, 1.0f);
120                         r.y = min(r.y, 1.0f);
121                         r.z = min(r.z, 1.0f);
122                 }
123         }
124
125         if(srgb) {
126                 r.x = color_srgb_to_scene_linear(r.x);
127                 r.y = color_srgb_to_scene_linear(r.y);
128                 r.z = color_srgb_to_scene_linear(r.z);
129         }
130
131         return r;
132 }
133
134 #else
135
136 ccl_device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, uint srgb, uint use_alpha)
137 {
138 #ifdef __KERNEL_CPU__
139 #  ifdef __KERNEL_SSE2__
140         ssef r_ssef;
141         float4 &r = (float4 &)r_ssef;
142         r = kernel_tex_image_interp(id, x, y);
143 #  else
144         float4 r = kernel_tex_image_interp(id, x, y);
145 #  endif
146 #else
147         float4 r;
148
149         /* not particularly proud of this massive switch, what are the
150          * alternatives?
151          * - use a single big 1D texture, and do our own lookup/filtering
152          * - group by size and use a 3d texture, performance impact
153          * - group into larger texture with some padding for correct lerp
154          *
155          * also note that cuda has a textures limit (128 for Fermi, 256 for Kepler),
156          * and we cannot use all since we still need some for other storage */
157
158         switch(id) {
159                 case 0: r = kernel_tex_image_interp(__tex_image_float_000, x, y); break;
160                 case 1: r = kernel_tex_image_interp(__tex_image_float_001, x, y); break;
161                 case 2: r = kernel_tex_image_interp(__tex_image_float_002, x, y); break;
162                 case 3: r = kernel_tex_image_interp(__tex_image_float_003, x, y); break;
163                 case 4: r = kernel_tex_image_interp(__tex_image_float_004, x, y); break;
164                 case 5: r = kernel_tex_image_interp(__tex_image_005, x, y); break;
165                 case 6: r = kernel_tex_image_interp(__tex_image_006, x, y); break;
166                 case 7: r = kernel_tex_image_interp(__tex_image_007, x, y); break;
167                 case 8: r = kernel_tex_image_interp(__tex_image_008, x, y); break;
168                 case 9: r = kernel_tex_image_interp(__tex_image_009, x, y); break;
169                 case 10: r = kernel_tex_image_interp(__tex_image_010, x, y); break;
170                 case 11: r = kernel_tex_image_interp(__tex_image_011, x, y); break;
171                 case 12: r = kernel_tex_image_interp(__tex_image_012, x, y); break;
172                 case 13: r = kernel_tex_image_interp(__tex_image_013, x, y); break;
173                 case 14: r = kernel_tex_image_interp(__tex_image_014, x, y); break;
174                 case 15: r = kernel_tex_image_interp(__tex_image_015, x, y); break;
175                 case 16: r = kernel_tex_image_interp(__tex_image_016, x, y); break;
176                 case 17: r = kernel_tex_image_interp(__tex_image_017, x, y); break;
177                 case 18: r = kernel_tex_image_interp(__tex_image_018, x, y); break;
178                 case 19: r = kernel_tex_image_interp(__tex_image_019, x, y); break;
179                 case 20: r = kernel_tex_image_interp(__tex_image_020, x, y); break;
180                 case 21: r = kernel_tex_image_interp(__tex_image_021, x, y); break;
181                 case 22: r = kernel_tex_image_interp(__tex_image_022, x, y); break;
182                 case 23: r = kernel_tex_image_interp(__tex_image_023, x, y); break;
183                 case 24: r = kernel_tex_image_interp(__tex_image_024, x, y); break;
184                 case 25: r = kernel_tex_image_interp(__tex_image_025, x, y); break;
185                 case 26: r = kernel_tex_image_interp(__tex_image_026, x, y); break;
186                 case 27: r = kernel_tex_image_interp(__tex_image_027, x, y); break;
187                 case 28: r = kernel_tex_image_interp(__tex_image_028, x, y); break;
188                 case 29: r = kernel_tex_image_interp(__tex_image_029, x, y); break;
189                 case 30: r = kernel_tex_image_interp(__tex_image_030, x, y); break;
190                 case 31: r = kernel_tex_image_interp(__tex_image_031, x, y); break;
191                 case 32: r = kernel_tex_image_interp(__tex_image_032, x, y); break;
192                 case 33: r = kernel_tex_image_interp(__tex_image_033, x, y); break;
193                 case 34: r = kernel_tex_image_interp(__tex_image_034, x, y); break;
194                 case 35: r = kernel_tex_image_interp(__tex_image_035, x, y); break;
195                 case 36: r = kernel_tex_image_interp(__tex_image_036, x, y); break;
196                 case 37: r = kernel_tex_image_interp(__tex_image_037, x, y); break;
197                 case 38: r = kernel_tex_image_interp(__tex_image_038, x, y); break;
198                 case 39: r = kernel_tex_image_interp(__tex_image_039, x, y); break;
199                 case 40: r = kernel_tex_image_interp(__tex_image_040, x, y); break;
200                 case 41: r = kernel_tex_image_interp(__tex_image_041, x, y); break;
201                 case 42: r = kernel_tex_image_interp(__tex_image_042, x, y); break;
202                 case 43: r = kernel_tex_image_interp(__tex_image_043, x, y); break;
203                 case 44: r = kernel_tex_image_interp(__tex_image_044, x, y); break;
204                 case 45: r = kernel_tex_image_interp(__tex_image_045, x, y); break;
205                 case 46: r = kernel_tex_image_interp(__tex_image_046, x, y); break;
206                 case 47: r = kernel_tex_image_interp(__tex_image_047, x, y); break;
207                 case 48: r = kernel_tex_image_interp(__tex_image_048, x, y); break;
208                 case 49: r = kernel_tex_image_interp(__tex_image_049, x, y); break;
209                 case 50: r = kernel_tex_image_interp(__tex_image_050, x, y); break;
210                 case 51: r = kernel_tex_image_interp(__tex_image_051, x, y); break;
211                 case 52: r = kernel_tex_image_interp(__tex_image_052, x, y); break;
212                 case 53: r = kernel_tex_image_interp(__tex_image_053, x, y); break;
213                 case 54: r = kernel_tex_image_interp(__tex_image_054, x, y); break;
214                 case 55: r = kernel_tex_image_interp(__tex_image_055, x, y); break;
215                 case 56: r = kernel_tex_image_interp(__tex_image_056, x, y); break;
216                 case 57: r = kernel_tex_image_interp(__tex_image_057, x, y); break;
217                 case 58: r = kernel_tex_image_interp(__tex_image_058, x, y); break;
218                 case 59: r = kernel_tex_image_interp(__tex_image_059, x, y); break;
219                 case 60: r = kernel_tex_image_interp(__tex_image_060, x, y); break;
220                 case 61: r = kernel_tex_image_interp(__tex_image_061, x, y); break;
221                 case 62: r = kernel_tex_image_interp(__tex_image_062, x, y); break;
222                 case 63: r = kernel_tex_image_interp(__tex_image_063, x, y); break;
223                 case 64: r = kernel_tex_image_interp(__tex_image_064, x, y); break;
224                 case 65: r = kernel_tex_image_interp(__tex_image_065, x, y); break;
225                 case 66: r = kernel_tex_image_interp(__tex_image_066, x, y); break;
226                 case 67: r = kernel_tex_image_interp(__tex_image_067, x, y); break;
227                 case 68: r = kernel_tex_image_interp(__tex_image_068, x, y); break;
228                 case 69: r = kernel_tex_image_interp(__tex_image_069, x, y); break;
229                 case 70: r = kernel_tex_image_interp(__tex_image_070, x, y); break;
230                 case 71: r = kernel_tex_image_interp(__tex_image_071, x, y); break;
231                 case 72: r = kernel_tex_image_interp(__tex_image_072, x, y); break;
232                 case 73: r = kernel_tex_image_interp(__tex_image_073, x, y); break;
233                 case 74: r = kernel_tex_image_interp(__tex_image_074, x, y); break;
234                 case 75: r = kernel_tex_image_interp(__tex_image_075, x, y); break;
235                 case 76: r = kernel_tex_image_interp(__tex_image_076, x, y); break;
236                 case 77: r = kernel_tex_image_interp(__tex_image_077, x, y); break;
237                 case 78: r = kernel_tex_image_interp(__tex_image_078, x, y); break;
238                 case 79: r = kernel_tex_image_interp(__tex_image_079, x, y); break;
239                 case 80: r = kernel_tex_image_interp(__tex_image_080, x, y); break;
240                 case 81: r = kernel_tex_image_interp(__tex_image_081, x, y); break;
241                 case 82: r = kernel_tex_image_interp(__tex_image_082, x, y); break;
242                 case 83: r = kernel_tex_image_interp(__tex_image_083, x, y); break;
243                 case 84: r = kernel_tex_image_interp(__tex_image_084, x, y); break;
244                 case 85: r = kernel_tex_image_interp(__tex_image_085, x, y); break;
245                 case 86: r = kernel_tex_image_interp(__tex_image_086, x, y); break;
246                 case 87: r = kernel_tex_image_interp(__tex_image_087, x, y); break;
247                 case 88: r = kernel_tex_image_interp(__tex_image_088, x, y); break;
248                 case 89: r = kernel_tex_image_interp(__tex_image_089, x, y); break;
249                 case 90: r = kernel_tex_image_interp(__tex_image_090, x, y); break;
250                 case 91: r = kernel_tex_image_interp(__tex_image_091, x, y); break;
251                 case 92: r = kernel_tex_image_interp(__tex_image_092, x, y); break;
252
253 #  if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 300)
254                 case 93: r = kernel_tex_image_interp(__tex_image_093, x, y); break;
255                 case 94: r = kernel_tex_image_interp(__tex_image_094, x, y); break;
256                 case 95: r = kernel_tex_image_interp(__tex_image_095, x, y); break;
257                 case 96: r = kernel_tex_image_interp(__tex_image_096, x, y); break;
258                 case 97: r = kernel_tex_image_interp(__tex_image_097, x, y); break;
259                 case 98: r = kernel_tex_image_interp(__tex_image_098, x, y); break;
260                 case 99: r = kernel_tex_image_interp(__tex_image_099, x, y); break;
261                 case 100: r = kernel_tex_image_interp(__tex_image_100, x, y); break;
262                 case 101: r = kernel_tex_image_interp(__tex_image_101, x, y); break;
263                 case 102: r = kernel_tex_image_interp(__tex_image_102, x, y); break;
264                 case 103: r = kernel_tex_image_interp(__tex_image_103, x, y); break;
265                 case 104: r = kernel_tex_image_interp(__tex_image_104, x, y); break;
266                 case 105: r = kernel_tex_image_interp(__tex_image_105, x, y); break;
267                 case 106: r = kernel_tex_image_interp(__tex_image_106, x, y); break;
268                 case 107: r = kernel_tex_image_interp(__tex_image_107, x, y); break;
269                 case 108: r = kernel_tex_image_interp(__tex_image_108, x, y); break;
270                 case 109: r = kernel_tex_image_interp(__tex_image_109, x, y); break;
271                 case 110: r = kernel_tex_image_interp(__tex_image_110, x, y); break;
272                 case 111: r = kernel_tex_image_interp(__tex_image_111, x, y); break;
273                 case 112: r = kernel_tex_image_interp(__tex_image_112, x, y); break;
274                 case 113: r = kernel_tex_image_interp(__tex_image_113, x, y); break;
275                 case 114: r = kernel_tex_image_interp(__tex_image_114, x, y); break;
276                 case 115: r = kernel_tex_image_interp(__tex_image_115, x, y); break;
277                 case 116: r = kernel_tex_image_interp(__tex_image_116, x, y); break;
278                 case 117: r = kernel_tex_image_interp(__tex_image_117, x, y); break;
279                 case 118: r = kernel_tex_image_interp(__tex_image_118, x, y); break;
280                 case 119: r = kernel_tex_image_interp(__tex_image_119, x, y); break;
281                 case 120: r = kernel_tex_image_interp(__tex_image_120, x, y); break;
282                 case 121: r = kernel_tex_image_interp(__tex_image_121, x, y); break;
283                 case 122: r = kernel_tex_image_interp(__tex_image_122, x, y); break;
284                 case 123: r = kernel_tex_image_interp(__tex_image_123, x, y); break;
285                 case 124: r = kernel_tex_image_interp(__tex_image_124, x, y); break;
286                 case 125: r = kernel_tex_image_interp(__tex_image_125, x, y); break;
287                 case 126: r = kernel_tex_image_interp(__tex_image_126, x, y); break;
288                 case 127: r = kernel_tex_image_interp(__tex_image_127, x, y); break;
289                 case 128: r = kernel_tex_image_interp(__tex_image_128, x, y); break;
290                 case 129: r = kernel_tex_image_interp(__tex_image_129, x, y); break;
291                 case 130: r = kernel_tex_image_interp(__tex_image_130, x, y); break;
292                 case 131: r = kernel_tex_image_interp(__tex_image_131, x, y); break;
293                 case 132: r = kernel_tex_image_interp(__tex_image_132, x, y); break;
294                 case 133: r = kernel_tex_image_interp(__tex_image_133, x, y); break;
295                 case 134: r = kernel_tex_image_interp(__tex_image_134, x, y); break;
296                 case 135: r = kernel_tex_image_interp(__tex_image_135, x, y); break;
297                 case 136: r = kernel_tex_image_interp(__tex_image_136, x, y); break;
298                 case 137: r = kernel_tex_image_interp(__tex_image_137, x, y); break;
299                 case 138: r = kernel_tex_image_interp(__tex_image_138, x, y); break;
300                 case 139: r = kernel_tex_image_interp(__tex_image_139, x, y); break;
301                 case 140: r = kernel_tex_image_interp(__tex_image_140, x, y); break;
302                 case 141: r = kernel_tex_image_interp(__tex_image_141, x, y); break;
303                 case 142: r = kernel_tex_image_interp(__tex_image_142, x, y); break;
304                 case 143: r = kernel_tex_image_interp(__tex_image_143, x, y); break;
305                 case 144: r = kernel_tex_image_interp(__tex_image_144, x, y); break;
306                 case 145: r = kernel_tex_image_interp(__tex_image_145, x, y); break;
307                 case 146: r = kernel_tex_image_interp(__tex_image_146, x, y); break;
308                 case 147: r = kernel_tex_image_interp(__tex_image_147, x, y); break;
309                 case 148: r = kernel_tex_image_interp(__tex_image_148, x, y); break;
310                 case 149: r = kernel_tex_image_interp(__tex_image_149, x, y); break;
311                 case 150: r = kernel_tex_image_interp(__tex_image_150, x, y); break;
312 #  endif
313
314                 default:
315                         kernel_assert(0);
316                         return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
317         }
318 #endif
319
320 #ifdef __KERNEL_SSE2__
321         float alpha = r.w;
322
323         if(use_alpha && alpha != 1.0f && alpha != 0.0f) {
324                 r_ssef = r_ssef / ssef(alpha);
325                 if(id >= TEX_NUM_FLOAT_IMAGES)
326                         r_ssef = min(r_ssef, ssef(1.0f));
327                 r.w = alpha;
328         }
329
330         if(srgb) {
331                 r_ssef = color_srgb_to_scene_linear(r_ssef);
332                 r.w = alpha;
333         }
334 #else
335         if(use_alpha && r.w != 1.0f && r.w != 0.0f) {
336                 float invw = 1.0f/r.w;
337                 r.x *= invw;
338                 r.y *= invw;
339                 r.z *= invw;
340
341                 if(id >= TEX_NUM_FLOAT_IMAGES) {
342                         r.x = min(r.x, 1.0f);
343                         r.y = min(r.y, 1.0f);
344                         r.z = min(r.z, 1.0f);
345                 }
346         }
347
348         if(srgb) {
349                 r.x = color_srgb_to_scene_linear(r.x);
350                 r.y = color_srgb_to_scene_linear(r.y);
351                 r.z = color_srgb_to_scene_linear(r.z);
352         }
353 #endif
354
355         return r;
356 }
357
358 #endif
359
360 /* Remap coordnate from 0..1 box to -1..-1 */
361 ccl_device_inline float3 texco_remap_square(float3 co)
362 {
363         return (co - make_float3(0.5f, 0.5f, 0.5f)) * 2.0f;
364 }
365
366 ccl_device void svm_node_tex_image(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
367 {
368         uint id = node.y;
369         uint co_offset, out_offset, alpha_offset, srgb;
370
371         decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
372
373         float3 co = stack_load_float3(stack, co_offset);
374         float2 tex_co;
375         uint use_alpha = stack_valid(alpha_offset);
376         if(node.w == NODE_IMAGE_PROJ_SPHERE) {
377                 co = texco_remap_square(co);
378                 tex_co = map_to_sphere(co);
379         }
380         else if(node.w == NODE_IMAGE_PROJ_TUBE) {
381                 co = texco_remap_square(co);
382                 tex_co = map_to_tube(co);
383         }
384         else {
385                 tex_co = make_float2(co.x, co.y);
386         }
387         float4 f = svm_image_texture(kg, id, tex_co.x, tex_co.y, srgb, use_alpha);
388
389         if(stack_valid(out_offset))
390                 stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
391         if(stack_valid(alpha_offset))
392                 stack_store_float(stack, alpha_offset, f.w);
393 }
394
395 ccl_device void svm_node_tex_image_box(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
396 {
397         /* get object space normal */
398         float3 N = ccl_fetch(sd, N);
399
400         N = ccl_fetch(sd, N);
401         if(ccl_fetch(sd, object) != OBJECT_NONE)
402                 object_inverse_normal_transform(kg, sd, &N);
403
404         /* project from direction vector to barycentric coordinates in triangles */
405         N.x = fabsf(N.x);
406         N.y = fabsf(N.y);
407         N.z = fabsf(N.z);
408
409         N /= (N.x + N.y + N.z);
410
411         /* basic idea is to think of this as a triangle, each corner representing
412          * one of the 3 faces of the cube. in the corners we have single textures,
413          * in between we blend between two textures, and in the middle we a blend
414          * between three textures.
415          *
416          * the Nxyz values are the barycentric coordinates in an equilateral
417          * triangle, which in case of blending, in the middle has a smaller
418          * equilateral triangle where 3 textures blend. this divides things into
419          * 7 zones, with an if() test for each zone */
420
421         float3 weight = make_float3(0.0f, 0.0f, 0.0f);
422         float blend = __int_as_float(node.w);
423         float limit = 0.5f*(1.0f + blend);
424
425         /* first test for corners with single texture */
426         if(N.x > limit*(N.x + N.y) && N.x > limit*(N.x + N.z)) {
427                 weight.x = 1.0f;
428         }
429         else if(N.y > limit*(N.x + N.y) && N.y > limit*(N.y + N.z)) {
430                 weight.y = 1.0f;
431         }
432         else if(N.z > limit*(N.x + N.z) && N.z > limit*(N.y + N.z)) {
433                 weight.z = 1.0f;
434         }
435         else if(blend > 0.0f) {
436                 /* in case of blending, test for mixes between two textures */
437                 if(N.z < (1.0f - limit)*(N.y + N.x)) {
438                         weight.x = N.x/(N.x + N.y);
439                         weight.x = saturate((weight.x - 0.5f*(1.0f - blend))/blend);
440                         weight.y = 1.0f - weight.x;
441                 }
442                 else if(N.x < (1.0f - limit)*(N.y + N.z)) {
443                         weight.y = N.y/(N.y + N.z);
444                         weight.y = saturate((weight.y - 0.5f*(1.0f - blend))/blend);
445                         weight.z = 1.0f - weight.y;
446                 }
447                 else if(N.y < (1.0f - limit)*(N.x + N.z)) {
448                         weight.x = N.x/(N.x + N.z);
449                         weight.x = saturate((weight.x - 0.5f*(1.0f - blend))/blend);
450                         weight.z = 1.0f - weight.x;
451                 }
452                 else {
453                         /* last case, we have a mix between three */
454                         weight.x = ((2.0f - limit)*N.x + (limit - 1.0f))/(2.0f*limit - 1.0f);
455                         weight.y = ((2.0f - limit)*N.y + (limit - 1.0f))/(2.0f*limit - 1.0f);
456                         weight.z = ((2.0f - limit)*N.z + (limit - 1.0f))/(2.0f*limit - 1.0f);
457                 }
458         }
459         else {
460                 /* Desperate mode, no valid choice anyway, fallback to one side.*/
461                 weight.x = 1.0f;
462         }
463
464         /* now fetch textures */
465         uint co_offset, out_offset, alpha_offset, srgb;
466         decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
467
468         float3 co = stack_load_float3(stack, co_offset);
469         uint id = node.y;
470
471         float4 f = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
472         uint use_alpha = stack_valid(alpha_offset);
473
474         if(weight.x > 0.0f)
475                 f += weight.x*svm_image_texture(kg, id, co.y, co.z, srgb, use_alpha);
476         if(weight.y > 0.0f)
477                 f += weight.y*svm_image_texture(kg, id, co.x, co.z, srgb, use_alpha);
478         if(weight.z > 0.0f)
479                 f += weight.z*svm_image_texture(kg, id, co.y, co.x, srgb, use_alpha);
480
481         if(stack_valid(out_offset))
482                 stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
483         if(stack_valid(alpha_offset))
484                 stack_store_float(stack, alpha_offset, f.w);
485 }
486
487 ccl_device void svm_node_tex_environment(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
488 {
489         uint id = node.y;
490         uint co_offset, out_offset, alpha_offset, srgb;
491         uint projection = node.w;
492
493         decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
494
495         float3 co = stack_load_float3(stack, co_offset);
496         float2 uv;
497
498         co = normalize(co);
499         
500         if(projection == 0)
501                 uv = direction_to_equirectangular(co);
502         else
503                 uv = direction_to_mirrorball(co);
504
505         uint use_alpha = stack_valid(alpha_offset);
506         float4 f = svm_image_texture(kg, id, uv.x, uv.y, srgb, use_alpha);
507
508         if(stack_valid(out_offset))
509                 stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
510         if(stack_valid(alpha_offset))
511                 stack_store_float(stack, alpha_offset, f.w);
512 }
513
514 CCL_NAMESPACE_END
515