Fix Cycles OpenCL not taking Extend and Clip extension types into account.
[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 /* Float4 textures on various devices. */
20 #if defined(__KERNEL_CPU__)
21 #  define TEX_NUM_FLOAT4_IMAGES TEX_NUM_FLOAT4_CPU
22 #elif defined(__KERNEL_CUDA__)
23 #  if __CUDA_ARCH__ < 300
24 #    define TEX_NUM_FLOAT4_IMAGES       TEX_NUM_FLOAT4_CUDA
25 #  else
26 #    define TEX_NUM_FLOAT4_IMAGES       TEX_NUM_FLOAT4_CUDA_KEPLER
27 #  endif
28 #else
29 #  define TEX_NUM_FLOAT4_IMAGES TEX_NUM_FLOAT4_OPENCL
30 #endif
31
32 #ifdef __KERNEL_OPENCL__
33
34 /* For OpenCL all images are packed in a single array, and we do manual lookup
35  * and interpolation. */
36
37 ccl_device_inline float4 svm_image_texture_read(KernelGlobals *kg, int id, int offset)
38 {
39         if(id >= TEX_NUM_FLOAT4_IMAGES) {
40                 uchar4 r = kernel_tex_fetch(__tex_image_byte4_packed, offset);
41                 float f = 1.0f/255.0f;
42                 return make_float4(r.x*f, r.y*f, r.z*f, r.w*f);
43         }
44         else {
45                 return kernel_tex_fetch(__tex_image_float4_packed, offset);
46         }
47 }
48
49 ccl_device_inline int svm_image_texture_wrap_periodic(int x, int width)
50 {
51         x %= width;
52         if(x < 0)
53                 x += width;
54         return x;
55 }
56
57 ccl_device_inline int svm_image_texture_wrap_clamp(int x, int width)
58 {
59         return clamp(x, 0, width-1);
60 }
61
62 ccl_device_inline float svm_image_texture_frac(float x, int *ix)
63 {
64         int i = float_to_int(x) - ((x < 0.0f)? 1: 0);
65         *ix = i;
66         return x - (float)i;
67 }
68
69 ccl_device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, uint srgb, uint use_alpha)
70 {
71         uint4 info = kernel_tex_fetch(__tex_image_packed_info, id);
72         uint width = info.x;
73         uint height = info.y;
74         uint offset = info.z;
75
76         /* Image Options */
77         uint interpolation = (info.w & (1 << 0)) ? INTERPOLATION_CLOSEST : INTERPOLATION_LINEAR;
78         uint extension;
79         if(info.w & (1 << 1))
80                 extension = EXTENSION_REPEAT;
81         else if(info.w & (1 << 2))
82                 extension = EXTENSION_EXTEND;
83         else
84                 extension = EXTENSION_CLIP;
85
86         float4 r;
87         int ix, iy, nix, niy;
88         if(interpolation == INTERPOLATION_CLOSEST) {
89                 svm_image_texture_frac(x*width, &ix);
90                 svm_image_texture_frac(y*height, &iy);
91
92                 if(extension == EXTENSION_REPEAT) {
93                         ix = svm_image_texture_wrap_periodic(ix, width);
94                         iy = svm_image_texture_wrap_periodic(iy, height);
95                 }
96                 else if(extension == EXTENSION_CLIP) {
97                         if(x < 0.0f || y < 0.0f || x > 1.0f || y > 1.0f)
98                                 return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
99                 }
100                 else { /* EXTENSION_EXTEND */
101                         ix = svm_image_texture_wrap_clamp(ix, width);
102                         iy = svm_image_texture_wrap_clamp(iy, height);
103                 }
104
105                 r = svm_image_texture_read(kg, id, offset + ix + iy*width);
106         }
107         else { /* INTERPOLATION_LINEAR */
108                 float tx = svm_image_texture_frac(x*width - 0.5f, &ix);
109                 float ty = svm_image_texture_frac(y*height - 0.5f, &iy);
110
111                 if(extension == EXTENSION_REPEAT) {
112                         ix = svm_image_texture_wrap_periodic(ix, width);
113                         iy = svm_image_texture_wrap_periodic(iy, height);
114
115                         nix = svm_image_texture_wrap_periodic(ix+1, width);
116                         niy = svm_image_texture_wrap_periodic(iy+1, height);
117                 }
118                 else if(extension == EXTENSION_CLIP) {
119                         if(x < 0.0f || y < 0.0f || x > 1.0f || y > 1.0f)
120                                 return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
121                 }
122                 else { /* EXTENSION_EXTEND */
123                         ix = svm_image_texture_wrap_clamp(ix, width);
124                         iy = svm_image_texture_wrap_clamp(iy, height);
125
126                         nix = svm_image_texture_wrap_clamp(ix+1, width);
127                         niy = svm_image_texture_wrap_clamp(iy+1, height);
128                 }
129
130                 r = (1.0f - ty)*(1.0f - tx)*svm_image_texture_read(kg, id, offset + ix + iy*width);
131                 r += (1.0f - ty)*tx*svm_image_texture_read(kg, id, offset + nix + iy*width);
132                 r += ty*(1.0f - tx)*svm_image_texture_read(kg, id, offset + ix + niy*width);
133                 r += ty*tx*svm_image_texture_read(kg, id, offset + nix + niy*width);
134         }
135
136         if(use_alpha && r.w != 1.0f && r.w != 0.0f) {
137                 float invw = 1.0f/r.w;
138                 r.x *= invw;
139                 r.y *= invw;
140                 r.z *= invw;
141
142                 if(id >= TEX_NUM_FLOAT4_IMAGES) {
143                         r.x = min(r.x, 1.0f);
144                         r.y = min(r.y, 1.0f);
145                         r.z = min(r.z, 1.0f);
146                 }
147         }
148
149         if(srgb) {
150                 r.x = color_srgb_to_scene_linear(r.x);
151                 r.y = color_srgb_to_scene_linear(r.y);
152                 r.z = color_srgb_to_scene_linear(r.z);
153         }
154
155         return r;
156 }
157
158 #else
159
160 ccl_device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, uint srgb, uint use_alpha)
161 {
162 #ifdef __KERNEL_CPU__
163 #  ifdef __KERNEL_SSE2__
164         ssef r_ssef;
165         float4 &r = (float4 &)r_ssef;
166         r = kernel_tex_image_interp(id, x, y);
167 #  else
168         float4 r = kernel_tex_image_interp(id, x, y);
169 #  endif
170 #else
171         float4 r;
172
173 #  if __CUDA_ARCH__ < 300
174         /* not particularly proud of this massive switch, what are the
175          * alternatives?
176          * - use a single big 1D texture, and do our own lookup/filtering
177          * - group by size and use a 3d texture, performance impact
178          * - group into larger texture with some padding for correct lerp
179          *
180          * also note that cuda has a textures limit (128 for Fermi, 256 for Kepler),
181          * and we cannot use all since we still need some for other storage */
182
183         switch(id) {
184                 case 0: r = kernel_tex_image_interp(__tex_image_float4_000, x, y); break;
185                 case 1: r = kernel_tex_image_interp(__tex_image_float4_001, x, y); break;
186                 case 2: r = kernel_tex_image_interp(__tex_image_float4_002, x, y); break;
187                 case 3: r = kernel_tex_image_interp(__tex_image_float4_003, x, y); break;
188                 case 4: r = kernel_tex_image_interp(__tex_image_float4_004, x, y); break;
189                 case 5: r = kernel_tex_image_interp(__tex_image_byte4_005, x, y); break;
190                 case 6: r = kernel_tex_image_interp(__tex_image_byte4_006, x, y); break;
191                 case 7: r = kernel_tex_image_interp(__tex_image_byte4_007, x, y); break;
192                 case 8: r = kernel_tex_image_interp(__tex_image_byte4_008, x, y); break;
193                 case 9: r = kernel_tex_image_interp(__tex_image_byte4_009, x, y); break;
194                 case 10: r = kernel_tex_image_interp(__tex_image_byte4_010, x, y); break;
195                 case 11: r = kernel_tex_image_interp(__tex_image_byte4_011, x, y); break;
196                 case 12: r = kernel_tex_image_interp(__tex_image_byte4_012, x, y); break;
197                 case 13: r = kernel_tex_image_interp(__tex_image_byte4_013, x, y); break;
198                 case 14: r = kernel_tex_image_interp(__tex_image_byte4_014, x, y); break;
199                 case 15: r = kernel_tex_image_interp(__tex_image_byte4_015, x, y); break;
200                 case 16: r = kernel_tex_image_interp(__tex_image_byte4_016, x, y); break;
201                 case 17: r = kernel_tex_image_interp(__tex_image_byte4_017, x, y); break;
202                 case 18: r = kernel_tex_image_interp(__tex_image_byte4_018, x, y); break;
203                 case 19: r = kernel_tex_image_interp(__tex_image_byte4_019, x, y); break;
204                 case 20: r = kernel_tex_image_interp(__tex_image_byte4_020, x, y); break;
205                 case 21: r = kernel_tex_image_interp(__tex_image_byte4_021, x, y); break;
206                 case 22: r = kernel_tex_image_interp(__tex_image_byte4_022, x, y); break;
207                 case 23: r = kernel_tex_image_interp(__tex_image_byte4_023, x, y); break;
208                 case 24: r = kernel_tex_image_interp(__tex_image_byte4_024, x, y); break;
209                 case 25: r = kernel_tex_image_interp(__tex_image_byte4_025, x, y); break;
210                 case 26: r = kernel_tex_image_interp(__tex_image_byte4_026, x, y); break;
211                 case 27: r = kernel_tex_image_interp(__tex_image_byte4_027, x, y); break;
212                 case 28: r = kernel_tex_image_interp(__tex_image_byte4_028, x, y); break;
213                 case 29: r = kernel_tex_image_interp(__tex_image_byte4_029, x, y); break;
214                 case 30: r = kernel_tex_image_interp(__tex_image_byte4_030, x, y); break;
215                 case 31: r = kernel_tex_image_interp(__tex_image_byte4_031, x, y); break;
216                 case 32: r = kernel_tex_image_interp(__tex_image_byte4_032, x, y); break;
217                 case 33: r = kernel_tex_image_interp(__tex_image_byte4_033, x, y); break;
218                 case 34: r = kernel_tex_image_interp(__tex_image_byte4_034, x, y); break;
219                 case 35: r = kernel_tex_image_interp(__tex_image_byte4_035, x, y); break;
220                 case 36: r = kernel_tex_image_interp(__tex_image_byte4_036, x, y); break;
221                 case 37: r = kernel_tex_image_interp(__tex_image_byte4_037, x, y); break;
222                 case 38: r = kernel_tex_image_interp(__tex_image_byte4_038, x, y); break;
223                 case 39: r = kernel_tex_image_interp(__tex_image_byte4_039, x, y); break;
224                 case 40: r = kernel_tex_image_interp(__tex_image_byte4_040, x, y); break;
225                 case 41: r = kernel_tex_image_interp(__tex_image_byte4_041, x, y); break;
226                 case 42: r = kernel_tex_image_interp(__tex_image_byte4_042, x, y); break;
227                 case 43: r = kernel_tex_image_interp(__tex_image_byte4_043, x, y); break;
228                 case 44: r = kernel_tex_image_interp(__tex_image_byte4_044, x, y); break;
229                 case 45: r = kernel_tex_image_interp(__tex_image_byte4_045, x, y); break;
230                 case 46: r = kernel_tex_image_interp(__tex_image_byte4_046, x, y); break;
231                 case 47: r = kernel_tex_image_interp(__tex_image_byte4_047, x, y); break;
232                 case 48: r = kernel_tex_image_interp(__tex_image_byte4_048, x, y); break;
233                 case 49: r = kernel_tex_image_interp(__tex_image_byte4_049, x, y); break;
234                 case 50: r = kernel_tex_image_interp(__tex_image_byte4_050, x, y); break;
235                 case 51: r = kernel_tex_image_interp(__tex_image_byte4_051, x, y); break;
236                 case 52: r = kernel_tex_image_interp(__tex_image_byte4_052, x, y); break;
237                 case 53: r = kernel_tex_image_interp(__tex_image_byte4_053, x, y); break;
238                 case 54: r = kernel_tex_image_interp(__tex_image_byte4_054, x, y); break;
239                 case 55: r = kernel_tex_image_interp(__tex_image_byte4_055, x, y); break;
240                 case 56: r = kernel_tex_image_interp(__tex_image_byte4_056, x, y); break;
241                 case 57: r = kernel_tex_image_interp(__tex_image_byte4_057, x, y); break;
242                 case 58: r = kernel_tex_image_interp(__tex_image_byte4_058, x, y); break;
243                 case 59: r = kernel_tex_image_interp(__tex_image_byte4_059, x, y); break;
244                 case 60: r = kernel_tex_image_interp(__tex_image_byte4_060, x, y); break;
245                 case 61: r = kernel_tex_image_interp(__tex_image_byte4_061, x, y); break;
246                 case 62: r = kernel_tex_image_interp(__tex_image_byte4_062, x, y); break;
247                 case 63: r = kernel_tex_image_interp(__tex_image_byte4_063, x, y); break;
248                 case 64: r = kernel_tex_image_interp(__tex_image_byte4_064, x, y); break;
249                 case 65: r = kernel_tex_image_interp(__tex_image_byte4_065, x, y); break;
250                 case 66: r = kernel_tex_image_interp(__tex_image_byte4_066, x, y); break;
251                 case 67: r = kernel_tex_image_interp(__tex_image_byte4_067, x, y); break;
252                 case 68: r = kernel_tex_image_interp(__tex_image_byte4_068, x, y); break;
253                 case 69: r = kernel_tex_image_interp(__tex_image_byte4_069, x, y); break;
254                 case 70: r = kernel_tex_image_interp(__tex_image_byte4_070, x, y); break;
255                 case 71: r = kernel_tex_image_interp(__tex_image_byte4_071, x, y); break;
256                 case 72: r = kernel_tex_image_interp(__tex_image_byte4_072, x, y); break;
257                 case 73: r = kernel_tex_image_interp(__tex_image_byte4_073, x, y); break;
258                 case 74: r = kernel_tex_image_interp(__tex_image_byte4_074, x, y); break;
259                 case 75: r = kernel_tex_image_interp(__tex_image_byte4_075, x, y); break;
260                 case 76: r = kernel_tex_image_interp(__tex_image_byte4_076, x, y); break;
261                 case 77: r = kernel_tex_image_interp(__tex_image_byte4_077, x, y); break;
262                 case 78: r = kernel_tex_image_interp(__tex_image_byte4_078, x, y); break;
263                 case 79: r = kernel_tex_image_interp(__tex_image_byte4_079, x, y); break;
264                 case 80: r = kernel_tex_image_interp(__tex_image_byte4_080, x, y); break;
265                 case 81: r = kernel_tex_image_interp(__tex_image_byte4_081, x, y); break;
266                 case 82: r = kernel_tex_image_interp(__tex_image_byte4_082, x, y); break;
267                 case 83: r = kernel_tex_image_interp(__tex_image_byte4_083, x, y); break;
268                 case 84: r = kernel_tex_image_interp(__tex_image_byte4_084, x, y); break;
269                 case 85: r = kernel_tex_image_interp(__tex_image_byte4_085, x, y); break;
270                 case 86: r = kernel_tex_image_interp(__tex_image_byte4_086, x, y); break;
271                 case 87: r = kernel_tex_image_interp(__tex_image_byte4_087, x, y); break;
272                 case 88: r = kernel_tex_image_interp(__tex_image_byte4_088, x, y); break;
273                 case 89: r = kernel_tex_image_interp(__tex_image_byte4_089, x, y); break;
274                 case 90: r = kernel_tex_image_interp(__tex_image_byte4_090, x, y); break;
275                 case 91: r = kernel_tex_image_interp(__tex_image_byte4_091, x, y); break;
276                 case 92: r = kernel_tex_image_interp(__tex_image_byte4_092, x, y); break;
277                 default:
278                         kernel_assert(0);
279                         return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
280         }
281 #  else
282         CUtexObject tex = kernel_tex_fetch(__bindless_mapping, id);
283         if(id < 2048) /* TODO(dingto): Make this a variable */
284                 r = kernel_tex_image_interp_float4(tex, x, y);
285         else {
286                 float f = kernel_tex_image_interp_float(tex, x, y);
287                 r = make_float4(f, f, f, 1.0);
288         }
289 #  endif
290 #endif
291
292 #ifdef __KERNEL_SSE2__
293         float alpha = r.w;
294
295         if(use_alpha && alpha != 1.0f && alpha != 0.0f) {
296                 r_ssef = r_ssef / ssef(alpha);
297                 if(id >= TEX_NUM_FLOAT4_IMAGES)
298                         r_ssef = min(r_ssef, ssef(1.0f));
299                 r.w = alpha;
300         }
301
302         if(srgb) {
303                 r_ssef = color_srgb_to_scene_linear(r_ssef);
304                 r.w = alpha;
305         }
306 #else
307         if(use_alpha && r.w != 1.0f && r.w != 0.0f) {
308                 float invw = 1.0f/r.w;
309                 r.x *= invw;
310                 r.y *= invw;
311                 r.z *= invw;
312
313                 if(id >= TEX_NUM_FLOAT4_IMAGES) {
314                         r.x = min(r.x, 1.0f);
315                         r.y = min(r.y, 1.0f);
316                         r.z = min(r.z, 1.0f);
317                 }
318         }
319
320         if(srgb) {
321                 r.x = color_srgb_to_scene_linear(r.x);
322                 r.y = color_srgb_to_scene_linear(r.y);
323                 r.z = color_srgb_to_scene_linear(r.z);
324         }
325 #endif
326
327         return r;
328 }
329
330 #endif
331
332 /* Remap coordnate from 0..1 box to -1..-1 */
333 ccl_device_inline float3 texco_remap_square(float3 co)
334 {
335         return (co - make_float3(0.5f, 0.5f, 0.5f)) * 2.0f;
336 }
337
338 ccl_device void svm_node_tex_image(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
339 {
340         uint id = node.y;
341         uint co_offset, out_offset, alpha_offset, srgb;
342
343         decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
344
345         float3 co = stack_load_float3(stack, co_offset);
346         float2 tex_co;
347         uint use_alpha = stack_valid(alpha_offset);
348         if(node.w == NODE_IMAGE_PROJ_SPHERE) {
349                 co = texco_remap_square(co);
350                 tex_co = map_to_sphere(co);
351         }
352         else if(node.w == NODE_IMAGE_PROJ_TUBE) {
353                 co = texco_remap_square(co);
354                 tex_co = map_to_tube(co);
355         }
356         else {
357                 tex_co = make_float2(co.x, co.y);
358         }
359         float4 f = svm_image_texture(kg, id, tex_co.x, tex_co.y, srgb, use_alpha);
360
361         if(stack_valid(out_offset))
362                 stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
363         if(stack_valid(alpha_offset))
364                 stack_store_float(stack, alpha_offset, f.w);
365 }
366
367 ccl_device void svm_node_tex_image_box(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
368 {
369         /* get object space normal */
370         float3 N = ccl_fetch(sd, N);
371
372         N = ccl_fetch(sd, N);
373         if(ccl_fetch(sd, object) != OBJECT_NONE)
374                 object_inverse_normal_transform(kg, sd, &N);
375
376         /* project from direction vector to barycentric coordinates in triangles */
377         N.x = fabsf(N.x);
378         N.y = fabsf(N.y);
379         N.z = fabsf(N.z);
380
381         N /= (N.x + N.y + N.z);
382
383         /* basic idea is to think of this as a triangle, each corner representing
384          * one of the 3 faces of the cube. in the corners we have single textures,
385          * in between we blend between two textures, and in the middle we a blend
386          * between three textures.
387          *
388          * the Nxyz values are the barycentric coordinates in an equilateral
389          * triangle, which in case of blending, in the middle has a smaller
390          * equilateral triangle where 3 textures blend. this divides things into
391          * 7 zones, with an if() test for each zone */
392
393         float3 weight = make_float3(0.0f, 0.0f, 0.0f);
394         float blend = __int_as_float(node.w);
395         float limit = 0.5f*(1.0f + blend);
396
397         /* first test for corners with single texture */
398         if(N.x > limit*(N.x + N.y) && N.x > limit*(N.x + N.z)) {
399                 weight.x = 1.0f;
400         }
401         else if(N.y > limit*(N.x + N.y) && N.y > limit*(N.y + N.z)) {
402                 weight.y = 1.0f;
403         }
404         else if(N.z > limit*(N.x + N.z) && N.z > limit*(N.y + N.z)) {
405                 weight.z = 1.0f;
406         }
407         else if(blend > 0.0f) {
408                 /* in case of blending, test for mixes between two textures */
409                 if(N.z < (1.0f - limit)*(N.y + N.x)) {
410                         weight.x = N.x/(N.x + N.y);
411                         weight.x = saturate((weight.x - 0.5f*(1.0f - blend))/blend);
412                         weight.y = 1.0f - weight.x;
413                 }
414                 else if(N.x < (1.0f - limit)*(N.y + N.z)) {
415                         weight.y = N.y/(N.y + N.z);
416                         weight.y = saturate((weight.y - 0.5f*(1.0f - blend))/blend);
417                         weight.z = 1.0f - weight.y;
418                 }
419                 else if(N.y < (1.0f - limit)*(N.x + N.z)) {
420                         weight.x = N.x/(N.x + N.z);
421                         weight.x = saturate((weight.x - 0.5f*(1.0f - blend))/blend);
422                         weight.z = 1.0f - weight.x;
423                 }
424                 else {
425                         /* last case, we have a mix between three */
426                         weight.x = ((2.0f - limit)*N.x + (limit - 1.0f))/(2.0f*limit - 1.0f);
427                         weight.y = ((2.0f - limit)*N.y + (limit - 1.0f))/(2.0f*limit - 1.0f);
428                         weight.z = ((2.0f - limit)*N.z + (limit - 1.0f))/(2.0f*limit - 1.0f);
429                 }
430         }
431         else {
432                 /* Desperate mode, no valid choice anyway, fallback to one side.*/
433                 weight.x = 1.0f;
434         }
435
436         /* now fetch textures */
437         uint co_offset, out_offset, alpha_offset, srgb;
438         decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
439
440         float3 co = stack_load_float3(stack, co_offset);
441         uint id = node.y;
442
443         float4 f = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
444         uint use_alpha = stack_valid(alpha_offset);
445
446         if(weight.x > 0.0f)
447                 f += weight.x*svm_image_texture(kg, id, co.y, co.z, srgb, use_alpha);
448         if(weight.y > 0.0f)
449                 f += weight.y*svm_image_texture(kg, id, co.x, co.z, srgb, use_alpha);
450         if(weight.z > 0.0f)
451                 f += weight.z*svm_image_texture(kg, id, co.y, co.x, srgb, use_alpha);
452
453         if(stack_valid(out_offset))
454                 stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
455         if(stack_valid(alpha_offset))
456                 stack_store_float(stack, alpha_offset, f.w);
457 }
458
459 ccl_device void svm_node_tex_environment(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
460 {
461         uint id = node.y;
462         uint co_offset, out_offset, alpha_offset, srgb;
463         uint projection = node.w;
464
465         decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
466
467         float3 co = stack_load_float3(stack, co_offset);
468         float2 uv;
469
470         co = normalize(co);
471         
472         if(projection == 0)
473                 uv = direction_to_equirectangular(co);
474         else
475                 uv = direction_to_mirrorball(co);
476
477         uint use_alpha = stack_valid(alpha_offset);
478         float4 f = svm_image_texture(kg, id, uv.x, uv.y, srgb, use_alpha);
479
480         if(stack_valid(out_offset))
481                 stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
482         if(stack_valid(alpha_offset))
483                 stack_store_float(stack, alpha_offset, f.w);
484 }
485
486 CCL_NAMESPACE_END
487