1734eaad2615ea8746d427e7c16906078c0518e2
[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                 return texture(filename,
121                                options,
122                                sg,
123                                s, t,
124                                dsdx, dtdx, dsdy, dtdy,
125                                nchannels,
126                                result);
127         }
128
129         bool texture3d(ustring filename,
130                        TextureHandle * /*texture_handle*/,
131                        TexturePerthread * /*texture_thread_info*/,
132                        TextureOpt &options,
133                        OSL::ShaderGlobals *sg,
134                        const OSL::Vec3 &P,
135                        const OSL::Vec3 &dPdx,
136                        const OSL::Vec3 &dPdy,
137                        const OSL::Vec3 &dPdz,
138                        int nchannels,
139                        float *result,
140                        float * /*dresultds*/,
141                        float * /*dresultdt*/,
142                        float * /*dresultdr*/)
143         {
144                 return texture3d(filename,
145                                  options,
146                                  sg,
147                                  P,
148                                  dPdx, dPdy, dPdz,
149                                  nchannels,
150                                  result);
151         }
152 #endif
153
154         bool environment(ustring filename, TextureOpt &options,
155                          OSL::ShaderGlobals *sg, const OSL::Vec3 &R,
156                          const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy,
157                          int nchannels, float *result);
158
159         bool get_texture_info(OSL::ShaderGlobals *sg, ustring filename, int subimage,
160                               ustring dataname, TypeDesc datatype, void *data);
161
162         static bool get_background_attribute(KernelGlobals *kg, ShaderData *sd, ustring name,
163                                              TypeDesc type, bool derivatives, void *val);
164         static bool get_object_standard_attribute(KernelGlobals *kg, ShaderData *sd, ustring name,
165                                                   TypeDesc type, bool derivatives, void *val);
166
167         static ustring u_distance;
168         static ustring u_index;
169         static ustring u_world;
170         static ustring u_camera;
171         static ustring u_screen;
172         static ustring u_raster;
173         static ustring u_ndc;
174         static ustring u_object_location;
175         static ustring u_object_index;
176         static ustring u_geom_dupli_generated;
177         static ustring u_geom_dupli_uv;
178         static ustring u_material_index;
179         static ustring u_object_random;
180         static ustring u_particle_index;
181         static ustring u_particle_age;
182         static ustring u_particle_lifetime;
183         static ustring u_particle_location;
184         static ustring u_particle_rotation;
185         static ustring u_particle_size;
186         static ustring u_particle_velocity;
187         static ustring u_particle_angular_velocity;
188         static ustring u_geom_numpolyvertices;
189         static ustring u_geom_trianglevertices;
190         static ustring u_geom_polyvertices;
191         static ustring u_geom_name;
192         static ustring u_is_smooth;
193         static ustring u_is_curve;
194         static ustring u_curve_thickness;
195         static ustring u_curve_tangent_normal;
196         static ustring u_path_ray_length;
197         static ustring u_path_ray_depth;
198         static ustring u_path_transparent_depth;
199         static ustring u_trace;
200         static ustring u_hit;
201         static ustring u_hitdist;
202         static ustring u_N;
203         static ustring u_Ng;
204         static ustring u_P;
205         static ustring u_I;
206         static ustring u_u;
207         static ustring u_v;
208         static ustring u_empty;
209
210         /* Code to make OSL versions transition smooth. */
211
212 #if OSL_LIBRARY_VERSION_CODE < 10600
213         inline bool texture(ustring filename, TextureOpt &options,
214                             OSL::ShaderGlobals *sg,
215                             float s, float t, float dsdx, float dtdx,
216                             float dsdy, float dtdy, float *result)
217         {
218                 return texture(filename, options, sg, s, t, dsdx, dtdx, dsdy, dtdy,
219                                options.nchannels, result);
220         }
221
222         inline bool texture3d(ustring filename, TextureOpt &options,
223                               OSL::ShaderGlobals *sg, const OSL::Vec3 &P,
224                               const OSL::Vec3 &dPdx, const OSL::Vec3 &dPdy,
225                               const OSL::Vec3 &dPdz, float *result)
226         {
227                 return texture3d(filename, options, sg, P, dPdx, dPdy, dPdz,
228                                  options.nchannels, result);
229         }
230
231         inline bool environment(ustring filename, TextureOpt &options,
232                                 OSL::ShaderGlobals *sg, const OSL::Vec3 &R,
233                                 const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy,
234                                 float *result)
235         {
236                 return environment(filename, options, sg, R, dRdx, dRdy,
237                                    options.nchannels, result);
238         }
239 #endif
240
241 private:
242         KernelGlobals *kernel_globals;
243         OSL::TextureSystem *osl_ts;
244 #ifdef WITH_PTEX
245         PtexCache *ptex_cache;
246 #endif
247 };
248
249 CCL_NAMESPACE_END
250
251 #endif /* __OSL_SERVICES_H__  */
252