Better fix for T38501: blender crashes right after adding image texture to
[blender.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 #ifdef __KERNEL_OPENCL__
20
21 /* For OpenCL all images are packed in a single array, and we do manual lookup
22  * and interpolation. */
23
24 ccl_device_inline float4 svm_image_texture_read(KernelGlobals *kg, int offset)
25 {
26         uchar4 r = kernel_tex_fetch(__tex_image_packed, offset);
27         float f = 1.0f/255.0f;
28         return make_float4(r.x*f, r.y*f, r.z*f, r.w*f);
29 }
30
31 ccl_device_inline int svm_image_texture_wrap_periodic(int x, int width)
32 {
33         x %= width;
34         if(x < 0)
35                 x += width;
36         return x;
37 }
38
39 ccl_device_inline int svm_image_texture_wrap_clamp(int x, int width)
40 {
41         return clamp(x, 0, width-1);
42 }
43
44 ccl_device_inline float svm_image_texture_frac(float x, int *ix)
45 {
46         int i = float_to_int(x) - ((x < 0.0f)? 1: 0);
47         *ix = i;
48         return x - (float)i;
49 }
50
51 ccl_device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, uint srgb, uint use_alpha)
52 {
53         /* first slots are used by float textures, which are not supported here */
54         if(id < TEX_NUM_FLOAT_IMAGES)
55                 return make_float4(1.0f, 0.0f, 1.0f, 1.0f);
56
57         id -= TEX_NUM_FLOAT_IMAGES;
58
59         uint4 info = kernel_tex_fetch(__tex_image_packed_info, id);
60         uint width = info.x;
61         uint height = info.y;
62         uint offset = info.z;
63         uint periodic = info.w;
64
65         int ix, iy, nix, niy;
66         float tx = svm_image_texture_frac(x*width, &ix);
67         float ty = svm_image_texture_frac(y*height, &iy);
68
69         if(periodic) {
70                 ix = svm_image_texture_wrap_periodic(ix, width);
71                 iy = svm_image_texture_wrap_periodic(iy, height);
72
73                 nix = svm_image_texture_wrap_periodic(ix+1, width);
74                 niy = svm_image_texture_wrap_periodic(iy+1, height);
75         }
76         else {
77                 ix = svm_image_texture_wrap_clamp(ix, width);
78                 iy = svm_image_texture_wrap_clamp(iy, height);
79
80                 nix = svm_image_texture_wrap_clamp(ix+1, width);
81                 niy = svm_image_texture_wrap_clamp(iy+1, height);
82         }
83
84         float4 r = (1.0f - ty)*(1.0f - tx)*svm_image_texture_read(kg, offset + ix + iy*width);
85         r += (1.0f - ty)*tx*svm_image_texture_read(kg, offset + nix + iy*width);
86         r += ty*(1.0f - tx)*svm_image_texture_read(kg, offset + ix + niy*width);
87         r += ty*tx*svm_image_texture_read(kg, offset + nix + niy*width);
88
89         if(use_alpha && r.w != 1.0f && r.w != 0.0f) {
90                 float invw = 1.0f/r.w;
91                 r.x *= invw;
92                 r.y *= invw;
93                 r.z *= invw;
94
95                 if(id >= TEX_NUM_FLOAT_IMAGES) {
96                         r.x = min(r.x, 1.0f);
97                         r.y = min(r.y, 1.0f);
98                         r.z = min(r.z, 1.0f);
99                 }
100         }
101
102         if(srgb) {
103                 r.x = color_srgb_to_scene_linear(r.x);
104                 r.y = color_srgb_to_scene_linear(r.y);
105                 r.z = color_srgb_to_scene_linear(r.z);
106         }
107
108         return r;
109 }
110
111 #else
112
113 ccl_device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, uint srgb, uint use_alpha)
114 {
115 #ifdef __KERNEL_CPU__
116 #ifdef __KERNEL_SSE2__
117         __m128 r_m128;
118         float4 &r = (float4 &)r_m128;
119         r = kernel_tex_image_interp(id, x, y);
120 #else
121         float4 r = kernel_tex_image_interp(id, x, y);
122 #endif
123 #else
124         float4 r;
125
126         /* not particularly proud of this massive switch, what are the
127          * alternatives?
128          * - use a single big 1D texture, and do our own lookup/filtering
129          * - group by size and use a 3d texture, performance impact
130          * - group into larger texture with some padding for correct lerp
131          *
132          * also note that cuda has 128 textures limit, we use 100 now, since
133          * we still need some for other storage */
134
135         switch(id) {
136                 case 0: r = kernel_tex_image_interp(__tex_image_float_000, x, y); break;
137                 case 1: r = kernel_tex_image_interp(__tex_image_float_001, x, y); break;
138                 case 2: r = kernel_tex_image_interp(__tex_image_float_002, x, y); break;
139                 case 3: r = kernel_tex_image_interp(__tex_image_float_003, x, y); break;
140                 case 4: r = kernel_tex_image_interp(__tex_image_float_004, x, y); break;
141                 case 5: r = kernel_tex_image_interp(__tex_image_005, x, y); break;
142                 case 6: r = kernel_tex_image_interp(__tex_image_006, x, y); break;
143                 case 7: r = kernel_tex_image_interp(__tex_image_007, x, y); break;
144                 case 8: r = kernel_tex_image_interp(__tex_image_008, x, y); break;
145                 case 9: r = kernel_tex_image_interp(__tex_image_009, x, y); break;
146                 case 10: r = kernel_tex_image_interp(__tex_image_010, x, y); break;
147                 case 11: r = kernel_tex_image_interp(__tex_image_011, x, y); break;
148                 case 12: r = kernel_tex_image_interp(__tex_image_012, x, y); break;
149                 case 13: r = kernel_tex_image_interp(__tex_image_013, x, y); break;
150                 case 14: r = kernel_tex_image_interp(__tex_image_014, x, y); break;
151                 case 15: r = kernel_tex_image_interp(__tex_image_015, x, y); break;
152                 case 16: r = kernel_tex_image_interp(__tex_image_016, x, y); break;
153                 case 17: r = kernel_tex_image_interp(__tex_image_017, x, y); break;
154                 case 18: r = kernel_tex_image_interp(__tex_image_018, x, y); break;
155                 case 19: r = kernel_tex_image_interp(__tex_image_019, x, y); break;
156                 case 20: r = kernel_tex_image_interp(__tex_image_020, x, y); break;
157                 case 21: r = kernel_tex_image_interp(__tex_image_021, x, y); break;
158                 case 22: r = kernel_tex_image_interp(__tex_image_022, x, y); break;
159                 case 23: r = kernel_tex_image_interp(__tex_image_023, x, y); break;
160                 case 24: r = kernel_tex_image_interp(__tex_image_024, x, y); break;
161                 case 25: r = kernel_tex_image_interp(__tex_image_025, x, y); break;
162                 case 26: r = kernel_tex_image_interp(__tex_image_026, x, y); break;
163                 case 27: r = kernel_tex_image_interp(__tex_image_027, x, y); break;
164                 case 28: r = kernel_tex_image_interp(__tex_image_028, x, y); break;
165                 case 29: r = kernel_tex_image_interp(__tex_image_029, x, y); break;
166                 case 30: r = kernel_tex_image_interp(__tex_image_030, x, y); break;
167                 case 31: r = kernel_tex_image_interp(__tex_image_031, x, y); break;
168                 case 32: r = kernel_tex_image_interp(__tex_image_032, x, y); break;
169                 case 33: r = kernel_tex_image_interp(__tex_image_033, x, y); break;
170                 case 34: r = kernel_tex_image_interp(__tex_image_034, x, y); break;
171                 case 35: r = kernel_tex_image_interp(__tex_image_035, x, y); break;
172                 case 36: r = kernel_tex_image_interp(__tex_image_036, x, y); break;
173                 case 37: r = kernel_tex_image_interp(__tex_image_037, x, y); break;
174                 case 38: r = kernel_tex_image_interp(__tex_image_038, x, y); break;
175                 case 39: r = kernel_tex_image_interp(__tex_image_039, x, y); break;
176                 case 40: r = kernel_tex_image_interp(__tex_image_040, x, y); break;
177                 case 41: r = kernel_tex_image_interp(__tex_image_041, x, y); break;
178                 case 42: r = kernel_tex_image_interp(__tex_image_042, x, y); break;
179                 case 43: r = kernel_tex_image_interp(__tex_image_043, x, y); break;
180                 case 44: r = kernel_tex_image_interp(__tex_image_044, x, y); break;
181                 case 45: r = kernel_tex_image_interp(__tex_image_045, x, y); break;
182                 case 46: r = kernel_tex_image_interp(__tex_image_046, x, y); break;
183                 case 47: r = kernel_tex_image_interp(__tex_image_047, x, y); break;
184                 case 48: r = kernel_tex_image_interp(__tex_image_048, x, y); break;
185                 case 49: r = kernel_tex_image_interp(__tex_image_049, x, y); break;
186                 case 50: r = kernel_tex_image_interp(__tex_image_050, x, y); break;
187                 case 51: r = kernel_tex_image_interp(__tex_image_051, x, y); break;
188                 case 52: r = kernel_tex_image_interp(__tex_image_052, x, y); break;
189                 case 53: r = kernel_tex_image_interp(__tex_image_053, x, y); break;
190                 case 54: r = kernel_tex_image_interp(__tex_image_054, x, y); break;
191                 case 55: r = kernel_tex_image_interp(__tex_image_055, x, y); break;
192                 case 56: r = kernel_tex_image_interp(__tex_image_056, x, y); break;
193                 case 57: r = kernel_tex_image_interp(__tex_image_057, x, y); break;
194                 case 58: r = kernel_tex_image_interp(__tex_image_058, x, y); break;
195                 case 59: r = kernel_tex_image_interp(__tex_image_059, x, y); break;
196                 case 60: r = kernel_tex_image_interp(__tex_image_060, x, y); break;
197                 case 61: r = kernel_tex_image_interp(__tex_image_061, x, y); break;
198                 case 62: r = kernel_tex_image_interp(__tex_image_062, x, y); break;
199                 case 63: r = kernel_tex_image_interp(__tex_image_063, x, y); break;
200                 case 64: r = kernel_tex_image_interp(__tex_image_064, x, y); break;
201                 case 65: r = kernel_tex_image_interp(__tex_image_065, x, y); break;
202                 case 66: r = kernel_tex_image_interp(__tex_image_066, x, y); break;
203                 case 67: r = kernel_tex_image_interp(__tex_image_067, x, y); break;
204                 case 68: r = kernel_tex_image_interp(__tex_image_068, x, y); break;
205                 case 69: r = kernel_tex_image_interp(__tex_image_069, x, y); break;
206                 case 70: r = kernel_tex_image_interp(__tex_image_070, x, y); break;
207                 case 71: r = kernel_tex_image_interp(__tex_image_071, x, y); break;
208                 case 72: r = kernel_tex_image_interp(__tex_image_072, x, y); break;
209                 case 73: r = kernel_tex_image_interp(__tex_image_073, x, y); break;
210                 case 74: r = kernel_tex_image_interp(__tex_image_074, x, y); break;
211                 case 75: r = kernel_tex_image_interp(__tex_image_075, x, y); break;
212                 case 76: r = kernel_tex_image_interp(__tex_image_076, x, y); break;
213                 case 77: r = kernel_tex_image_interp(__tex_image_077, x, y); break;
214                 case 78: r = kernel_tex_image_interp(__tex_image_078, x, y); break;
215                 case 79: r = kernel_tex_image_interp(__tex_image_079, x, y); break;
216                 case 80: r = kernel_tex_image_interp(__tex_image_080, x, y); break;
217                 case 81: r = kernel_tex_image_interp(__tex_image_081, x, y); break;
218                 case 82: r = kernel_tex_image_interp(__tex_image_082, x, y); break;
219                 case 83: r = kernel_tex_image_interp(__tex_image_083, x, y); break;
220                 case 84: r = kernel_tex_image_interp(__tex_image_084, x, y); break;
221                 case 85: r = kernel_tex_image_interp(__tex_image_085, x, y); break;
222                 case 86: r = kernel_tex_image_interp(__tex_image_086, x, y); break;
223                 case 87: r = kernel_tex_image_interp(__tex_image_087, x, y); break;
224                 case 88: r = kernel_tex_image_interp(__tex_image_088, x, y); break;
225                 case 89: r = kernel_tex_image_interp(__tex_image_089, x, y); break;
226                 case 90: r = kernel_tex_image_interp(__tex_image_090, x, y); break;
227                 case 91: r = kernel_tex_image_interp(__tex_image_091, x, y); break;
228                 case 92: r = kernel_tex_image_interp(__tex_image_092, x, y); break;
229                 case 93: r = kernel_tex_image_interp(__tex_image_093, x, y); break;
230                 case 94: r = kernel_tex_image_interp(__tex_image_094, x, y); break;
231                 case 95: r = kernel_tex_image_interp(__tex_image_095, x, y); break;
232                 case 96: r = kernel_tex_image_interp(__tex_image_096, x, y); break;
233                 case 97: r = kernel_tex_image_interp(__tex_image_097, x, y); break;
234                 case 98: r = kernel_tex_image_interp(__tex_image_098, x, y); break;
235                 case 99: r = kernel_tex_image_interp(__tex_image_099, x, y); break;
236                 default: 
237                         kernel_assert(0);
238                         return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
239         }
240 #endif
241
242 #ifdef __KERNEL_SSE2__
243         float alpha = r.w;
244
245         if(use_alpha && alpha != 1.0f && alpha != 0.0f) {
246                 r_m128 = _mm_div_ps(r_m128, _mm_set1_ps(alpha));
247                 if(id >= TEX_NUM_FLOAT_IMAGES)
248                         r_m128 = _mm_min_ps(r_m128, _mm_set1_ps(1.0f));
249                 r.w = alpha;
250         }
251
252         if(srgb) {
253                 r_m128 = color_srgb_to_scene_linear(r_m128);
254                 r.w = alpha;
255         }
256 #else
257         if(use_alpha && r.w != 1.0f && r.w != 0.0f) {
258                 float invw = 1.0f/r.w;
259                 r.x *= invw;
260                 r.y *= invw;
261                 r.z *= invw;
262
263                 if(id >= TEX_NUM_FLOAT_IMAGES) {
264                         r.x = min(r.x, 1.0f);
265                         r.y = min(r.y, 1.0f);
266                         r.z = min(r.z, 1.0f);
267                 }
268         }
269
270         if(srgb) {
271                 r.x = color_srgb_to_scene_linear(r.x);
272                 r.y = color_srgb_to_scene_linear(r.y);
273                 r.z = color_srgb_to_scene_linear(r.z);
274         }
275 #endif
276
277         return r;
278 }
279
280 #endif
281
282 ccl_device void svm_node_tex_image(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
283 {
284         uint id = node.y;
285         uint co_offset, out_offset, alpha_offset, srgb;
286
287         decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
288
289         float3 co = stack_load_float3(stack, co_offset);
290         uint use_alpha = stack_valid(alpha_offset);
291         float4 f = svm_image_texture(kg, id, co.x, co.y, srgb, use_alpha);
292
293         if(stack_valid(out_offset))
294                 stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
295         if(stack_valid(alpha_offset))
296                 stack_store_float(stack, alpha_offset, f.w);
297 }
298
299 ccl_device void svm_node_tex_image_box(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
300 {
301         /* get object space normal */
302         float3 N = sd->N;
303
304         N = sd->N;
305         if(sd->object != ~0)
306                 object_inverse_normal_transform(kg, sd, &N);
307
308         /* project from direction vector to barycentric coordinates in triangles */
309         N.x = fabsf(N.x);
310         N.y = fabsf(N.y);
311         N.z = fabsf(N.z);
312
313         N /= (N.x + N.y + N.z);
314
315         /* basic idea is to think of this as a triangle, each corner representing
316          * one of the 3 faces of the cube. in the corners we have single textures,
317          * in between we blend between two textures, and in the middle we a blend
318          * between three textures.
319          *
320          * the Nxyz values are the barycentric coordinates in an equilateral
321          * triangle, which in case of blending, in the middle has a smaller
322          * equilateral triangle where 3 textures blend. this divides things into
323          * 7 zones, with an if() test for each zone */
324
325         float3 weight = make_float3(0.0f, 0.0f, 0.0f);
326         float blend = __int_as_float(node.w);
327         float limit = 0.5f*(1.0f + blend);
328
329         /* first test for corners with single texture */
330         if(N.x > limit*(N.x + N.y) && N.x > limit*(N.x + N.z)) {
331                 weight.x = 1.0f;
332         }
333         else if(N.y > limit*(N.x + N.y) && N.y > limit*(N.y + N.z)) {
334                 weight.y = 1.0f;
335         }
336         else if(N.z > limit*(N.x + N.z) && N.z > limit*(N.y + N.z)) {
337                 weight.z = 1.0f;
338         }
339         else if(blend > 0.0f) {
340                 /* in case of blending, test for mixes between two textures */
341                 if(N.z < (1.0f - limit)*(N.y + N.x)) {
342                         weight.x = N.x/(N.x + N.y);
343                         weight.x = clamp((weight.x - 0.5f*(1.0f - blend))/blend, 0.0f, 1.0f);
344                         weight.y = 1.0f - weight.x;
345                 }
346                 else if(N.x < (1.0f - limit)*(N.y + N.z)) {
347                         weight.y = N.y/(N.y + N.z);
348                         weight.y = clamp((weight.y - 0.5f*(1.0f - blend))/blend, 0.0f, 1.0f);
349                         weight.z = 1.0f - weight.y;
350                 }
351                 else if(N.y < (1.0f - limit)*(N.x + N.z)) {
352                         weight.x = N.x/(N.x + N.z);
353                         weight.x = clamp((weight.x - 0.5f*(1.0f - blend))/blend, 0.0f, 1.0f);
354                         weight.z = 1.0f - weight.x;
355                 }
356                 else {
357                         /* last case, we have a mix between three */
358                         weight.x = ((2.0f - limit)*N.x + (limit - 1.0f))/(2.0f*limit - 1.0f);
359                         weight.y = ((2.0f - limit)*N.y + (limit - 1.0f))/(2.0f*limit - 1.0f);
360                         weight.z = ((2.0f - limit)*N.z + (limit - 1.0f))/(2.0f*limit - 1.0f);
361                 }
362         }
363
364         /* now fetch textures */
365         uint co_offset, out_offset, alpha_offset, srgb;
366         decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
367
368         float3 co = stack_load_float3(stack, co_offset);
369         uint id = node.y;
370
371         float4 f = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
372         uint use_alpha = stack_valid(alpha_offset);
373
374         if(weight.x > 0.0f)
375                 f += weight.x*svm_image_texture(kg, id, co.y, co.z, srgb, use_alpha);
376         if(weight.y > 0.0f)
377                 f += weight.y*svm_image_texture(kg, id, co.x, co.z, srgb, use_alpha);
378         if(weight.z > 0.0f)
379                 f += weight.z*svm_image_texture(kg, id, co.y, co.x, srgb, use_alpha);
380
381         if(stack_valid(out_offset))
382                 stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
383         if(stack_valid(alpha_offset))
384                 stack_store_float(stack, alpha_offset, f.w);
385 }
386
387
388 ccl_device void svm_node_tex_environment(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
389 {
390         uint id = node.y;
391         uint co_offset, out_offset, alpha_offset, srgb;
392         uint projection = node.w;
393
394         decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
395
396         float3 co = stack_load_float3(stack, co_offset);
397         float2 uv;
398
399         co = normalize(co);
400         
401         if(projection == 0)
402                 uv = direction_to_equirectangular(co);
403         else
404                 uv = direction_to_mirrorball(co);
405
406         uint use_alpha = stack_valid(alpha_offset);
407         float4 f = svm_image_texture(kg, id, uv.x, uv.y, srgb, use_alpha);
408
409         if(stack_valid(out_offset))
410                 stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
411         if(stack_valid(alpha_offset))
412                 stack_store_float(stack, alpha_offset, f.w);
413 }
414
415 CCL_NAMESPACE_END
416