aa2befd6e989db66700c7aeae95bd3648e1602a0
[blender.git] / intern / cycles / kernel / osl / osl_services.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 #ifndef __OSL_SERVICES_H__
18 #define __OSL_SERVICES_H__
19
20 /* OSL Render Services
21  *
22  * Implementation of OSL render services, to retriever matrices, attributes,
23  * textures and point clouds. In principle this should only be accessing
24  * kernel data, but currently we also reach back into the Scene to retrieve
25  * attributes.
26  */
27
28 #include <OSL/oslexec.h>
29 #include <OSL/oslclosure.h>
30
31 #ifdef WITH_PTEX
32 class PtexCache;
33 #endif
34
35 CCL_NAMESPACE_BEGIN
36
37 class Object;
38 class Scene;
39 class Shader;
40 struct ShaderData;
41 struct float3;
42 struct KernelGlobals;
43
44 class OSLRenderServices : public OSL::RendererServices
45 {
46 public:
47         OSLRenderServices();
48         ~OSLRenderServices();
49         
50         void thread_init(KernelGlobals *kernel_globals, OSL::TextureSystem *ts);
51
52         bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform, float time);
53         bool get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform, float time);
54         
55         bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from, float time);
56         bool get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring to, float time);
57         
58         bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform);
59         bool get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform);
60         
61         bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from);
62         bool get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from);
63
64         bool get_array_attribute(OSL::ShaderGlobals *sg, bool derivatives,
65                                  ustring object, TypeDesc type, ustring name,
66                                  int index, void *val);
67         bool get_attribute(OSL::ShaderGlobals *sg, bool derivatives, ustring object,
68                            TypeDesc type, ustring name, void *val);
69         bool get_attribute(ShaderData *sd, bool derivatives, ustring object_name,
70                            TypeDesc type, ustring name, void *val);
71
72         bool get_userdata(bool derivatives, ustring name, TypeDesc type,
73                           OSL::ShaderGlobals *sg, void *val);
74         bool has_userdata(ustring name, TypeDesc type, OSL::ShaderGlobals *sg);
75
76         int pointcloud_search(OSL::ShaderGlobals *sg, ustring filename, const OSL::Vec3 &center,
77                               float radius, int max_points, bool sort, size_t *out_indices,
78                               float *out_distances, int derivs_offset);
79
80         int pointcloud_get(OSL::ShaderGlobals *sg, ustring filename, size_t *indices, int count,
81                            ustring attr_name, TypeDesc attr_type, void *out_data);
82
83         bool pointcloud_write(OSL::ShaderGlobals *sg,
84                               ustring filename, const OSL::Vec3 &pos,
85                               int nattribs, const ustring *names,
86                               const TypeDesc *types,
87                               const void **data);
88
89         bool trace(TraceOpt &options, OSL::ShaderGlobals *sg,
90                    const OSL::Vec3 &P, const OSL::Vec3 &dPdx,
91                    const OSL::Vec3 &dPdy, const OSL::Vec3 &R,
92                    const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy);
93
94         bool getmessage(OSL::ShaderGlobals *sg, ustring source, ustring name,
95                         TypeDesc type, void *val, bool derivatives);
96
97         bool texture(ustring filename, TextureOpt &options,
98                      OSL::ShaderGlobals *sg,
99                      float s, float t, float dsdx, float dtdx,
100                      float dsdy, float dtdy, int nchannels, float *result);
101
102         bool texture3d(ustring filename, TextureOpt &options,
103                        OSL::ShaderGlobals *sg, const OSL::Vec3 &P,
104                        const OSL::Vec3 &dPdx, const OSL::Vec3 &dPdy,
105                        const OSL::Vec3 &dPdz, int nchannels, float *result);
106
107 #if OSL_LIBRARY_VERSION_CODE >= 10600
108         bool texture(ustring filename,
109                      TextureHandle * /*texture_handle*/,
110                      TexturePerthread * /*texture_thread_info*/,
111                      TextureOpt &options,
112                      OSL::ShaderGlobals *sg,
113                      float s, float t,
114                      float dsdx, float dtdx, float dsdy, float dtdy,
115                      int nchannels,
116                      float *result,
117                      float * /*dresultds*/,
118                      float * /*dresultdt*/)
119         {
120                 /* TODO(sergey): Support derivatives. */
121                 return texture(filename,
122                                options,
123                                sg,
124                                s, t,
125                                dsdx, dtdx, dsdy, dtdy,
126                                nchannels,
127                                result);
128         }
129
130         bool texture3d(ustring filename,
131                        TextureHandle * /*texture_handle*/,
132                        TexturePerthread * /*texture_thread_info*/,
133                        TextureOpt &options,
134                        OSL::ShaderGlobals *sg,
135                        const OSL::Vec3 &P,
136                        const OSL::Vec3 &dPdx,
137                        const OSL::Vec3 &dPdy,
138                        const OSL::Vec3 &dPdz,
139                        int nchannels,
140                        float *result,
141                        float * /*dresultds*/,
142                        float * /*dresultdt*/,
143                        float * /*dresultdr*/)
144         {
145                 /* TODO(sergey): Support derivatives. */
146                 return texture3d(filename,
147                                  options,
148                                  sg,
149                                  P,
150                                  dPdx, dPdy, dPdz,
151                                  nchannels,
152                                  result);
153         }
154 #endif
155
156         bool environment(ustring filename, TextureOpt &options,
157                          OSL::ShaderGlobals *sg, const OSL::Vec3 &R,
158                          const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy,
159                          int nchannels, float *result);
160
161         bool get_texture_info(OSL::ShaderGlobals *sg, ustring filename, int subimage,
162                               ustring dataname, TypeDesc datatype, void *data);
163
164         static bool get_background_attribute(KernelGlobals *kg, ShaderData *sd, ustring name,
165                                              TypeDesc type, bool derivatives, void *val);
166         static bool get_object_standard_attribute(KernelGlobals *kg, ShaderData *sd, ustring name,
167                                                   TypeDesc type, bool derivatives, void *val);
168
169         static ustring u_distance;
170         static ustring u_index;
171         static ustring u_world;
172         static ustring u_camera;
173         static ustring u_screen;
174         static ustring u_raster;
175         static ustring u_ndc;
176         static ustring u_object_location;
177         static ustring u_object_index;
178         static ustring u_geom_dupli_generated;
179         static ustring u_geom_dupli_uv;
180         static ustring u_material_index;
181         static ustring u_object_random;
182         static ustring u_particle_index;
183         static ustring u_particle_age;
184         static ustring u_particle_lifetime;
185         static ustring u_particle_location;
186         static ustring u_particle_rotation;
187         static ustring u_particle_size;
188         static ustring u_particle_velocity;
189         static ustring u_particle_angular_velocity;
190         static ustring u_geom_numpolyvertices;
191         static ustring u_geom_trianglevertices;
192         static ustring u_geom_polyvertices;
193         static ustring u_geom_name;
194         static ustring u_is_smooth;
195         static ustring u_is_curve;
196         static ustring u_curve_thickness;
197         static ustring u_curve_tangent_normal;
198         static ustring u_path_ray_length;
199         static ustring u_path_ray_depth;
200         static ustring u_path_transparent_depth;
201         static ustring u_trace;
202         static ustring u_hit;
203         static ustring u_hitdist;
204         static ustring u_N;
205         static ustring u_Ng;
206         static ustring u_P;
207         static ustring u_I;
208         static ustring u_u;
209         static ustring u_v;
210         static ustring u_empty;
211
212         /* Code to make OSL versions transition smooth. */
213
214 #if OSL_LIBRARY_VERSION_CODE < 10600
215         inline bool texture(ustring filename, TextureOpt &options,
216                             OSL::ShaderGlobals *sg,
217                             float s, float t, float dsdx, float dtdx,
218                             float dsdy, float dtdy, float *result)
219         {
220                 return texture(filename, options, sg, s, t, dsdx, dtdx, dsdy, dtdy,
221                                options.nchannels, result);
222         }
223
224         inline bool texture3d(ustring filename, TextureOpt &options,
225                               OSL::ShaderGlobals *sg, const OSL::Vec3 &P,
226                               const OSL::Vec3 &dPdx, const OSL::Vec3 &dPdy,
227                               const OSL::Vec3 &dPdz, float *result)
228         {
229                 return texture3d(filename, options, sg, P, dPdx, dPdy, dPdz,
230                                  options.nchannels, result);
231         }
232
233         inline bool environment(ustring filename, TextureOpt &options,
234                                 OSL::ShaderGlobals *sg, const OSL::Vec3 &R,
235                                 const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy,
236                                 float *result)
237         {
238                 return environment(filename, options, sg, R, dRdx, dRdy,
239                                    options.nchannels, result);
240         }
241 #endif
242
243 private:
244         KernelGlobals *kernel_globals;
245         OSL::TextureSystem *osl_ts;
246 #ifdef WITH_PTEX
247         PtexCache *ptex_cache;
248 #endif
249 };
250
251 CCL_NAMESPACE_END
252
253 #endif /* __OSL_SERVICES_H__  */
254