Cycles: svn merge -r41225:41232 ^/trunk/blender
[blender.git] / intern / cycles / kernel / svm / svm_magic.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 /* Magic */
22
23 __device_noinline float3 svm_magic(float3 p, int n, float turbulence)
24 {
25         float turb = turbulence/5.0f;
26
27         float x = sinf((p.x + p.y + p.z)*5.0f);
28         float y = cosf((-p.x + p.y - p.z)*5.0f);
29         float z = -cosf((-p.x - p.y + p.z)*5.0f);
30
31         if(n > 0) {
32                 x *= turb;
33                 y *= turb;
34                 z *= turb;
35                 y = -cosf(x-y+z);
36                 y *= turb;
37
38                 if(n > 1) {
39                         x= cosf(x-y-z);
40                         x *= turb;
41
42                         if(n > 2) {
43                                 z= sinf(-x-y-z);
44                                 z *= turb;
45
46                                 if(n > 3) {
47                                         x= -cosf(-x+y-z);
48                                         x *= turb;
49
50                                         if(n > 4) {
51                                                 y= -sinf(-x+y+z);
52                                                 y *= turb;
53
54                                                 if(n > 5) {
55                                                         y= -cosf(-x+y+z);
56                                                         y *= turb;
57
58                                                         if(n > 6) {
59                                                                 x= cosf(x+y+z);
60                                                                 x *= turb;
61
62                                                                 if(n > 7) {
63                                                                         z= sinf(x+y-z);
64                                                                         z *= turb;
65
66                                                                         if(n > 8) {
67                                                                                 x= -cosf(-x-y+z);
68                                                                                 x *= turb;
69
70                                                                                 if(n > 9) {
71                                                                                         y= -sinf(x-y+z);
72                                                                                         y *= turb;
73                                                                                 }
74                                                                         }
75                                                                 }
76                                                         }
77                                                 }
78                                         }
79                                 }
80                         }
81                 }
82         }
83
84         if(turb != 0.0f) {
85                 turb *= 2.0f;
86                 x /= turb;
87                 y /= turb;
88                 z /= turb;
89         }
90
91         return make_float3(0.5f - x, 0.5f - y, 0.5f - z);
92 }
93
94 __device void svm_node_tex_magic(ShaderData *sd, float *stack, uint4 node)
95 {
96         uint depth, turbulence_offset, co_offset, color_offset;
97
98         decode_node_uchar4(node.y, &depth, &turbulence_offset, &co_offset, &color_offset);
99
100         float3 co = stack_load_float3(stack, co_offset);
101         float turbulence = stack_load_float_default(stack, turbulence_offset, node.z);
102
103         float3 color = svm_magic(co, depth, turbulence);
104         stack_store_float3(stack, color_offset, color);
105 }
106
107 CCL_NAMESPACE_END
108