svn merge: Merging r46558 through r46612, from trunk
[blender-staging.git] / intern / cycles / kernel / svm / svm_image.h
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 CCL_NAMESPACE_BEGIN
20
21 #ifdef __KERNEL_OPENCL__
22
23 /* For OpenCL all images are packed in a single array, and we do manual lookup
24  * and interpolation. */
25
26 __device_inline float4 svm_image_texture_read(KernelGlobals *kg, int offset)
27 {
28         uchar4 r = kernel_tex_fetch(__tex_image_packed, offset);
29         float f = 1.0f/255.0f;
30         return make_float4(r.x*f, r.y*f, r.z*f, r.w*f);
31 }
32
33 __device_inline int svm_image_texture_wrap_periodic(int x, int width)
34 {
35         x %= width;
36         if(x < 0)
37                 x += width;
38         return x;
39 }
40
41 __device_inline int svm_image_texture_wrap_clamp(int x, int width)
42 {
43         return clamp(x, 0, width-1);
44 }
45
46 __device_inline float svm_image_texture_frac(float x, int *ix)
47 {
48         int i = (int)x - ((x < 0.0f)? 1: 0);
49         *ix = i;
50         return x - (float)i;
51 }
52
53 __device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y)
54 {
55         uint4 info = kernel_tex_fetch(__tex_image_packed_info, id);
56         uint width = info.x;
57         uint height = info.y;
58         uint offset = info.z;
59         uint periodic = info.w;
60
61         int ix, iy, nix, niy;
62         float tx = svm_image_texture_frac(x*width, &ix);
63         float ty = svm_image_texture_frac(y*height, &iy);
64
65         if(periodic) {
66                 ix = svm_image_texture_wrap_periodic(ix, width);
67                 iy = svm_image_texture_wrap_periodic(iy, height);
68
69                 nix = svm_image_texture_wrap_periodic(ix+1, width);
70                 niy = svm_image_texture_wrap_periodic(iy+1, height);
71         }
72         else {
73                 ix = svm_image_texture_wrap_clamp(ix, width);
74                 iy = svm_image_texture_wrap_clamp(iy, height);
75
76                 nix = svm_image_texture_wrap_clamp(ix+1, width);
77                 niy = svm_image_texture_wrap_clamp(iy+1, height);
78         }
79
80         float4 r = (1.0f - ty)*(1.0f - tx)*svm_image_texture_read(kg, offset + ix + iy*width);
81         r += (1.0f - ty)*tx*svm_image_texture_read(kg, offset + nix + iy*width);
82         r += ty*(1.0f - tx)*svm_image_texture_read(kg, offset + ix + niy*width);
83         r += ty*tx*svm_image_texture_read(kg, offset + nix + niy*width);
84
85         return r;
86 }
87
88 #else
89
90 __device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y)
91 {
92         float4 r;
93
94         /* not particularly proud of this massive switch, what are the
95            alternatives?
96            - use a single big 1D texture, and do our own lookup/filtering
97            - group by size and use a 3d texture, performance impact
98            - group into larger texture with some padding for correct lerp
99
100            also note that cuda has 128 textures limit, we use 100 now, since
101            we still need some for other storage */
102
103         switch(id) {
104                 case 0: r = kernel_tex_image_interp(__tex_image_000, x, y); break;
105                 case 1: r = kernel_tex_image_interp(__tex_image_001, x, y); break;
106                 case 2: r = kernel_tex_image_interp(__tex_image_002, x, y); break;
107                 case 3: r = kernel_tex_image_interp(__tex_image_003, x, y); break;
108                 case 4: r = kernel_tex_image_interp(__tex_image_004, x, y); break;
109                 case 5: r = kernel_tex_image_interp(__tex_image_005, x, y); break;
110                 case 6: r = kernel_tex_image_interp(__tex_image_006, x, y); break;
111                 case 7: r = kernel_tex_image_interp(__tex_image_007, x, y); break;
112                 case 8: r = kernel_tex_image_interp(__tex_image_008, x, y); break;
113                 case 9: r = kernel_tex_image_interp(__tex_image_009, x, y); break;
114                 case 10: r = kernel_tex_image_interp(__tex_image_010, x, y); break;
115                 case 11: r = kernel_tex_image_interp(__tex_image_011, x, y); break;
116                 case 12: r = kernel_tex_image_interp(__tex_image_012, x, y); break;
117                 case 13: r = kernel_tex_image_interp(__tex_image_013, x, y); break;
118                 case 14: r = kernel_tex_image_interp(__tex_image_014, x, y); break;
119                 case 15: r = kernel_tex_image_interp(__tex_image_015, x, y); break;
120                 case 16: r = kernel_tex_image_interp(__tex_image_016, x, y); break;
121                 case 17: r = kernel_tex_image_interp(__tex_image_017, x, y); break;
122                 case 18: r = kernel_tex_image_interp(__tex_image_018, x, y); break;
123                 case 19: r = kernel_tex_image_interp(__tex_image_019, x, y); break;
124                 case 20: r = kernel_tex_image_interp(__tex_image_020, x, y); break;
125                 case 21: r = kernel_tex_image_interp(__tex_image_021, x, y); break;
126                 case 22: r = kernel_tex_image_interp(__tex_image_022, x, y); break;
127                 case 23: r = kernel_tex_image_interp(__tex_image_023, x, y); break;
128                 case 24: r = kernel_tex_image_interp(__tex_image_024, x, y); break;
129                 case 25: r = kernel_tex_image_interp(__tex_image_025, x, y); break;
130                 case 26: r = kernel_tex_image_interp(__tex_image_026, x, y); break;
131                 case 27: r = kernel_tex_image_interp(__tex_image_027, x, y); break;
132                 case 28: r = kernel_tex_image_interp(__tex_image_028, x, y); break;
133                 case 29: r = kernel_tex_image_interp(__tex_image_029, x, y); break;
134                 case 30: r = kernel_tex_image_interp(__tex_image_030, x, y); break;
135                 case 31: r = kernel_tex_image_interp(__tex_image_031, x, y); break;
136                 case 32: r = kernel_tex_image_interp(__tex_image_032, x, y); break;
137                 case 33: r = kernel_tex_image_interp(__tex_image_033, x, y); break;
138                 case 34: r = kernel_tex_image_interp(__tex_image_034, x, y); break;
139                 case 35: r = kernel_tex_image_interp(__tex_image_035, x, y); break;
140                 case 36: r = kernel_tex_image_interp(__tex_image_036, x, y); break;
141                 case 37: r = kernel_tex_image_interp(__tex_image_037, x, y); break;
142                 case 38: r = kernel_tex_image_interp(__tex_image_038, x, y); break;
143                 case 39: r = kernel_tex_image_interp(__tex_image_039, x, y); break;
144                 case 40: r = kernel_tex_image_interp(__tex_image_040, x, y); break;
145                 case 41: r = kernel_tex_image_interp(__tex_image_041, x, y); break;
146                 case 42: r = kernel_tex_image_interp(__tex_image_042, x, y); break;
147                 case 43: r = kernel_tex_image_interp(__tex_image_043, x, y); break;
148                 case 44: r = kernel_tex_image_interp(__tex_image_044, x, y); break;
149                 case 45: r = kernel_tex_image_interp(__tex_image_045, x, y); break;
150                 case 46: r = kernel_tex_image_interp(__tex_image_046, x, y); break;
151                 case 47: r = kernel_tex_image_interp(__tex_image_047, x, y); break;
152                 case 48: r = kernel_tex_image_interp(__tex_image_048, x, y); break;
153                 case 49: r = kernel_tex_image_interp(__tex_image_049, x, y); break;
154                 case 50: r = kernel_tex_image_interp(__tex_image_050, x, y); break;
155                 case 51: r = kernel_tex_image_interp(__tex_image_051, x, y); break;
156                 case 52: r = kernel_tex_image_interp(__tex_image_052, x, y); break;
157                 case 53: r = kernel_tex_image_interp(__tex_image_053, x, y); break;
158                 case 54: r = kernel_tex_image_interp(__tex_image_054, x, y); break;
159                 case 55: r = kernel_tex_image_interp(__tex_image_055, x, y); break;
160                 case 56: r = kernel_tex_image_interp(__tex_image_056, x, y); break;
161                 case 57: r = kernel_tex_image_interp(__tex_image_057, x, y); break;
162                 case 58: r = kernel_tex_image_interp(__tex_image_058, x, y); break;
163                 case 59: r = kernel_tex_image_interp(__tex_image_059, x, y); break;
164                 case 60: r = kernel_tex_image_interp(__tex_image_060, x, y); break;
165                 case 61: r = kernel_tex_image_interp(__tex_image_061, x, y); break;
166                 case 62: r = kernel_tex_image_interp(__tex_image_062, x, y); break;
167                 case 63: r = kernel_tex_image_interp(__tex_image_063, x, y); break;
168                 case 64: r = kernel_tex_image_interp(__tex_image_064, x, y); break;
169                 case 65: r = kernel_tex_image_interp(__tex_image_065, x, y); break;
170                 case 66: r = kernel_tex_image_interp(__tex_image_066, x, y); break;
171                 case 67: r = kernel_tex_image_interp(__tex_image_067, x, y); break;
172                 case 68: r = kernel_tex_image_interp(__tex_image_068, x, y); break;
173                 case 69: r = kernel_tex_image_interp(__tex_image_069, x, y); break;
174                 case 70: r = kernel_tex_image_interp(__tex_image_070, x, y); break;
175                 case 71: r = kernel_tex_image_interp(__tex_image_071, x, y); break;
176                 case 72: r = kernel_tex_image_interp(__tex_image_072, x, y); break;
177                 case 73: r = kernel_tex_image_interp(__tex_image_073, x, y); break;
178                 case 74: r = kernel_tex_image_interp(__tex_image_074, x, y); break;
179                 case 75: r = kernel_tex_image_interp(__tex_image_075, x, y); break;
180                 case 76: r = kernel_tex_image_interp(__tex_image_076, x, y); break;
181                 case 77: r = kernel_tex_image_interp(__tex_image_077, x, y); break;
182                 case 78: r = kernel_tex_image_interp(__tex_image_078, x, y); break;
183                 case 79: r = kernel_tex_image_interp(__tex_image_079, x, y); break;
184                 case 80: r = kernel_tex_image_interp(__tex_image_080, x, y); break;
185                 case 81: r = kernel_tex_image_interp(__tex_image_081, x, y); break;
186                 case 82: r = kernel_tex_image_interp(__tex_image_082, x, y); break;
187                 case 83: r = kernel_tex_image_interp(__tex_image_083, x, y); break;
188                 case 84: r = kernel_tex_image_interp(__tex_image_084, x, y); break;
189                 case 85: r = kernel_tex_image_interp(__tex_image_085, x, y); break;
190                 case 86: r = kernel_tex_image_interp(__tex_image_086, x, y); break;
191                 case 87: r = kernel_tex_image_interp(__tex_image_087, x, y); break;
192                 case 88: r = kernel_tex_image_interp(__tex_image_088, x, y); break;
193                 case 89: r = kernel_tex_image_interp(__tex_image_089, x, y); break;
194                 case 90: r = kernel_tex_image_interp(__tex_image_090, x, y); break;
195                 case 91: r = kernel_tex_image_interp(__tex_image_091, x, y); break;
196                 case 92: r = kernel_tex_image_interp(__tex_image_092, x, y); break;
197                 case 93: r = kernel_tex_image_interp(__tex_image_093, x, y); break;
198                 case 94: r = kernel_tex_image_interp(__tex_image_094, x, y); break;
199                 case 95: r = kernel_tex_image_interp(__tex_image_float_095, x, y); break;
200                 case 96: r = kernel_tex_image_interp(__tex_image_float_096, x, y); break;
201                 case 97: r = kernel_tex_image_interp(__tex_image_float_097, x, y); break;
202                 case 98: r = kernel_tex_image_interp(__tex_image_float_098, x, y); break;
203                 case 99: r = kernel_tex_image_interp(__tex_image_float_099, x, y); break;
204                 default: 
205                         kernel_assert(0);
206                         return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
207         }
208
209         return r;
210 }
211
212 #endif
213
214 __device void svm_node_tex_image(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
215 {
216         uint id = node.y;
217         uint co_offset, out_offset, alpha_offset, srgb;
218
219         decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
220
221         float3 co = stack_load_float3(stack, co_offset);
222         float4 f = svm_image_texture(kg, id, co.x, co.y);
223         float3 r = make_float3(f.x, f.y, f.z);
224
225         if(srgb) {
226                 r.x = color_srgb_to_scene_linear(r.x);
227                 r.y = color_srgb_to_scene_linear(r.y);
228                 r.z = color_srgb_to_scene_linear(r.z);
229         }
230
231         if(stack_valid(out_offset))
232                 stack_store_float3(stack, out_offset, r);
233         if(stack_valid(alpha_offset))
234                 stack_store_float(stack, alpha_offset, f.w);
235 }
236
237 __device void svm_node_tex_environment(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
238 {
239         uint id = node.y;
240         uint co_offset, out_offset, alpha_offset, srgb;
241         uint projection = node.w;
242
243         decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
244
245         float3 co = stack_load_float3(stack, co_offset);
246         float2 uv;
247
248         co = normalize(co);
249         
250         if(projection == 0)
251                 uv = direction_to_equirectangular(co);
252         else
253                 uv = direction_to_mirrorball(co);
254
255         float4 f = svm_image_texture(kg, id, uv.x, uv.y);
256         float3 r = make_float3(f.x, f.y, f.z);
257
258         if(srgb) {
259                 r.x = color_srgb_to_scene_linear(r.x);
260                 r.y = color_srgb_to_scene_linear(r.y);
261                 r.z = color_srgb_to_scene_linear(r.z);
262         }
263
264         if(stack_valid(out_offset))
265                 stack_store_float3(stack, out_offset, r);
266         if(stack_valid(alpha_offset))
267                 stack_store_float(stack, alpha_offset, f.w);
268 }
269
270 CCL_NAMESPACE_END
271