Hide object center and other info when drawing sets
[blender.git] / intern / cycles / kernel / kernel_image_opencl.h
1 /*
2  * Copyright 2016 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
18 /* For OpenCL all images are packed in a single array, and we do manual lookup
19  * and interpolation. */
20
21 ccl_device_inline float4 svm_image_texture_read(KernelGlobals *kg, int id, int offset)
22 {
23         const texture_type = kernel_tex_type(id);
24         /* Float4 */
25         if(texture_type == IMAGE_DATA_TYPE_FLOAT4) {
26                 return kernel_tex_fetch(__tex_image_float4_packed, offset);
27         }
28         /* Byte4 */
29         else if(texture_type == IMAGE_DATA_TYPE_BYTE4) {
30                 uchar4 r = kernel_tex_fetch(__tex_image_byte4_packed, offset);
31                 float f = 1.0f/255.0f;
32                 return make_float4(r.x*f, r.y*f, r.z*f, r.w*f);
33         }
34         /* Float */
35         else if(texture_type == IMAGE_DATA_TYPE_FLOAT) {
36                 float f = kernel_tex_fetch(__tex_image_float_packed, offset);
37                 return make_float4(f, f, f, 1.0f);
38         }
39         /* Byte */
40         else {
41                 uchar r = kernel_tex_fetch(__tex_image_byte_packed, offset);
42                 float f = r * (1.0f/255.0f);
43                 return make_float4(f, f, f, 1.0f);
44         }
45 }
46
47 ccl_device_inline int svm_image_texture_wrap_periodic(int x, int width)
48 {
49         x %= width;
50         if(x < 0)
51                 x += width;
52         return x;
53 }
54
55 ccl_device_inline int svm_image_texture_wrap_clamp(int x, int width)
56 {
57         return clamp(x, 0, width-1);
58 }
59
60 ccl_device_inline float svm_image_texture_frac(float x, int *ix)
61 {
62         int i = float_to_int(x) - ((x < 0.0f)? 1: 0);
63         *ix = i;
64         return x - (float)i;
65 }
66
67 ccl_device float4 kernel_tex_image_interp(KernelGlobals *kg, int id, float x, float y)
68 {
69         uint4 info = kernel_tex_fetch(__tex_image_packed_info, id*2);
70         uint width = info.x;
71         uint height = info.y;
72         uint offset = info.z;
73
74         /* Image Options */
75         uint interpolation = (info.w & (1 << 0)) ? INTERPOLATION_CLOSEST : INTERPOLATION_LINEAR;
76         uint extension;
77         if(info.w & (1 << 1))
78                 extension = EXTENSION_REPEAT;
79         else if(info.w & (1 << 2))
80                 extension = EXTENSION_EXTEND;
81         else
82                 extension = EXTENSION_CLIP;
83
84         float4 r;
85         int ix, iy, nix, niy;
86         if(interpolation == INTERPOLATION_CLOSEST) {
87                 svm_image_texture_frac(x*width, &ix);
88                 svm_image_texture_frac(y*height, &iy);
89
90                 if(extension == EXTENSION_REPEAT) {
91                         ix = svm_image_texture_wrap_periodic(ix, width);
92                         iy = svm_image_texture_wrap_periodic(iy, height);
93                 }
94                 else {
95                         if(extension == EXTENSION_CLIP) {
96                                 if(x < 0.0f || y < 0.0f || x > 1.0f || y > 1.0f) {
97                                         return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
98                                 }
99                         }
100                         /* Fall through. */
101                         /* EXTENSION_EXTEND */
102                         ix = svm_image_texture_wrap_clamp(ix, width);
103                         iy = svm_image_texture_wrap_clamp(iy, height);
104                 }
105
106                 r = svm_image_texture_read(kg, id, offset + ix + iy*width);
107         }
108         else { /* INTERPOLATION_LINEAR */
109                 float tx = svm_image_texture_frac(x*width - 0.5f, &ix);
110                 float ty = svm_image_texture_frac(y*height - 0.5f, &iy);
111
112                 if(extension == EXTENSION_REPEAT) {
113                         ix = svm_image_texture_wrap_periodic(ix, width);
114                         iy = svm_image_texture_wrap_periodic(iy, height);
115
116                         nix = svm_image_texture_wrap_periodic(ix+1, width);
117                         niy = svm_image_texture_wrap_periodic(iy+1, height);
118                 }
119                 else {
120                         if(extension == EXTENSION_CLIP) {
121                                 if(x < 0.0f || y < 0.0f || x > 1.0f || y > 1.0f) {
122                                         return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
123                                 }
124                         }
125                         nix = svm_image_texture_wrap_clamp(ix+1, width);
126                         niy = svm_image_texture_wrap_clamp(iy+1, height);
127                         ix = svm_image_texture_wrap_clamp(ix, width);
128                         iy = svm_image_texture_wrap_clamp(iy, height);
129                 }
130
131                 r = (1.0f - ty)*(1.0f - tx)*svm_image_texture_read(kg, id, offset + ix + iy*width);
132                 r += (1.0f - ty)*tx*svm_image_texture_read(kg, id, offset + nix + iy*width);
133                 r += ty*(1.0f - tx)*svm_image_texture_read(kg, id, offset + ix + niy*width);
134                 r += ty*tx*svm_image_texture_read(kg, id, offset + nix + niy*width);
135         }
136
137         return r;
138 }
139
140
141 ccl_device float4 kernel_tex_image_interp_3d(KernelGlobals *kg, int id, float x, float y, float z)
142 {
143         uint4 info = kernel_tex_fetch(__tex_image_packed_info, id*2);
144         uint width = info.x;
145         uint height = info.y;
146         uint offset = info.z;
147         uint depth = kernel_tex_fetch(__tex_image_packed_info, id*2+1).x;
148
149         /* Image Options */
150         uint interpolation = (info.w & (1 << 0)) ? INTERPOLATION_CLOSEST : INTERPOLATION_LINEAR;
151         uint extension;
152         if(info.w & (1 << 1))
153                 extension = EXTENSION_REPEAT;
154         else if(info.w & (1 << 2))
155                 extension = EXTENSION_EXTEND;
156         else
157                 extension = EXTENSION_CLIP;
158
159         float4 r;
160         int ix, iy, iz, nix, niy, niz;
161         if(interpolation == INTERPOLATION_CLOSEST) {
162                 svm_image_texture_frac(x*width, &ix);
163                 svm_image_texture_frac(y*height, &iy);
164                 svm_image_texture_frac(z*depth, &iz);
165
166                 if(extension == EXTENSION_REPEAT) {
167                         ix = svm_image_texture_wrap_periodic(ix, width);
168                         iy = svm_image_texture_wrap_periodic(iy, height);
169                         iz = svm_image_texture_wrap_periodic(iz, depth);
170                 }
171                 else {
172                         if(extension == EXTENSION_CLIP) {
173                                 if(x < 0.0f || y < 0.0f || z < 0.0f ||
174                                    x > 1.0f || y > 1.0f || z > 1.0f)
175                                  {
176                                         return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
177                                 }
178                         }
179                         /* Fall through. */
180                         /* EXTENSION_EXTEND */
181                         ix = svm_image_texture_wrap_clamp(ix, width);
182                         iy = svm_image_texture_wrap_clamp(iy, height);
183                         iz = svm_image_texture_wrap_clamp(iz, depth);
184                 }
185                 r = svm_image_texture_read(kg, id, offset + ix + iy*width + iz*width*height);
186         }
187         else { /* INTERPOLATION_LINEAR */
188                 float tx = svm_image_texture_frac(x*(float)width - 0.5f, &ix);
189                 float ty = svm_image_texture_frac(y*(float)height - 0.5f, &iy);
190                 float tz = svm_image_texture_frac(z*(float)depth - 0.5f, &iz);
191
192                 if(extension == EXTENSION_REPEAT) {
193                         ix = svm_image_texture_wrap_periodic(ix, width);
194                         iy = svm_image_texture_wrap_periodic(iy, height);
195                         iz = svm_image_texture_wrap_periodic(iz, depth);
196
197                         nix = svm_image_texture_wrap_periodic(ix+1, width);
198                         niy = svm_image_texture_wrap_periodic(iy+1, height);
199                         niz = svm_image_texture_wrap_periodic(iz+1, depth);
200                 }
201                 else {
202                         if(extension == EXTENSION_CLIP)
203                                 if(x < 0.0f || y < 0.0f || z < 0.0f ||
204                                    x > 1.0f || y > 1.0f || z > 1.0f)
205                                 {
206                                         return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
207                                 }
208                         /* Fall through. */
209                         /*  EXTENSION_EXTEND */
210                         nix = svm_image_texture_wrap_clamp(ix+1, width);
211                         niy = svm_image_texture_wrap_clamp(iy+1, height);
212                         niz = svm_image_texture_wrap_clamp(iz+1, depth);
213
214                         ix = svm_image_texture_wrap_clamp(ix, width);
215                         iy = svm_image_texture_wrap_clamp(iy, height);
216                         iz = svm_image_texture_wrap_clamp(iz, depth);
217                 }
218
219                 r  = (1.0f - tz)*(1.0f - ty)*(1.0f - tx)*svm_image_texture_read(kg, id, offset + ix + iy*width + iz*width*height);
220                 r += (1.0f - tz)*(1.0f - ty)*tx*svm_image_texture_read(kg, id, offset + nix + iy*width + iz*width*height);
221                 r += (1.0f - tz)*ty*(1.0f - tx)*svm_image_texture_read(kg, id, offset + ix + niy*width + iz*width*height);
222                 r += (1.0f - tz)*ty*tx*svm_image_texture_read(kg, id, offset + nix + niy*width + iz*width*height);
223
224                 r += tz*(1.0f - ty)*(1.0f - tx)*svm_image_texture_read(kg, id, offset + ix + iy*width + niz*width*height);
225                 r += tz*(1.0f - ty)*tx*svm_image_texture_read(kg, id, offset + nix + iy*width + niz*width*height);
226                 r += tz*ty*(1.0f - tx)*svm_image_texture_read(kg, id, offset + ix + niy*width + niz*width*height);
227                 r += tz*ty*tx*svm_image_texture_read(kg, id, offset + nix + niy*width + niz*width*height);
228
229         }
230
231         return r;
232 }