osl_shader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. #include <OSL/oslexec.h>
  17. #include "kernel/kernel_compat_cpu.h"
  18. #include "kernel/kernel_montecarlo.h"
  19. #include "kernel/kernel_types.h"
  20. #include "kernel/split/kernel_split_data_types.h"
  21. #include "kernel/kernel_globals.h"
  22. #include "kernel/geom/geom_object.h"
  23. #include "kernel/osl/osl_closures.h"
  24. #include "kernel/osl/osl_globals.h"
  25. #include "kernel/osl/osl_services.h"
  26. #include "kernel/osl/osl_shader.h"
  27. #include "util/util_foreach.h"
  28. #include "render/attribute.h"
  29. CCL_NAMESPACE_BEGIN
  30. /* Threads */
  31. void OSLShader::thread_init(KernelGlobals *kg,
  32. KernelGlobals *kernel_globals,
  33. OSLGlobals *osl_globals)
  34. {
  35. /* no osl used? */
  36. if (!osl_globals->use) {
  37. kg->osl = NULL;
  38. return;
  39. }
  40. /* per thread kernel data init*/
  41. kg->osl = osl_globals;
  42. OSL::ShadingSystem *ss = kg->osl->ss;
  43. OSLThreadData *tdata = new OSLThreadData();
  44. memset((void *)&tdata->globals, 0, sizeof(OSL::ShaderGlobals));
  45. tdata->globals.tracedata = &tdata->tracedata;
  46. tdata->globals.flipHandedness = false;
  47. tdata->osl_thread_info = ss->create_thread_info();
  48. tdata->context = ss->get_context(tdata->osl_thread_info);
  49. tdata->oiio_thread_info = osl_globals->ts->get_perthread_info();
  50. kg->osl_ss = (OSLShadingSystem *)ss;
  51. kg->osl_tdata = tdata;
  52. }
  53. void OSLShader::thread_free(KernelGlobals *kg)
  54. {
  55. if (!kg->osl)
  56. return;
  57. OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss;
  58. OSLThreadData *tdata = kg->osl_tdata;
  59. ss->release_context(tdata->context);
  60. ss->destroy_thread_info(tdata->osl_thread_info);
  61. delete tdata;
  62. kg->osl = NULL;
  63. kg->osl_ss = NULL;
  64. kg->osl_tdata = NULL;
  65. }
  66. /* Globals */
  67. static void shaderdata_to_shaderglobals(
  68. KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag, OSLThreadData *tdata)
  69. {
  70. OSL::ShaderGlobals *globals = &tdata->globals;
  71. /* copy from shader data to shader globals */
  72. globals->P = TO_VEC3(sd->P);
  73. globals->dPdx = TO_VEC3(sd->dP.dx);
  74. globals->dPdy = TO_VEC3(sd->dP.dy);
  75. globals->I = TO_VEC3(sd->I);
  76. globals->dIdx = TO_VEC3(sd->dI.dx);
  77. globals->dIdy = TO_VEC3(sd->dI.dy);
  78. globals->N = TO_VEC3(sd->N);
  79. globals->Ng = TO_VEC3(sd->Ng);
  80. globals->u = sd->u;
  81. globals->dudx = sd->du.dx;
  82. globals->dudy = sd->du.dy;
  83. globals->v = sd->v;
  84. globals->dvdx = sd->dv.dx;
  85. globals->dvdy = sd->dv.dy;
  86. globals->dPdu = TO_VEC3(sd->dPdu);
  87. globals->dPdv = TO_VEC3(sd->dPdv);
  88. globals->surfacearea = (sd->object == OBJECT_NONE) ? 1.0f : object_surface_area(kg, sd->object);
  89. globals->time = sd->time;
  90. /* booleans */
  91. globals->raytype = path_flag;
  92. globals->backfacing = (sd->flag & SD_BACKFACING);
  93. /* shader data to be used in services callbacks */
  94. globals->renderstate = sd;
  95. /* hacky, we leave it to services to fetch actual object matrix */
  96. globals->shader2common = sd;
  97. globals->object2common = sd;
  98. /* must be set to NULL before execute */
  99. globals->Ci = NULL;
  100. /* clear trace data */
  101. tdata->tracedata.init = false;
  102. /* used by renderservices */
  103. sd->osl_globals = kg;
  104. sd->osl_path_state = state;
  105. }
  106. /* Surface */
  107. static void flatten_surface_closure_tree(ShaderData *sd,
  108. int path_flag,
  109. const OSL::ClosureColor *closure,
  110. float3 weight = make_float3(1.0f, 1.0f, 1.0f))
  111. {
  112. /* OSL gives us a closure tree, we flatten it into arrays per
  113. * closure type, for evaluation, sampling, etc later on. */
  114. switch (closure->id) {
  115. case OSL::ClosureColor::MUL: {
  116. OSL::ClosureMul *mul = (OSL::ClosureMul *)closure;
  117. flatten_surface_closure_tree(sd, path_flag, mul->closure, TO_FLOAT3(mul->weight) * weight);
  118. break;
  119. }
  120. case OSL::ClosureColor::ADD: {
  121. OSL::ClosureAdd *add = (OSL::ClosureAdd *)closure;
  122. flatten_surface_closure_tree(sd, path_flag, add->closureA, weight);
  123. flatten_surface_closure_tree(sd, path_flag, add->closureB, weight);
  124. break;
  125. }
  126. default: {
  127. OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure;
  128. CClosurePrimitive *prim = (CClosurePrimitive *)comp->data();
  129. if (prim) {
  130. #ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS
  131. weight = weight * TO_FLOAT3(comp->w);
  132. #endif
  133. prim->setup(sd, path_flag, weight);
  134. }
  135. break;
  136. }
  137. }
  138. }
  139. void OSLShader::eval_surface(KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag)
  140. {
  141. /* setup shader globals from shader data */
  142. OSLThreadData *tdata = kg->osl_tdata;
  143. shaderdata_to_shaderglobals(kg, sd, state, path_flag, tdata);
  144. /* execute shader for this point */
  145. OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss;
  146. OSL::ShaderGlobals *globals = &tdata->globals;
  147. OSL::ShadingContext *octx = tdata->context;
  148. int shader = sd->shader & SHADER_MASK;
  149. /* automatic bump shader */
  150. if (kg->osl->bump_state[shader]) {
  151. /* save state */
  152. float3 P = sd->P;
  153. float3 dPdx = sd->dP.dx;
  154. float3 dPdy = sd->dP.dy;
  155. /* set state as if undisplaced */
  156. if (sd->flag & SD_HAS_DISPLACEMENT) {
  157. float data[9];
  158. bool found = kg->osl->services->get_attribute(sd,
  159. true,
  160. OSLRenderServices::u_empty,
  161. TypeDesc::TypeVector,
  162. OSLRenderServices::u_geom_undisplaced,
  163. data);
  164. (void)found;
  165. assert(found);
  166. memcpy(&sd->P, data, sizeof(float) * 3);
  167. memcpy(&sd->dP.dx, data + 3, sizeof(float) * 3);
  168. memcpy(&sd->dP.dy, data + 6, sizeof(float) * 3);
  169. object_position_transform(kg, sd, &sd->P);
  170. object_dir_transform(kg, sd, &sd->dP.dx);
  171. object_dir_transform(kg, sd, &sd->dP.dy);
  172. globals->P = TO_VEC3(sd->P);
  173. globals->dPdx = TO_VEC3(sd->dP.dx);
  174. globals->dPdy = TO_VEC3(sd->dP.dy);
  175. }
  176. /* execute bump shader */
  177. ss->execute(octx, *(kg->osl->bump_state[shader]), *globals);
  178. /* reset state */
  179. sd->P = P;
  180. sd->dP.dx = dPdx;
  181. sd->dP.dy = dPdy;
  182. globals->P = TO_VEC3(P);
  183. globals->dPdx = TO_VEC3(dPdx);
  184. globals->dPdy = TO_VEC3(dPdy);
  185. }
  186. /* surface shader */
  187. if (kg->osl->surface_state[shader]) {
  188. ss->execute(octx, *(kg->osl->surface_state[shader]), *globals);
  189. }
  190. /* flatten closure tree */
  191. if (globals->Ci)
  192. flatten_surface_closure_tree(sd, path_flag, globals->Ci);
  193. }
  194. /* Background */
  195. static void flatten_background_closure_tree(ShaderData *sd,
  196. const OSL::ClosureColor *closure,
  197. float3 weight = make_float3(1.0f, 1.0f, 1.0f))
  198. {
  199. /* OSL gives us a closure tree, if we are shading for background there
  200. * is only one supported closure type at the moment, which has no evaluation
  201. * functions, so we just sum the weights */
  202. switch (closure->id) {
  203. case OSL::ClosureColor::MUL: {
  204. OSL::ClosureMul *mul = (OSL::ClosureMul *)closure;
  205. flatten_background_closure_tree(sd, mul->closure, weight * TO_FLOAT3(mul->weight));
  206. break;
  207. }
  208. case OSL::ClosureColor::ADD: {
  209. OSL::ClosureAdd *add = (OSL::ClosureAdd *)closure;
  210. flatten_background_closure_tree(sd, add->closureA, weight);
  211. flatten_background_closure_tree(sd, add->closureB, weight);
  212. break;
  213. }
  214. default: {
  215. OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure;
  216. CClosurePrimitive *prim = (CClosurePrimitive *)comp->data();
  217. if (prim) {
  218. #ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS
  219. weight = weight * TO_FLOAT3(comp->w);
  220. #endif
  221. prim->setup(sd, 0, weight);
  222. }
  223. break;
  224. }
  225. }
  226. }
  227. void OSLShader::eval_background(KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag)
  228. {
  229. /* setup shader globals from shader data */
  230. OSLThreadData *tdata = kg->osl_tdata;
  231. shaderdata_to_shaderglobals(kg, sd, state, path_flag, tdata);
  232. /* execute shader for this point */
  233. OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss;
  234. OSL::ShaderGlobals *globals = &tdata->globals;
  235. OSL::ShadingContext *octx = tdata->context;
  236. if (kg->osl->background_state) {
  237. ss->execute(octx, *(kg->osl->background_state), *globals);
  238. }
  239. /* return background color immediately */
  240. if (globals->Ci)
  241. flatten_background_closure_tree(sd, globals->Ci);
  242. }
  243. /* Volume */
  244. static void flatten_volume_closure_tree(ShaderData *sd,
  245. const OSL::ClosureColor *closure,
  246. float3 weight = make_float3(1.0f, 1.0f, 1.0f))
  247. {
  248. /* OSL gives us a closure tree, we flatten it into arrays per
  249. * closure type, for evaluation, sampling, etc later on. */
  250. switch (closure->id) {
  251. case OSL::ClosureColor::MUL: {
  252. OSL::ClosureMul *mul = (OSL::ClosureMul *)closure;
  253. flatten_volume_closure_tree(sd, mul->closure, TO_FLOAT3(mul->weight) * weight);
  254. break;
  255. }
  256. case OSL::ClosureColor::ADD: {
  257. OSL::ClosureAdd *add = (OSL::ClosureAdd *)closure;
  258. flatten_volume_closure_tree(sd, add->closureA, weight);
  259. flatten_volume_closure_tree(sd, add->closureB, weight);
  260. break;
  261. }
  262. default: {
  263. OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure;
  264. CClosurePrimitive *prim = (CClosurePrimitive *)comp->data();
  265. if (prim) {
  266. #ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS
  267. weight = weight * TO_FLOAT3(comp->w);
  268. #endif
  269. prim->setup(sd, 0, weight);
  270. }
  271. }
  272. }
  273. }
  274. void OSLShader::eval_volume(KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag)
  275. {
  276. /* setup shader globals from shader data */
  277. OSLThreadData *tdata = kg->osl_tdata;
  278. shaderdata_to_shaderglobals(kg, sd, state, path_flag, tdata);
  279. /* execute shader */
  280. OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss;
  281. OSL::ShaderGlobals *globals = &tdata->globals;
  282. OSL::ShadingContext *octx = tdata->context;
  283. int shader = sd->shader & SHADER_MASK;
  284. if (kg->osl->volume_state[shader]) {
  285. ss->execute(octx, *(kg->osl->volume_state[shader]), *globals);
  286. }
  287. /* flatten closure tree */
  288. if (globals->Ci)
  289. flatten_volume_closure_tree(sd, globals->Ci);
  290. }
  291. /* Displacement */
  292. void OSLShader::eval_displacement(KernelGlobals *kg, ShaderData *sd, PathState *state)
  293. {
  294. /* setup shader globals from shader data */
  295. OSLThreadData *tdata = kg->osl_tdata;
  296. shaderdata_to_shaderglobals(kg, sd, state, 0, tdata);
  297. /* execute shader */
  298. OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss;
  299. OSL::ShaderGlobals *globals = &tdata->globals;
  300. OSL::ShadingContext *octx = tdata->context;
  301. int shader = sd->shader & SHADER_MASK;
  302. if (kg->osl->displacement_state[shader]) {
  303. ss->execute(octx, *(kg->osl->displacement_state[shader]), *globals);
  304. }
  305. /* get back position */
  306. sd->P = TO_FLOAT3(globals->P);
  307. }
  308. /* Attributes */
  309. int OSLShader::find_attribute(KernelGlobals *kg,
  310. const ShaderData *sd,
  311. uint id,
  312. AttributeDescriptor *desc)
  313. {
  314. /* for OSL, a hash map is used to lookup the attribute by name. */
  315. int object = sd->object * ATTR_PRIM_TYPES;
  316. #ifdef __HAIR__
  317. if (sd->type & PRIMITIVE_ALL_CURVE)
  318. object += ATTR_PRIM_CURVE;
  319. #endif
  320. OSLGlobals::AttributeMap &attr_map = kg->osl->attribute_map[object];
  321. ustring stdname(std::string("geom:") +
  322. std::string(Attribute::standard_name((AttributeStandard)id)));
  323. OSLGlobals::AttributeMap::const_iterator it = attr_map.find(stdname);
  324. if (it != attr_map.end()) {
  325. const OSLGlobals::Attribute &osl_attr = it->second;
  326. *desc = osl_attr.desc;
  327. if (sd->prim == PRIM_NONE && (AttributeElement)osl_attr.desc.element != ATTR_ELEMENT_MESH) {
  328. desc->offset = ATTR_STD_NOT_FOUND;
  329. return ATTR_STD_NOT_FOUND;
  330. }
  331. /* return result */
  332. if (osl_attr.desc.element == ATTR_ELEMENT_NONE) {
  333. desc->offset = ATTR_STD_NOT_FOUND;
  334. }
  335. return desc->offset;
  336. }
  337. else {
  338. desc->offset = ATTR_STD_NOT_FOUND;
  339. return (int)ATTR_STD_NOT_FOUND;
  340. }
  341. }
  342. CCL_NAMESPACE_END