Cycles:
[blender.git] / intern / cycles / kernel / svm / svm_texture.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 /* Voronoi Distances */
22
23 __device float voronoi_distance(NodeDistanceMetric distance_metric, float3 d, float e)
24 {
25         if(distance_metric == NODE_VORONOI_DISTANCE_SQUARED)
26                 return dot(d, d);
27         if(distance_metric == NODE_VORONOI_ACTUAL_DISTANCE)
28                 return len(d);
29         if(distance_metric == NODE_VORONOI_MANHATTAN)
30                 return fabsf(d.x) + fabsf(d.y) + fabsf(d.z);
31         if(distance_metric == NODE_VORONOI_CHEBYCHEV)
32                 return fmaxf(fabsf(d.x), fmaxf(fabsf(d.y), fabsf(d.z)));
33         if(distance_metric == NODE_VORONOI_MINKOVSKY_H)
34                 return sqrtf(fabsf(d.x)) + sqrtf(fabsf(d.y)) + sqrtf(fabsf(d.y));
35         if(distance_metric == NODE_VORONOI_MINKOVSKY_4)
36                 return sqrtf(sqrtf(dot(d*d, d*d)));
37         if(distance_metric == NODE_VORONOI_MINKOVSKY)
38                 return powf(powf(fabsf(d.x), e) + powf(fabsf(d.y), e) + powf(fabsf(d.z), e), 1.0f/e);
39         
40         return 0.0f;
41 }
42
43 /* Voronoi / Worley like */
44
45 __device_noinline float4 voronoi_Fn(float3 p, float e, int n1, int n2)
46 {
47         float da[4];
48         float3 pa[4];
49         NodeDistanceMetric distance_metric = NODE_VORONOI_DISTANCE_SQUARED;
50
51         /* returns distances in da and point coords in pa */
52         int xx, yy, zz, xi, yi, zi;
53
54         xi = (int)floorf(p.x);
55         yi = (int)floorf(p.y);
56         zi = (int)floorf(p.z);
57
58         da[0] = 1e10f;
59         da[1] = 1e10f;
60         da[2] = 1e10f;
61         da[3] = 1e10f;
62
63         pa[0] = make_float3(0.0f, 0.0f, 0.0f);
64         pa[1] = make_float3(0.0f, 0.0f, 0.0f);
65         pa[2] = make_float3(0.0f, 0.0f, 0.0f);
66         pa[3] = make_float3(0.0f, 0.0f, 0.0f);
67
68         for(xx = xi-1; xx <= xi+1; xx++) {
69                 for(yy = yi-1; yy <= yi+1; yy++) {
70                         for(zz = zi-1; zz <= zi+1; zz++) {
71                                 float3 ip = make_float3((float)xx, (float)yy, (float)zz);
72                                 float3 vp = cellnoise_color(ip);
73                                 float3 pd = p - (vp + ip);
74                                 float d = voronoi_distance(distance_metric, pd, e);
75
76                                 vp += ip;
77
78                                 if(d < da[0]) {
79                                         da[3] = da[2];
80                                         da[2] = da[1];
81                                         da[1] = da[0];
82                                         da[0] = d;
83
84                                         pa[3] = pa[2];
85                                         pa[2] = pa[1];
86                                         pa[1] = pa[0];
87                                         pa[0] = vp;
88                                 }
89                                 else if(d < da[1]) {
90                                         da[3] = da[2];
91                                         da[2] = da[1];
92                                         da[1] = d;
93
94                                         pa[3] = pa[2];
95                                         pa[2] = pa[1];
96                                         pa[1] = vp;
97                                 }
98                                 else if(d < da[2]) {
99                                         da[3] = da[2];
100                                         da[2] = d;
101
102                                         pa[3] = pa[2];
103                                         pa[2] = vp;
104                                 }
105                                 else if(d < da[3]) {
106                                         da[3] = d;
107                                         pa[3] = vp;
108                                 }
109                         }
110                 }
111         }
112
113         float4 result = make_float4(pa[n1].x, pa[n1].y, pa[n1].z, da[n1]);
114
115         if(n2 != -1)
116                 result = make_float4(pa[n2].x, pa[n2].y, pa[n2].z, da[n2]) - result;
117
118         return result;
119 }
120
121 __device float voronoi_F1(float3 p) { return voronoi_Fn(p, 0.0f, 0, -1).w; }
122 __device float voronoi_F2(float3 p) { return voronoi_Fn(p, 0.0f, 1, -1).w; }
123 __device float voronoi_F3(float3 p) { return voronoi_Fn(p, 0.0f, 2, -1).w; }
124 __device float voronoi_F4(float3 p) { return voronoi_Fn(p, 0.0f, 3, -1).w; }
125 __device float voronoi_F1F2(float3 p) { return voronoi_Fn(p, 0.0f, 0, 1).w; }
126
127 __device float voronoi_Cr(float3 p)
128 {
129         /* crackle type pattern, just a scale/clamp of F2-F1 */
130         float t = 10.0f*voronoi_F1F2(p);
131         return (t > 1.0f)? 1.0f: t;
132 }
133
134 __device float voronoi_F1S(float3 p) { return 2.0f*voronoi_F1(p) - 1.0f; }
135 __device float voronoi_F2S(float3 p) { return 2.0f*voronoi_F2(p) - 1.0f; }
136 __device float voronoi_F3S(float3 p) { return 2.0f*voronoi_F3(p) - 1.0f; }
137 __device float voronoi_F4S(float3 p) { return 2.0f*voronoi_F4(p) - 1.0f; }
138 __device float voronoi_F1F2S(float3 p) { return 2.0f*voronoi_F1F2(p) - 1.0f; }
139 __device float voronoi_CrS(float3 p) { return 2.0f*voronoi_Cr(p) - 1.0f; }
140
141 /* Noise Bases */
142
143 __device float noise_basis(float3 p, NodeNoiseBasis basis)
144 {
145         /* Only Perlin enabled for now, others break CUDA compile by making kernel
146          * too big, with compile using > 4GB, due to everything being inlined. */
147
148 #if 0
149         if(basis == NODE_NOISE_PERLIN)
150 #endif
151                 return noise(p);
152 #if 0
153         if(basis == NODE_NOISE_VORONOI_F1)
154                 return voronoi_F1S(p);
155         if(basis == NODE_NOISE_VORONOI_F2)
156                 return voronoi_F2S(p);
157         if(basis == NODE_NOISE_VORONOI_F3)
158                 return voronoi_F3S(p);
159         if(basis == NODE_NOISE_VORONOI_F4)
160                 return voronoi_F4S(p);
161         if(basis == NODE_NOISE_VORONOI_F2_F1)
162                 return voronoi_F1F2S(p);
163         if(basis == NODE_NOISE_VORONOI_CRACKLE)
164                 return voronoi_CrS(p);
165         if(basis == NODE_NOISE_CELL_NOISE)
166                 return cellnoise(p);
167         
168         return 0.0f;
169 #endif
170 }
171
172 /* Soft/Hard Noise */
173
174 __device float noise_basis_hard(float3 p, NodeNoiseBasis basis, int hard)
175 {
176         float t = noise_basis(p, basis);
177         return (hard)? fabsf(2.0f*t - 1.0f): t;
178 }
179
180 /* Waves */
181
182 __device float noise_wave(NodeWaveBasis wave, float a)
183 {
184         if(wave == NODE_WAVE_SINE) {
185                 return 0.5f + 0.5f * sinf(a);
186         }
187         else if(wave == NODE_WAVE_SAW) {
188                 float b = 2.0f*M_PI_F;
189                 int n = (int)(a / b);
190                 a -= n*b;
191                 if(a < 0.0f) a += b;
192
193                 return a / b;
194         }
195         else if(wave == NODE_WAVE_TRI) {
196                 float b = 2.0f*M_PI_F;
197                 float rmax = 1.0f;
198
199                 return rmax - 2.0f*fabsf(floorf((a*(1.0f/b))+0.5f) - (a*(1.0f/b)));
200         }
201
202         return 0.0f;
203 }
204
205 /* Turbulence */
206
207 __device_noinline float noise_turbulence(float3 p, NodeNoiseBasis basis, float octaves, int hard)
208 {
209         float fscale = 1.0f;
210         float amp = 1.0f;
211         float sum = 0.0f;
212         int i, n;
213
214         octaves = clamp(octaves, 0.0f, 16.0f);
215         n = (int)octaves;
216
217         for(i = 0; i <= n; i++) {
218                 float t = noise_basis(fscale*p, basis);
219
220                 if(hard)
221                         t = fabsf(2.0f*t - 1.0f);
222
223                 sum += t*amp;
224                 amp *= 0.5f;
225                 fscale *= 2.0f;
226         }
227
228         float rmd = octaves - floorf(octaves);
229
230         if(rmd != 0.0f) {
231                 float t = noise_basis(fscale*p, basis);
232
233                 if(hard)
234                         t = fabsf(2.0f*t - 1.0f);
235
236                 float sum2 = sum + t*amp;
237
238                 sum *= ((float)(1 << n)/(float)((1 << (n+1)) - 1));
239                 sum2 *= ((float)(1 << (n+1))/(float)((1 << (n+2)) - 1));
240
241                 return (1.0f - rmd)*sum + rmd*sum2;
242         }
243         else {
244                 sum *= ((float)(1 << n)/(float)((1 << (n+1)) - 1));
245                 return sum;
246         }
247 }
248
249 CCL_NAMESPACE_END
250