Cycles: Implement proper texture derivatives evaluation for OSL
[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 class OSLRenderServices : public OSL::RendererServices
44 {
45 public:
46         OSLRenderServices();
47         ~OSLRenderServices();
48         
49         void thread_init(KernelGlobals *kernel_globals, OSL::TextureSystem *ts);
50
51         bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform, float time);
52         bool get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform, float time);
53         
54         bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from, float time);
55         bool get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring to, float time);
56         
57         bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform);
58         bool get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform);
59         
60         bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from);
61         bool get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from);
62
63         bool get_array_attribute(OSL::ShaderGlobals *sg, bool derivatives,
64                                  ustring object, TypeDesc type, ustring name,
65                                  int index, void *val);
66         bool get_attribute(OSL::ShaderGlobals *sg, bool derivatives, ustring object,
67                            TypeDesc type, ustring name, void *val);
68         bool get_attribute(ShaderData *sd, bool derivatives, ustring object_name,
69                            TypeDesc type, ustring name, void *val);
70
71         bool get_userdata(bool derivatives, ustring name, TypeDesc type,
72                           OSL::ShaderGlobals *sg, void *val);
73         bool has_userdata(ustring name, TypeDesc type, OSL::ShaderGlobals *sg);
74
75         int pointcloud_search(OSL::ShaderGlobals *sg, ustring filename, const OSL::Vec3 &center,
76                               float radius, int max_points, bool sort, size_t *out_indices,
77                               float *out_distances, int derivs_offset);
78
79         int pointcloud_get(OSL::ShaderGlobals *sg, ustring filename, size_t *indices, int count,
80                            ustring attr_name, TypeDesc attr_type, void *out_data);
81
82         bool pointcloud_write(OSL::ShaderGlobals *sg,
83                               ustring filename, const OSL::Vec3 &pos,
84                               int nattribs, const ustring *names,
85                               const TypeDesc *types,
86                               const void **data);
87
88         bool trace(TraceOpt &options, OSL::ShaderGlobals *sg,
89                    const OSL::Vec3 &P, const OSL::Vec3 &dPdx,
90                    const OSL::Vec3 &dPdy, const OSL::Vec3 &R,
91                    const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy);
92
93         bool getmessage(OSL::ShaderGlobals *sg, ustring source, ustring name,
94                         TypeDesc type, void *val, bool derivatives);
95
96 #if OSL_LIBRARY_VERSION_CODE < 10600
97         typedef TextureSystem::TextureHandle TextureHandle;
98         typedef TextureSystem::Perthread TexturePerthread;
99 #endif
100
101         bool texture(ustring filename,
102                      TextureSystem::TextureHandle *texture_handle,
103                      TexturePerthread *texture_thread_info,
104                      TextureOpt &options,
105                      OSL::ShaderGlobals *sg,
106                      float s, float t,
107                      float dsdx, float dtdx, float dsdy, float dtdy,
108                      int nchannels,
109                      float *result,
110                      float *dresultds,
111                      float *dresultdt);
112
113         bool texture3d(ustring filename,
114                        TextureHandle *texture_handle,
115                        TexturePerthread *texture_thread_info,
116                        TextureOpt &options,
117                        OSL::ShaderGlobals *sg,
118                        const OSL::Vec3 &P,
119                        const OSL::Vec3 &dPdx,
120                        const OSL::Vec3 &dPdy,
121                        const OSL::Vec3 &dPdz,
122                        int nchannels,
123                        float *result,
124                        float *dresultds,
125                        float *dresultdt,
126                        float *dresultdr);
127
128         bool environment(ustring filename, TextureOpt &options,
129                          OSL::ShaderGlobals *sg, const OSL::Vec3 &R,
130                          const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy,
131                          int nchannels, float *result);
132
133         bool get_texture_info(OSL::ShaderGlobals *sg, ustring filename, int subimage,
134                               ustring dataname, TypeDesc datatype, void *data);
135
136         static bool get_background_attribute(KernelGlobals *kg, ShaderData *sd, ustring name,
137                                              TypeDesc type, bool derivatives, void *val);
138         static bool get_object_standard_attribute(KernelGlobals *kg, ShaderData *sd, ustring name,
139                                                   TypeDesc type, bool derivatives, void *val);
140
141         static ustring u_distance;
142         static ustring u_index;
143         static ustring u_world;
144         static ustring u_camera;
145         static ustring u_screen;
146         static ustring u_raster;
147         static ustring u_ndc;
148         static ustring u_object_location;
149         static ustring u_object_index;
150         static ustring u_geom_dupli_generated;
151         static ustring u_geom_dupli_uv;
152         static ustring u_material_index;
153         static ustring u_object_random;
154         static ustring u_particle_index;
155         static ustring u_particle_age;
156         static ustring u_particle_lifetime;
157         static ustring u_particle_location;
158         static ustring u_particle_rotation;
159         static ustring u_particle_size;
160         static ustring u_particle_velocity;
161         static ustring u_particle_angular_velocity;
162         static ustring u_geom_numpolyvertices;
163         static ustring u_geom_trianglevertices;
164         static ustring u_geom_polyvertices;
165         static ustring u_geom_name;
166         static ustring u_is_smooth;
167         static ustring u_is_curve;
168         static ustring u_curve_thickness;
169         static ustring u_curve_tangent_normal;
170         static ustring u_path_ray_length;
171         static ustring u_path_ray_depth;
172         static ustring u_path_transparent_depth;
173         static ustring u_trace;
174         static ustring u_hit;
175         static ustring u_hitdist;
176         static ustring u_N;
177         static ustring u_Ng;
178         static ustring u_P;
179         static ustring u_I;
180         static ustring u_u;
181         static ustring u_v;
182         static ustring u_empty;
183
184         /* Code to make OSL versions transition smooth. */
185
186 #if OSL_LIBRARY_VERSION_CODE < 10600
187         bool texture(ustring filename,
188                      TextureOpt &options,
189                      OSL::ShaderGlobals *sg,
190                      float s, float t,
191                      float dsdx, float dtdx,
192                      float dsdy, float dtdy,
193                      float *result);
194
195         bool texture3d(ustring filename,
196                        TextureOpt &options,
197                        OSL::ShaderGlobals *sg,
198                        const OSL::Vec3 &P,
199                        const OSL::Vec3 &dPdx,
200                        const OSL::Vec3 &dPdy,
201                        const OSL::Vec3 &dPdz,
202                        float *result);
203
204         inline bool environment(ustring filename, TextureOpt &options,
205                                 OSL::ShaderGlobals *sg, const OSL::Vec3 &R,
206                                 const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy,
207                                 float *result)
208         {
209                 return environment(filename, options, sg, R, dRdx, dRdy,
210                                    options.nchannels, result);
211         }
212 #endif
213
214 private:
215         KernelGlobals *kernel_globals;
216         OSL::TextureSystem *osl_ts;
217 #ifdef WITH_PTEX
218         PtexCache *ptex_cache;
219 #endif
220 };
221
222 CCL_NAMESPACE_END
223
224 #endif /* __OSL_SERVICES_H__  */
225