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