Cycles: svn merge -r40934:41157 ^/trunk/blender
[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 __device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y)
22 {
23         float4 r;
24
25         /* not particularly proud of this massive switch, what are the
26            alternatives?
27            - use a single big 1D texture, and do our own lookup/filtering
28            - group by size and use a 3d texture, performance impact
29            - group into larger texture with some padding for correct lerp
30
31            also note that cuda has 128 textures limit, we use 100 now, since
32            we still need some for other storage */
33
34 #ifdef __KERNEL_OPENCL__
35         r = make_float4(0.0f, 0.0f, 0.0f, 0.0f); /* todo */
36 #else
37         switch(id) {
38                 case 0: r = kernel_tex_image_interp(__tex_image_000, x, y); break;
39                 case 1: r = kernel_tex_image_interp(__tex_image_001, x, y); break;
40                 case 2: r = kernel_tex_image_interp(__tex_image_002, x, y); break;
41                 case 3: r = kernel_tex_image_interp(__tex_image_003, x, y); break;
42                 case 4: r = kernel_tex_image_interp(__tex_image_004, x, y); break;
43                 case 5: r = kernel_tex_image_interp(__tex_image_005, x, y); break;
44                 case 6: r = kernel_tex_image_interp(__tex_image_006, x, y); break;
45                 case 7: r = kernel_tex_image_interp(__tex_image_007, x, y); break;
46                 case 8: r = kernel_tex_image_interp(__tex_image_008, x, y); break;
47                 case 9: r = kernel_tex_image_interp(__tex_image_009, x, y); break;
48                 case 10: r = kernel_tex_image_interp(__tex_image_010, x, y); break;
49                 case 11: r = kernel_tex_image_interp(__tex_image_011, x, y); break;
50                 case 12: r = kernel_tex_image_interp(__tex_image_012, x, y); break;
51                 case 13: r = kernel_tex_image_interp(__tex_image_013, x, y); break;
52                 case 14: r = kernel_tex_image_interp(__tex_image_014, x, y); break;
53                 case 15: r = kernel_tex_image_interp(__tex_image_015, x, y); break;
54                 case 16: r = kernel_tex_image_interp(__tex_image_016, x, y); break;
55                 case 17: r = kernel_tex_image_interp(__tex_image_017, x, y); break;
56                 case 18: r = kernel_tex_image_interp(__tex_image_018, x, y); break;
57                 case 19: r = kernel_tex_image_interp(__tex_image_019, x, y); break;
58                 case 20: r = kernel_tex_image_interp(__tex_image_020, x, y); break;
59                 case 21: r = kernel_tex_image_interp(__tex_image_021, x, y); break;
60                 case 22: r = kernel_tex_image_interp(__tex_image_022, x, y); break;
61                 case 23: r = kernel_tex_image_interp(__tex_image_023, x, y); break;
62                 case 24: r = kernel_tex_image_interp(__tex_image_024, x, y); break;
63                 case 25: r = kernel_tex_image_interp(__tex_image_025, x, y); break;
64                 case 26: r = kernel_tex_image_interp(__tex_image_026, x, y); break;
65                 case 27: r = kernel_tex_image_interp(__tex_image_027, x, y); break;
66                 case 28: r = kernel_tex_image_interp(__tex_image_028, x, y); break;
67                 case 29: r = kernel_tex_image_interp(__tex_image_029, x, y); break;
68                 case 30: r = kernel_tex_image_interp(__tex_image_030, x, y); break;
69                 case 31: r = kernel_tex_image_interp(__tex_image_031, x, y); break;
70                 case 32: r = kernel_tex_image_interp(__tex_image_032, x, y); break;
71                 case 33: r = kernel_tex_image_interp(__tex_image_033, x, y); break;
72                 case 34: r = kernel_tex_image_interp(__tex_image_034, x, y); break;
73                 case 35: r = kernel_tex_image_interp(__tex_image_035, x, y); break;
74                 case 36: r = kernel_tex_image_interp(__tex_image_036, x, y); break;
75                 case 37: r = kernel_tex_image_interp(__tex_image_037, x, y); break;
76                 case 38: r = kernel_tex_image_interp(__tex_image_038, x, y); break;
77                 case 39: r = kernel_tex_image_interp(__tex_image_039, x, y); break;
78                 case 40: r = kernel_tex_image_interp(__tex_image_040, x, y); break;
79                 case 41: r = kernel_tex_image_interp(__tex_image_041, x, y); break;
80                 case 42: r = kernel_tex_image_interp(__tex_image_042, x, y); break;
81                 case 43: r = kernel_tex_image_interp(__tex_image_043, x, y); break;
82                 case 44: r = kernel_tex_image_interp(__tex_image_044, x, y); break;
83                 case 45: r = kernel_tex_image_interp(__tex_image_045, x, y); break;
84                 case 46: r = kernel_tex_image_interp(__tex_image_046, x, y); break;
85                 case 47: r = kernel_tex_image_interp(__tex_image_047, x, y); break;
86                 case 48: r = kernel_tex_image_interp(__tex_image_048, x, y); break;
87                 case 49: r = kernel_tex_image_interp(__tex_image_049, x, y); break;
88                 case 50: r = kernel_tex_image_interp(__tex_image_050, x, y); break;
89                 case 51: r = kernel_tex_image_interp(__tex_image_051, x, y); break;
90                 case 52: r = kernel_tex_image_interp(__tex_image_052, x, y); break;
91                 case 53: r = kernel_tex_image_interp(__tex_image_053, x, y); break;
92                 case 54: r = kernel_tex_image_interp(__tex_image_054, x, y); break;
93                 case 55: r = kernel_tex_image_interp(__tex_image_055, x, y); break;
94                 case 56: r = kernel_tex_image_interp(__tex_image_056, x, y); break;
95                 case 57: r = kernel_tex_image_interp(__tex_image_057, x, y); break;
96                 case 58: r = kernel_tex_image_interp(__tex_image_058, x, y); break;
97                 case 59: r = kernel_tex_image_interp(__tex_image_059, x, y); break;
98                 case 60: r = kernel_tex_image_interp(__tex_image_060, x, y); break;
99                 case 61: r = kernel_tex_image_interp(__tex_image_061, x, y); break;
100                 case 62: r = kernel_tex_image_interp(__tex_image_062, x, y); break;
101                 case 63: r = kernel_tex_image_interp(__tex_image_063, x, y); break;
102                 case 64: r = kernel_tex_image_interp(__tex_image_064, x, y); break;
103                 case 65: r = kernel_tex_image_interp(__tex_image_065, x, y); break;
104                 case 66: r = kernel_tex_image_interp(__tex_image_066, x, y); break;
105                 case 67: r = kernel_tex_image_interp(__tex_image_067, x, y); break;
106                 case 68: r = kernel_tex_image_interp(__tex_image_068, x, y); break;
107                 case 69: r = kernel_tex_image_interp(__tex_image_069, x, y); break;
108                 case 70: r = kernel_tex_image_interp(__tex_image_070, x, y); break;
109                 case 71: r = kernel_tex_image_interp(__tex_image_071, x, y); break;
110                 case 72: r = kernel_tex_image_interp(__tex_image_072, x, y); break;
111                 case 73: r = kernel_tex_image_interp(__tex_image_073, x, y); break;
112                 case 74: r = kernel_tex_image_interp(__tex_image_074, x, y); break;
113                 case 75: r = kernel_tex_image_interp(__tex_image_075, x, y); break;
114                 case 76: r = kernel_tex_image_interp(__tex_image_076, x, y); break;
115                 case 77: r = kernel_tex_image_interp(__tex_image_077, x, y); break;
116                 case 78: r = kernel_tex_image_interp(__tex_image_078, x, y); break;
117                 case 79: r = kernel_tex_image_interp(__tex_image_079, x, y); break;
118                 case 80: r = kernel_tex_image_interp(__tex_image_080, x, y); break;
119                 case 81: r = kernel_tex_image_interp(__tex_image_081, x, y); break;
120                 case 82: r = kernel_tex_image_interp(__tex_image_082, x, y); break;
121                 case 83: r = kernel_tex_image_interp(__tex_image_083, x, y); break;
122                 case 84: r = kernel_tex_image_interp(__tex_image_084, x, y); break;
123                 case 85: r = kernel_tex_image_interp(__tex_image_085, x, y); break;
124                 case 86: r = kernel_tex_image_interp(__tex_image_086, x, y); break;
125                 case 87: r = kernel_tex_image_interp(__tex_image_087, x, y); break;
126                 case 88: r = kernel_tex_image_interp(__tex_image_088, x, y); break;
127                 case 89: r = kernel_tex_image_interp(__tex_image_089, x, y); break;
128                 case 90: r = kernel_tex_image_interp(__tex_image_090, x, y); break;
129                 case 91: r = kernel_tex_image_interp(__tex_image_091, x, y); break;
130                 case 92: r = kernel_tex_image_interp(__tex_image_092, x, y); break;
131                 case 93: r = kernel_tex_image_interp(__tex_image_093, x, y); break;
132                 case 94: r = kernel_tex_image_interp(__tex_image_094, x, y); break;
133                 case 95: r = kernel_tex_image_interp(__tex_image_095, x, y); break;
134                 case 96: r = kernel_tex_image_interp(__tex_image_096, x, y); break;
135                 case 97: r = kernel_tex_image_interp(__tex_image_097, x, y); break;
136                 case 98: r = kernel_tex_image_interp(__tex_image_098, x, y); break;
137                 case 99: r = kernel_tex_image_interp(__tex_image_099, x, y); break;
138                 default: 
139                         kernel_assert(0);
140                         return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
141         }
142 #endif
143
144         return r;
145 }
146
147 __device void svm_node_tex_image(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
148 {
149         uint id = node.y;
150         uint co_offset, out_offset, alpha_offset, srgb;
151
152         decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
153
154         float3 co = stack_load_float3(stack, co_offset);
155         float4 f = svm_image_texture(kg, id, co.x, co.y);
156         float3 r = make_float3(f.x, f.y, f.z);
157
158         if(srgb) {
159                 r.x = color_srgb_to_scene_linear(r.x);
160                 r.y = color_srgb_to_scene_linear(r.y);
161                 r.z = color_srgb_to_scene_linear(r.z);
162         }
163
164         if(stack_valid(out_offset))
165                 stack_store_float3(stack, out_offset, r);
166         if(stack_valid(alpha_offset))
167                 stack_store_float(stack, alpha_offset, f.w);
168 }
169
170 __device void svm_node_tex_environment(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
171 {
172         uint id = node.y;
173         uint co_offset, out_offset, alpha_offset, srgb;
174
175         decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
176
177         float3 co = stack_load_float3(stack, co_offset);
178         float u = (atan2f(co.y, co.x) + M_PI_F)/(2*M_PI_F);
179         float v = atan2f(co.z, hypotf(co.x, co.y))/M_PI_F + 0.5f;
180         float4 f = svm_image_texture(kg, id, u, v);
181         float3 r = make_float3(f.x, f.y, f.z);
182
183         if(srgb) {
184                 r.x = color_srgb_to_scene_linear(r.x);
185                 r.y = color_srgb_to_scene_linear(r.y);
186                 r.z = color_srgb_to_scene_linear(r.z);
187         }
188
189         if(stack_valid(out_offset))
190                 stack_store_float3(stack, out_offset, r);
191         if(stack_valid(alpha_offset))
192                 stack_store_float(stack, alpha_offset, f.w);
193 }
194
195 CCL_NAMESPACE_END
196