Blender  V2.93
osl_services.h
Go to the documentation of this file.
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/oslclosure.h>
29 #include <OSL/oslexec.h>
30 #include <OSL/rendererservices.h>
31 
32 #ifdef WITH_PTEX
33 class PtexCache;
34 #endif
35 
37 
38 class Object;
39 class Scene;
40 class Shader;
41 struct ShaderData;
42 struct float3;
43 struct KernelGlobals;
44 
45 /* OSL Texture Handle
46  *
47  * OSL texture lookups are string based. If those strings are known at compile
48  * time, the OSL compiler can cache a texture handle to use instead of a string.
49  *
50  * By default it uses TextureSystem::TextureHandle. But since we want to support
51  * different kinds of textures and color space conversions, this is our own handle
52  * with additional data.
53  *
54  * These are stored in a concurrent hash map, because OSL can compile multiple
55  * shaders in parallel. */
56 
57 struct OSLTextureHandle : public OIIO::RefCnt {
58  enum Type { OIIO, SVM, IES, BEVEL, AO };
59 
62  {
63  }
64 
66  int svm_slot;
67  OSL::TextureSystem::TextureHandle *oiio_handle;
68  ColorSpaceProcessor *processor;
69 };
70 
71 typedef OIIO::intrusive_ptr<OSLTextureHandle> OSLTextureHandleRef;
72 typedef OIIO::unordered_map_concurrent<ustring, OSLTextureHandleRef, ustringHash>
74 
75 /* OSL Render Services
76  *
77  * Interface for OSL to access attributes, textures and other scene data. */
78 
79 class OSLRenderServices : public OSL::RendererServices {
80  public:
81  OSLRenderServices(OSL::TextureSystem *texture_system);
83 
84  bool get_matrix(OSL::ShaderGlobals *sg,
85  OSL::Matrix44 &result,
86  OSL::TransformationPtr xform,
87  float time) override;
88  bool get_inverse_matrix(OSL::ShaderGlobals *sg,
89  OSL::Matrix44 &result,
90  OSL::TransformationPtr xform,
91  float time) override;
92 
93  bool get_matrix(OSL::ShaderGlobals *sg,
94  OSL::Matrix44 &result,
95  ustring from,
96  float time) override;
97  bool get_inverse_matrix(OSL::ShaderGlobals *sg,
98  OSL::Matrix44 &result,
99  ustring to,
100  float time) override;
101 
102  bool get_matrix(OSL::ShaderGlobals *sg,
103  OSL::Matrix44 &result,
104  OSL::TransformationPtr xform) override;
105  bool get_inverse_matrix(OSL::ShaderGlobals *sg,
106  OSL::Matrix44 &result,
107  OSL::TransformationPtr xform) override;
108 
109  bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from) override;
110  bool get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from) override;
111 
112  bool get_array_attribute(OSL::ShaderGlobals *sg,
113  bool derivatives,
114  ustring object,
115  TypeDesc type,
116  ustring name,
117  int index,
118  void *val) override;
119  bool get_attribute(OSL::ShaderGlobals *sg,
120  bool derivatives,
121  ustring object,
122  TypeDesc type,
123  ustring name,
124  void *val) override;
125  bool get_attribute(ShaderData *sd,
126  bool derivatives,
127  ustring object_name,
128  TypeDesc type,
129  ustring name,
130  void *val);
131 
132  bool get_userdata(
133  bool derivatives, ustring name, TypeDesc type, OSL::ShaderGlobals *sg, void *val) override;
134 
135  int pointcloud_search(OSL::ShaderGlobals *sg,
136  ustring filename,
137  const OSL::Vec3 &center,
138  float radius,
139  int max_points,
140  bool sort,
141  size_t *out_indices,
142  float *out_distances,
143  int derivs_offset) override;
144 
145  int pointcloud_get(OSL::ShaderGlobals *sg,
146  ustring filename,
147  size_t *indices,
148  int count,
149  ustring attr_name,
150  TypeDesc attr_type,
151  void *out_data) override;
152 
153  bool pointcloud_write(OSL::ShaderGlobals *sg,
154  ustring filename,
155  const OSL::Vec3 &pos,
156  int nattribs,
157  const ustring *names,
158  const TypeDesc *types,
159  const void **data) override;
160 
161  bool trace(TraceOpt &options,
162  OSL::ShaderGlobals *sg,
163  const OSL::Vec3 &P,
164  const OSL::Vec3 &dPdx,
165  const OSL::Vec3 &dPdy,
166  const OSL::Vec3 &R,
167  const OSL::Vec3 &dRdx,
168  const OSL::Vec3 &dRdy) override;
169 
170  bool getmessage(OSL::ShaderGlobals *sg,
171  ustring source,
172  ustring name,
173  TypeDesc type,
174  void *val,
175  bool derivatives) override;
176 
177 #if OSL_LIBRARY_VERSION_CODE >= 11100
178  TextureSystem::TextureHandle *get_texture_handle(ustring filename,
179  OSL::ShadingContext *context) override;
180 #else
181  TextureSystem::TextureHandle *get_texture_handle(ustring filename) override;
182 #endif
183 
184  bool good(TextureSystem::TextureHandle *texture_handle) override;
185 
186  bool texture(ustring filename,
187  TextureSystem::TextureHandle *texture_handle,
188  TexturePerthread *texture_thread_info,
189  TextureOpt &options,
190  OSL::ShaderGlobals *sg,
191  float s,
192  float t,
193  float dsdx,
194  float dtdx,
195  float dsdy,
196  float dtdy,
197  int nchannels,
198  float *result,
199  float *dresultds,
200  float *dresultdt,
201  ustring *errormessage) override;
202 
203  bool texture3d(ustring filename,
204  TextureHandle *texture_handle,
205  TexturePerthread *texture_thread_info,
206  TextureOpt &options,
207  OSL::ShaderGlobals *sg,
208  const OSL::Vec3 &P,
209  const OSL::Vec3 &dPdx,
210  const OSL::Vec3 &dPdy,
211  const OSL::Vec3 &dPdz,
212  int nchannels,
213  float *result,
214  float *dresultds,
215  float *dresultdt,
216  float *dresultdr,
217  ustring *errormessage) override;
218 
219  bool environment(ustring filename,
220  TextureHandle *texture_handle,
221  TexturePerthread *texture_thread_info,
222  TextureOpt &options,
223  OSL::ShaderGlobals *sg,
224  const OSL::Vec3 &R,
225  const OSL::Vec3 &dRdx,
226  const OSL::Vec3 &dRdy,
227  int nchannels,
228  float *result,
229  float *dresultds,
230  float *dresultdt,
231  ustring *errormessage) override;
232 
233 #if OSL_LIBRARY_VERSION_CODE >= 11100
234  bool get_texture_info(ustring filename,
235  TextureHandle *texture_handle,
236  TexturePerthread *texture_thread_info,
237  OSL::ShadingContext *shading_context,
238  int subimage,
239  ustring dataname,
240  TypeDesc datatype,
241  void *data,
242  ustring *errormessage) override;
243 #else
244  bool get_texture_info(OSL::ShaderGlobals *sg,
245  ustring filename,
246  TextureHandle *texture_handle,
247  int subimage,
248  ustring dataname,
249  TypeDesc datatype,
250  void *data) override;
251 #endif
252 
253  static bool get_background_attribute(
254  KernelGlobals *kg, ShaderData *sd, ustring name, TypeDesc type, bool derivatives, void *val);
255  static bool get_object_standard_attribute(
256  KernelGlobals *kg, ShaderData *sd, ustring name, TypeDesc type, bool derivatives, void *val);
257 
258  static ustring u_distance;
259  static ustring u_index;
260  static ustring u_world;
261  static ustring u_camera;
262  static ustring u_screen;
263  static ustring u_raster;
264  static ustring u_ndc;
265  static ustring u_object_location;
266  static ustring u_object_color;
267  static ustring u_object_index;
268  static ustring u_geom_dupli_generated;
269  static ustring u_geom_dupli_uv;
270  static ustring u_material_index;
271  static ustring u_object_random;
272  static ustring u_particle_index;
273  static ustring u_particle_random;
274  static ustring u_particle_age;
275  static ustring u_particle_lifetime;
276  static ustring u_particle_location;
277  static ustring u_particle_rotation;
278  static ustring u_particle_size;
279  static ustring u_particle_velocity;
281  static ustring u_geom_numpolyvertices;
282  static ustring u_geom_trianglevertices;
283  static ustring u_geom_polyvertices;
284  static ustring u_geom_name;
285  static ustring u_geom_undisplaced;
286  static ustring u_is_smooth;
287  static ustring u_is_curve;
288  static ustring u_curve_thickness;
289  static ustring u_curve_tangent_normal;
290  static ustring u_curve_random;
291  static ustring u_path_ray_length;
292  static ustring u_path_ray_depth;
293  static ustring u_path_diffuse_depth;
294  static ustring u_path_glossy_depth;
295  static ustring u_path_transparent_depth;
297  static ustring u_trace;
298  static ustring u_hit;
299  static ustring u_hitdist;
300  static ustring u_N;
301  static ustring u_Ng;
302  static ustring u_P;
303  static ustring u_I;
304  static ustring u_u;
305  static ustring u_v;
306  static ustring u_empty;
307  static ustring u_at_bevel;
308  static ustring u_at_ao;
309 
310  /* Texture system and texture handle map are part of the services instead of
311  * globals to be shared between different render sessions. This saves memory,
312  * and is required because texture handles are cached as part of the shared
313  * shading system. */
314  OSL::TextureSystem *texture_system;
316 };
317 
319 
320 #endif /* __OSL_SERVICES_H__ */
NSNotificationCenter * center
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
void sort(btMatrix3x3 &U, btVector3 &sigma, btMatrix3x3 &V, int t)
Helper function of 3X3 SVD for sorting singular values.
static ustring u_path_transmission_depth
Definition: osl_services.h:296
static ustring u_particle_location
Definition: osl_services.h:276
bool pointcloud_write(OSL::ShaderGlobals *sg, ustring filename, const OSL::Vec3 &pos, int nattribs, const ustring *names, const TypeDesc *types, const void **data) override
static ustring u_u
Definition: osl_services.h:304
bool get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform, float time) override
static ustring u_raster
Definition: osl_services.h:263
static ustring u_empty
Definition: osl_services.h:306
static ustring u_curve_thickness
Definition: osl_services.h:288
static ustring u_ndc
Definition: osl_services.h:264
static ustring u_particle_index
Definition: osl_services.h:272
static ustring u_curve_tangent_normal
Definition: osl_services.h:289
static ustring u_path_diffuse_depth
Definition: osl_services.h:293
static bool get_object_standard_attribute(KernelGlobals *kg, ShaderData *sd, ustring name, TypeDesc type, bool derivatives, void *val)
static ustring u_index
Definition: osl_services.h:259
bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform, float time) override
static ustring u_hit
Definition: osl_services.h:298
static ustring u_material_index
Definition: osl_services.h:270
static ustring u_path_ray_depth
Definition: osl_services.h:292
static bool get_background_attribute(KernelGlobals *kg, ShaderData *sd, ustring name, TypeDesc type, bool derivatives, void *val)
static ustring u_is_smooth
Definition: osl_services.h:286
static ustring u_object_location
Definition: osl_services.h:265
static ustring u_curve_random
Definition: osl_services.h:290
int pointcloud_get(OSL::ShaderGlobals *sg, ustring filename, size_t *indices, int count, ustring attr_name, TypeDesc attr_type, void *out_data) override
static ustring u_particle_rotation
Definition: osl_services.h:277
OSL::TextureSystem * texture_system
Definition: osl_services.h:314
static ustring u_particle_velocity
Definition: osl_services.h:279
static ustring u_particle_angular_velocity
Definition: osl_services.h:280
static ustring u_N
Definition: osl_services.h:300
static ustring u_path_glossy_depth
Definition: osl_services.h:294
static ustring u_hitdist
Definition: osl_services.h:299
static ustring u_geom_dupli_uv
Definition: osl_services.h:269
static ustring u_I
Definition: osl_services.h:303
static ustring u_geom_undisplaced
Definition: osl_services.h:285
OSLTextureHandleMap textures
Definition: osl_services.h:315
static ustring u_geom_numpolyvertices
Definition: osl_services.h:281
static ustring u_v
Definition: osl_services.h:305
static ustring u_world
Definition: osl_services.h:260
static ustring u_object_random
Definition: osl_services.h:271
static ustring u_at_bevel
Definition: osl_services.h:307
static ustring u_path_ray_length
Definition: osl_services.h:291
bool getmessage(OSL::ShaderGlobals *sg, ustring source, ustring name, TypeDesc type, void *val, bool derivatives) override
static ustring u_screen
Definition: osl_services.h:262
bool environment(ustring filename, TextureHandle *texture_handle, TexturePerthread *texture_thread_info, TextureOpt &options, OSL::ShaderGlobals *sg, const OSL::Vec3 &R, const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy, int nchannels, float *result, float *dresultds, float *dresultdt, ustring *errormessage) override
bool get_attribute(OSL::ShaderGlobals *sg, bool derivatives, ustring object, TypeDesc type, ustring name, void *val) override
static ustring u_particle_random
Definition: osl_services.h:273
static ustring u_camera
Definition: osl_services.h:261
static ustring u_particle_age
Definition: osl_services.h:274
static ustring u_P
Definition: osl_services.h:302
OSLRenderServices(OSL::TextureSystem *texture_system)
bool texture(ustring filename, TextureSystem::TextureHandle *texture_handle, TexturePerthread *texture_thread_info, TextureOpt &options, OSL::ShaderGlobals *sg, float s, float t, float dsdx, float dtdx, float dsdy, float dtdy, int nchannels, float *result, float *dresultds, float *dresultdt, ustring *errormessage) override
bool get_texture_info(OSL::ShaderGlobals *sg, ustring filename, TextureHandle *texture_handle, int subimage, ustring dataname, TypeDesc datatype, void *data) override
static ustring u_object_index
Definition: osl_services.h:267
static ustring u_path_transparent_depth
Definition: osl_services.h:295
TextureSystem::TextureHandle * get_texture_handle(ustring filename) override
bool trace(TraceOpt &options, OSL::ShaderGlobals *sg, const OSL::Vec3 &P, const OSL::Vec3 &dPdx, const OSL::Vec3 &dPdy, const OSL::Vec3 &R, const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy) override
static ustring u_particle_lifetime
Definition: osl_services.h:275
bool texture3d(ustring filename, TextureHandle *texture_handle, TexturePerthread *texture_thread_info, TextureOpt &options, OSL::ShaderGlobals *sg, const OSL::Vec3 &P, const OSL::Vec3 &dPdx, const OSL::Vec3 &dPdy, const OSL::Vec3 &dPdz, int nchannels, float *result, float *dresultds, float *dresultdt, float *dresultdr, ustring *errormessage) override
static ustring u_is_curve
Definition: osl_services.h:287
static ustring u_object_color
Definition: osl_services.h:266
static ustring u_distance
Definition: osl_services.h:258
static ustring u_Ng
Definition: osl_services.h:301
static ustring u_at_ao
Definition: osl_services.h:308
static ustring u_trace
Definition: osl_services.h:297
bool get_userdata(bool derivatives, ustring name, TypeDesc type, OSL::ShaderGlobals *sg, void *val) override
static ustring u_geom_trianglevertices
Definition: osl_services.h:282
bool get_array_attribute(OSL::ShaderGlobals *sg, bool derivatives, ustring object, TypeDesc type, ustring name, int index, void *val) override
static ustring u_geom_polyvertices
Definition: osl_services.h:283
static ustring u_particle_size
Definition: osl_services.h:278
int pointcloud_search(OSL::ShaderGlobals *sg, ustring filename, const OSL::Vec3 &center, float radius, int max_points, bool sort, size_t *out_indices, float *out_distances, int derivs_offset) override
static ustring u_geom_dupli_generated
Definition: osl_services.h:268
bool good(TextureSystem::TextureHandle *texture_handle) override
static ustring u_geom_name
Definition: osl_services.h:284
Definition: shader.h:80
CCL_NAMESPACE_BEGIN struct Options options
StackEntry * from
double time
static ushort indices[]
uint pos
int count
#define CCL_NAMESPACE_END
ShaderData
static char ** names
Definition: makesdna.c:162
static char ** types
Definition: makesdna.c:164
static float P(float k)
Definition: math_interp.c:41
#define R
Eigen::Vector3d Vec3
Definition: numeric.h:106
OIIO::unordered_map_concurrent< ustring, OSLTextureHandleRef, ustringHash > OSLTextureHandleMap
Definition: osl_services.h:73
OIIO::intrusive_ptr< OSLTextureHandle > OSLTextureHandleRef
Definition: osl_services.h:71
static const char * dataname(short id_code)
Definition: readfile.c:3225
struct SELECTID_Context context
Definition: select_engine.c:47
OSLTextureHandle(Type type=OIIO, int svm_slot=-1)
Definition: osl_services.h:60
ColorSpaceProcessor * processor
Definition: osl_services.h:68
OSL::TextureSystem::TextureHandle * oiio_handle
Definition: osl_services.h:67