osl_services.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #ifndef __OSL_SERVICES_H__
  17. #define __OSL_SERVICES_H__
  18. /* OSL Render Services
  19. *
  20. * Implementation of OSL render services, to retriever matrices, attributes,
  21. * textures and point clouds. In principle this should only be accessing
  22. * kernel data, but currently we also reach back into the Scene to retrieve
  23. * attributes.
  24. */
  25. #include <OSL/oslexec.h>
  26. #include <OSL/oslclosure.h>
  27. #ifdef WITH_PTEX
  28. class PtexCache;
  29. #endif
  30. CCL_NAMESPACE_BEGIN
  31. class Object;
  32. class Scene;
  33. class Shader;
  34. struct ShaderData;
  35. struct float3;
  36. struct KernelGlobals;
  37. /* OSL Texture Handle
  38. *
  39. * OSL texture lookups are string based. If those strings are known at compile
  40. * time, the OSL compiler can cache a texture handle to use instead of a string.
  41. *
  42. * By default it uses TextureSystem::TextureHandle. But since we want to support
  43. * different kinds of textures and color space conversions, this is our own handle
  44. * with additional data.
  45. *
  46. * These are stored in a concurrent hash map, because OSL can compile multiple
  47. * shaders in parallel. */
  48. struct OSLTextureHandle : public OIIO::RefCnt {
  49. enum Type { OIIO, SVM, IES, BEVEL, AO };
  50. OSLTextureHandle(Type type = OIIO, int svm_slot = -1)
  51. : type(type), svm_slot(svm_slot), oiio_handle(NULL), processor(NULL)
  52. {
  53. }
  54. Type type;
  55. int svm_slot;
  56. OSL::TextureSystem::TextureHandle *oiio_handle;
  57. ColorSpaceProcessor *processor;
  58. };
  59. typedef OIIO::intrusive_ptr<OSLTextureHandle> OSLTextureHandleRef;
  60. typedef OIIO::unordered_map_concurrent<ustring, OSLTextureHandleRef, ustringHash>
  61. OSLTextureHandleMap;
  62. /* OSL Render Services
  63. *
  64. * Interface for OSL to access attributes, textures and other scene data. */
  65. class OSLRenderServices : public OSL::RendererServices {
  66. public:
  67. OSLRenderServices(OSL::TextureSystem *texture_system);
  68. ~OSLRenderServices();
  69. bool get_matrix(OSL::ShaderGlobals *sg,
  70. OSL::Matrix44 &result,
  71. OSL::TransformationPtr xform,
  72. float time) override;
  73. bool get_inverse_matrix(OSL::ShaderGlobals *sg,
  74. OSL::Matrix44 &result,
  75. OSL::TransformationPtr xform,
  76. float time) override;
  77. bool get_matrix(OSL::ShaderGlobals *sg,
  78. OSL::Matrix44 &result,
  79. ustring from,
  80. float time) override;
  81. bool get_inverse_matrix(OSL::ShaderGlobals *sg,
  82. OSL::Matrix44 &result,
  83. ustring to,
  84. float time) override;
  85. bool get_matrix(OSL::ShaderGlobals *sg,
  86. OSL::Matrix44 &result,
  87. OSL::TransformationPtr xform) override;
  88. bool get_inverse_matrix(OSL::ShaderGlobals *sg,
  89. OSL::Matrix44 &result,
  90. OSL::TransformationPtr xform) override;
  91. bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from) override;
  92. bool get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, ustring from) override;
  93. bool get_array_attribute(OSL::ShaderGlobals *sg,
  94. bool derivatives,
  95. ustring object,
  96. TypeDesc type,
  97. ustring name,
  98. int index,
  99. void *val) override;
  100. bool get_attribute(OSL::ShaderGlobals *sg,
  101. bool derivatives,
  102. ustring object,
  103. TypeDesc type,
  104. ustring name,
  105. void *val) override;
  106. bool get_attribute(ShaderData *sd,
  107. bool derivatives,
  108. ustring object_name,
  109. TypeDesc type,
  110. ustring name,
  111. void *val);
  112. bool get_userdata(
  113. bool derivatives, ustring name, TypeDesc type, OSL::ShaderGlobals *sg, void *val) override;
  114. int pointcloud_search(OSL::ShaderGlobals *sg,
  115. ustring filename,
  116. const OSL::Vec3 &center,
  117. float radius,
  118. int max_points,
  119. bool sort,
  120. size_t *out_indices,
  121. float *out_distances,
  122. int derivs_offset) override;
  123. int pointcloud_get(OSL::ShaderGlobals *sg,
  124. ustring filename,
  125. size_t *indices,
  126. int count,
  127. ustring attr_name,
  128. TypeDesc attr_type,
  129. void *out_data) override;
  130. bool pointcloud_write(OSL::ShaderGlobals *sg,
  131. ustring filename,
  132. const OSL::Vec3 &pos,
  133. int nattribs,
  134. const ustring *names,
  135. const TypeDesc *types,
  136. const void **data) override;
  137. bool trace(TraceOpt &options,
  138. OSL::ShaderGlobals *sg,
  139. const OSL::Vec3 &P,
  140. const OSL::Vec3 &dPdx,
  141. const OSL::Vec3 &dPdy,
  142. const OSL::Vec3 &R,
  143. const OSL::Vec3 &dRdx,
  144. const OSL::Vec3 &dRdy) override;
  145. bool getmessage(OSL::ShaderGlobals *sg,
  146. ustring source,
  147. ustring name,
  148. TypeDesc type,
  149. void *val,
  150. bool derivatives) override;
  151. TextureSystem::TextureHandle *get_texture_handle(ustring filename) override;
  152. bool good(TextureSystem::TextureHandle *texture_handle) override;
  153. bool texture(ustring filename,
  154. TextureSystem::TextureHandle *texture_handle,
  155. TexturePerthread *texture_thread_info,
  156. TextureOpt &options,
  157. OSL::ShaderGlobals *sg,
  158. float s,
  159. float t,
  160. float dsdx,
  161. float dtdx,
  162. float dsdy,
  163. float dtdy,
  164. int nchannels,
  165. float *result,
  166. float *dresultds,
  167. float *dresultdt,
  168. ustring *errormessage) override;
  169. bool texture3d(ustring filename,
  170. TextureHandle *texture_handle,
  171. TexturePerthread *texture_thread_info,
  172. TextureOpt &options,
  173. OSL::ShaderGlobals *sg,
  174. const OSL::Vec3 &P,
  175. const OSL::Vec3 &dPdx,
  176. const OSL::Vec3 &dPdy,
  177. const OSL::Vec3 &dPdz,
  178. int nchannels,
  179. float *result,
  180. float *dresultds,
  181. float *dresultdt,
  182. float *dresultdr,
  183. ustring *errormessage) override;
  184. bool environment(ustring filename,
  185. TextureHandle *texture_handle,
  186. TexturePerthread *texture_thread_info,
  187. TextureOpt &options,
  188. OSL::ShaderGlobals *sg,
  189. const OSL::Vec3 &R,
  190. const OSL::Vec3 &dRdx,
  191. const OSL::Vec3 &dRdy,
  192. int nchannels,
  193. float *result,
  194. float *dresultds,
  195. float *dresultdt,
  196. ustring *errormessage) override;
  197. bool get_texture_info(OSL::ShaderGlobals *sg,
  198. ustring filename,
  199. TextureHandle *texture_handle,
  200. int subimage,
  201. ustring dataname,
  202. TypeDesc datatype,
  203. void *data) override;
  204. static bool get_background_attribute(
  205. KernelGlobals *kg, ShaderData *sd, ustring name, TypeDesc type, bool derivatives, void *val);
  206. static bool get_object_standard_attribute(
  207. KernelGlobals *kg, ShaderData *sd, ustring name, TypeDesc type, bool derivatives, void *val);
  208. static ustring u_distance;
  209. static ustring u_index;
  210. static ustring u_world;
  211. static ustring u_camera;
  212. static ustring u_screen;
  213. static ustring u_raster;
  214. static ustring u_ndc;
  215. static ustring u_object_location;
  216. static ustring u_object_index;
  217. static ustring u_geom_dupli_generated;
  218. static ustring u_geom_dupli_uv;
  219. static ustring u_material_index;
  220. static ustring u_object_random;
  221. static ustring u_particle_index;
  222. static ustring u_particle_random;
  223. static ustring u_particle_age;
  224. static ustring u_particle_lifetime;
  225. static ustring u_particle_location;
  226. static ustring u_particle_rotation;
  227. static ustring u_particle_size;
  228. static ustring u_particle_velocity;
  229. static ustring u_particle_angular_velocity;
  230. static ustring u_geom_numpolyvertices;
  231. static ustring u_geom_trianglevertices;
  232. static ustring u_geom_polyvertices;
  233. static ustring u_geom_name;
  234. static ustring u_geom_undisplaced;
  235. static ustring u_is_smooth;
  236. static ustring u_is_curve;
  237. static ustring u_curve_thickness;
  238. static ustring u_curve_tangent_normal;
  239. static ustring u_curve_random;
  240. static ustring u_path_ray_length;
  241. static ustring u_path_ray_depth;
  242. static ustring u_path_diffuse_depth;
  243. static ustring u_path_glossy_depth;
  244. static ustring u_path_transparent_depth;
  245. static ustring u_path_transmission_depth;
  246. static ustring u_trace;
  247. static ustring u_hit;
  248. static ustring u_hitdist;
  249. static ustring u_N;
  250. static ustring u_Ng;
  251. static ustring u_P;
  252. static ustring u_I;
  253. static ustring u_u;
  254. static ustring u_v;
  255. static ustring u_empty;
  256. static ustring u_at_bevel;
  257. static ustring u_at_ao;
  258. /* Texture system and texture handle map are part of the services instead of
  259. * globals to be shared between different render sessions. This saves memory,
  260. * and is required because texture handles are cached as part of the shared
  261. * shading system. */
  262. OSL::TextureSystem *texture_system;
  263. OSLTextureHandleMap textures;
  264. };
  265. CCL_NAMESPACE_END
  266. #endif /* __OSL_SERVICES_H__ */