osl.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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 "device/device.h"
  17. #include "render/colorspace.h"
  18. #include "render/graph.h"
  19. #include "render/light.h"
  20. #include "render/osl.h"
  21. #include "render/scene.h"
  22. #include "render/shader.h"
  23. #include "render/nodes.h"
  24. #ifdef WITH_OSL
  25. # include "kernel/osl/osl_globals.h"
  26. # include "kernel/osl/osl_services.h"
  27. # include "kernel/osl/osl_shader.h"
  28. # include "util/util_aligned_malloc.h"
  29. # include "util/util_foreach.h"
  30. # include "util/util_logging.h"
  31. # include "util/util_md5.h"
  32. # include "util/util_path.h"
  33. # include "util/util_progress.h"
  34. # include "util/util_projection.h"
  35. #endif
  36. CCL_NAMESPACE_BEGIN
  37. #ifdef WITH_OSL
  38. /* Shared Texture and Shading System */
  39. OSL::TextureSystem *OSLShaderManager::ts_shared = NULL;
  40. int OSLShaderManager::ts_shared_users = 0;
  41. thread_mutex OSLShaderManager::ts_shared_mutex;
  42. OSL::ShadingSystem *OSLShaderManager::ss_shared = NULL;
  43. OSLRenderServices *OSLShaderManager::services_shared = NULL;
  44. int OSLShaderManager::ss_shared_users = 0;
  45. thread_mutex OSLShaderManager::ss_shared_mutex;
  46. thread_mutex OSLShaderManager::ss_mutex;
  47. int OSLCompiler::texture_shared_unique_id = 0;
  48. /* Shader Manager */
  49. OSLShaderManager::OSLShaderManager()
  50. {
  51. texture_system_init();
  52. shading_system_init();
  53. }
  54. OSLShaderManager::~OSLShaderManager()
  55. {
  56. shading_system_free();
  57. texture_system_free();
  58. }
  59. void OSLShaderManager::free_memory()
  60. {
  61. # ifdef OSL_HAS_BLENDER_CLEANUP_FIX
  62. /* There is a problem with llvm+osl: The order global destructors across
  63. * different compilation units run cannot be guaranteed, on windows this means
  64. * that the llvm destructors run before the osl destructors, causing a crash
  65. * when the process exits. the OSL in svn has a special cleanup hack to
  66. * sidestep this behavior */
  67. OSL::pvt::LLVM_Util::Cleanup();
  68. # endif
  69. }
  70. void OSLShaderManager::reset(Scene * /*scene*/)
  71. {
  72. shading_system_free();
  73. shading_system_init();
  74. }
  75. void OSLShaderManager::device_update(Device *device,
  76. DeviceScene *dscene,
  77. Scene *scene,
  78. Progress &progress)
  79. {
  80. if (!need_update)
  81. return;
  82. VLOG(1) << "Total " << scene->shaders.size() << " shaders.";
  83. device_free(device, dscene, scene);
  84. /* determine which shaders are in use */
  85. device_update_shaders_used(scene);
  86. /* create shaders */
  87. OSLGlobals *og = (OSLGlobals *)device->osl_memory();
  88. foreach (Shader *shader, scene->shaders) {
  89. assert(shader->graph);
  90. if (progress.get_cancel())
  91. return;
  92. /* we can only compile one shader at the time as the OSL ShadingSytem
  93. * has a single state, but we put the lock here so different renders can
  94. * compile shaders alternating */
  95. thread_scoped_lock lock(ss_mutex);
  96. OSLCompiler compiler(this, services, ss, scene->image_manager, scene->light_manager);
  97. compiler.background = (shader == scene->default_background);
  98. compiler.compile(scene, og, shader);
  99. if (shader->use_mis && shader->has_surface_emission)
  100. scene->light_manager->need_update = true;
  101. }
  102. /* setup shader engine */
  103. og->ss = ss;
  104. og->ts = ts;
  105. og->services = services;
  106. int background_id = scene->shader_manager->get_shader_id(scene->default_background);
  107. og->background_state = og->surface_state[background_id & SHADER_MASK];
  108. og->use = true;
  109. foreach (Shader *shader, scene->shaders)
  110. shader->need_update = false;
  111. need_update = false;
  112. /* set texture system */
  113. scene->image_manager->set_osl_texture_system((void *)ts);
  114. /* add special builtin texture types */
  115. services->textures.insert(ustring("@ao"), new OSLTextureHandle(OSLTextureHandle::AO));
  116. services->textures.insert(ustring("@bevel"), new OSLTextureHandle(OSLTextureHandle::BEVEL));
  117. device_update_common(device, dscene, scene, progress);
  118. {
  119. /* Perform greedyjit optimization.
  120. *
  121. * This might waste time on optimizing gorups which are never actually
  122. * used, but this prevents OSL from allocating data on TLS at render
  123. * time.
  124. *
  125. * This is much better for us because this way we aren't required to
  126. * stop task scheduler threads to make sure all TLS is clean and don't
  127. * have issues with TLS data free accessing freed memory if task scheduler
  128. * is being freed after the Session is freed.
  129. */
  130. thread_scoped_lock lock(ss_shared_mutex);
  131. ss->optimize_all_groups();
  132. }
  133. }
  134. void OSLShaderManager::device_free(Device *device, DeviceScene *dscene, Scene *scene)
  135. {
  136. OSLGlobals *og = (OSLGlobals *)device->osl_memory();
  137. device_free_common(device, dscene, scene);
  138. /* clear shader engine */
  139. og->use = false;
  140. og->ss = NULL;
  141. og->ts = NULL;
  142. og->surface_state.clear();
  143. og->volume_state.clear();
  144. og->displacement_state.clear();
  145. og->bump_state.clear();
  146. og->background_state.reset();
  147. }
  148. void OSLShaderManager::texture_system_init()
  149. {
  150. /* create texture system, shared between different renders to reduce memory usage */
  151. thread_scoped_lock lock(ts_shared_mutex);
  152. if (ts_shared_users == 0) {
  153. ts_shared = TextureSystem::create(true);
  154. ts_shared->attribute("automip", 1);
  155. ts_shared->attribute("autotile", 64);
  156. ts_shared->attribute("gray_to_rgb", 1);
  157. /* effectively unlimited for now, until we support proper mipmap lookups */
  158. ts_shared->attribute("max_memory_MB", 16384);
  159. }
  160. ts = ts_shared;
  161. ts_shared_users++;
  162. }
  163. void OSLShaderManager::texture_system_free()
  164. {
  165. /* shared texture system decrease users and destroy if no longer used */
  166. thread_scoped_lock lock(ts_shared_mutex);
  167. ts_shared_users--;
  168. if (ts_shared_users == 0) {
  169. ts_shared->invalidate_all(true);
  170. OSL::TextureSystem::destroy(ts_shared);
  171. ts_shared = NULL;
  172. }
  173. ts = NULL;
  174. }
  175. void OSLShaderManager::shading_system_init()
  176. {
  177. /* create shading system, shared between different renders to reduce memory usage */
  178. thread_scoped_lock lock(ss_shared_mutex);
  179. if (ss_shared_users == 0) {
  180. /* Must use aligned new due to concurrent hash map. */
  181. services_shared = util_aligned_new<OSLRenderServices>(ts_shared);
  182. string shader_path = path_get("shader");
  183. # ifdef _WIN32
  184. /* Annoying thing, Cycles stores paths in UTF-8 codepage, so it can
  185. * operate with file paths with any character. This requires to use wide
  186. * char functions, but OSL uses old fashioned ANSI functions which means:
  187. *
  188. * - We have to convert our paths to ANSI before passing to OSL
  189. * - OSL can't be used when there's a multi-byte character in the path
  190. * to the shaders folder.
  191. */
  192. shader_path = string_to_ansi(shader_path);
  193. # endif
  194. ss_shared = new OSL::ShadingSystem(services_shared, ts_shared, &errhandler);
  195. ss_shared->attribute("lockgeom", 1);
  196. ss_shared->attribute("commonspace", "world");
  197. ss_shared->attribute("searchpath:shader", shader_path);
  198. ss_shared->attribute("greedyjit", 1);
  199. VLOG(1) << "Using shader search path: " << shader_path;
  200. /* our own ray types */
  201. static const char *raytypes[] = {
  202. "camera", /* PATH_RAY_CAMERA */
  203. "reflection", /* PATH_RAY_REFLECT */
  204. "refraction", /* PATH_RAY_TRANSMIT */
  205. "diffuse", /* PATH_RAY_DIFFUSE */
  206. "glossy", /* PATH_RAY_GLOSSY */
  207. "singular", /* PATH_RAY_SINGULAR */
  208. "transparent", /* PATH_RAY_TRANSPARENT */
  209. "shadow", /* PATH_RAY_SHADOW_OPAQUE_NON_CATCHER */
  210. "shadow", /* PATH_RAY_SHADOW_OPAQUE_CATCHER */
  211. "shadow", /* PATH_RAY_SHADOW_TRANSPARENT_NON_CATCHER */
  212. "shadow", /* PATH_RAY_SHADOW_TRANSPARENT_CATCHER */
  213. "__unused__", "volume_scatter", /* PATH_RAY_VOLUME_SCATTER */
  214. "__unused__",
  215. "__unused__", "diffuse_ancestor", /* PATH_RAY_DIFFUSE_ANCESTOR */
  216. "__unused__", "__unused__", "__unused__", "__unused__",
  217. "__unused__", "__unused__", "__unused__",
  218. };
  219. const int nraytypes = sizeof(raytypes) / sizeof(raytypes[0]);
  220. ss_shared->attribute("raytypes", TypeDesc(TypeDesc::STRING, nraytypes), raytypes);
  221. OSLShader::register_closures((OSLShadingSystem *)ss_shared);
  222. loaded_shaders.clear();
  223. }
  224. ss = ss_shared;
  225. services = services_shared;
  226. ss_shared_users++;
  227. }
  228. void OSLShaderManager::shading_system_free()
  229. {
  230. /* shared shading system decrease users and destroy if no longer used */
  231. thread_scoped_lock lock(ss_shared_mutex);
  232. ss_shared_users--;
  233. if (ss_shared_users == 0) {
  234. delete ss_shared;
  235. ss_shared = NULL;
  236. util_aligned_delete(services_shared);
  237. services_shared = NULL;
  238. }
  239. ss = NULL;
  240. services = NULL;
  241. }
  242. bool OSLShaderManager::osl_compile(const string &inputfile, const string &outputfile)
  243. {
  244. vector<string> options;
  245. string stdosl_path;
  246. string shader_path = path_get("shader");
  247. /* specify output file name */
  248. options.push_back("-o");
  249. options.push_back(outputfile);
  250. /* specify standard include path */
  251. string include_path_arg = string("-I") + shader_path;
  252. options.push_back(include_path_arg);
  253. stdosl_path = path_get("shader/stdosl.h");
  254. /* compile */
  255. OSL::OSLCompiler *compiler = new OSL::OSLCompiler(&OSL::ErrorHandler::default_handler());
  256. bool ok = compiler->compile(string_view(inputfile), options, string_view(stdosl_path));
  257. delete compiler;
  258. return ok;
  259. }
  260. bool OSLShaderManager::osl_query(OSL::OSLQuery &query, const string &filepath)
  261. {
  262. string searchpath = path_user_get("shaders");
  263. return query.open(filepath, searchpath);
  264. }
  265. static string shader_filepath_hash(const string &filepath, uint64_t modified_time)
  266. {
  267. /* compute a hash from filepath and modified time to detect changes */
  268. MD5Hash md5;
  269. md5.append((const uint8_t *)filepath.c_str(), filepath.size());
  270. md5.append((const uint8_t *)&modified_time, sizeof(modified_time));
  271. return md5.get_hex();
  272. }
  273. const char *OSLShaderManager::shader_test_loaded(const string &hash)
  274. {
  275. map<string, OSLShaderInfo>::iterator it = loaded_shaders.find(hash);
  276. return (it == loaded_shaders.end()) ? NULL : it->first.c_str();
  277. }
  278. OSLShaderInfo *OSLShaderManager::shader_loaded_info(const string &hash)
  279. {
  280. map<string, OSLShaderInfo>::iterator it = loaded_shaders.find(hash);
  281. return (it == loaded_shaders.end()) ? NULL : &it->second;
  282. }
  283. const char *OSLShaderManager::shader_load_filepath(string filepath)
  284. {
  285. size_t len = filepath.size();
  286. string extension = filepath.substr(len - 4);
  287. uint64_t modified_time = path_modified_time(filepath);
  288. if (extension == ".osl") {
  289. /* .OSL File */
  290. string osopath = filepath.substr(0, len - 4) + ".oso";
  291. uint64_t oso_modified_time = path_modified_time(osopath);
  292. /* test if we have loaded the corresponding .OSO already */
  293. if (oso_modified_time != 0) {
  294. const char *hash = shader_test_loaded(shader_filepath_hash(osopath, oso_modified_time));
  295. if (hash)
  296. return hash;
  297. }
  298. /* autocompile .OSL to .OSO if needed */
  299. if (oso_modified_time == 0 || (oso_modified_time < modified_time)) {
  300. OSLShaderManager::osl_compile(filepath, osopath);
  301. modified_time = path_modified_time(osopath);
  302. }
  303. else
  304. modified_time = oso_modified_time;
  305. filepath = osopath;
  306. }
  307. else {
  308. if (extension == ".oso") {
  309. /* .OSO File, nothing to do */
  310. }
  311. else if (path_dirname(filepath) == "") {
  312. /* .OSO File in search path */
  313. filepath = path_join(path_user_get("shaders"), filepath + ".oso");
  314. }
  315. else {
  316. /* unknown file */
  317. return NULL;
  318. }
  319. /* test if we have loaded this .OSO already */
  320. const char *hash = shader_test_loaded(shader_filepath_hash(filepath, modified_time));
  321. if (hash)
  322. return hash;
  323. }
  324. /* read oso bytecode from file */
  325. string bytecode_hash = shader_filepath_hash(filepath, modified_time);
  326. string bytecode;
  327. if (!path_read_text(filepath, bytecode)) {
  328. fprintf(stderr, "Cycles shader graph: failed to read file %s\n", filepath.c_str());
  329. OSLShaderInfo info;
  330. loaded_shaders[bytecode_hash] = info; /* to avoid repeat tries */
  331. return NULL;
  332. }
  333. return shader_load_bytecode(bytecode_hash, bytecode);
  334. }
  335. const char *OSLShaderManager::shader_load_bytecode(const string &hash, const string &bytecode)
  336. {
  337. ss->LoadMemoryCompiledShader(hash.c_str(), bytecode.c_str());
  338. OSLShaderInfo info;
  339. if (!info.query.open_bytecode(bytecode)) {
  340. fprintf(stderr, "OSL query error: %s\n", info.query.geterror().c_str());
  341. }
  342. /* this is a bit weak, but works */
  343. info.has_surface_emission = (bytecode.find("\"emission\"") != string::npos);
  344. info.has_surface_transparent = (bytecode.find("\"transparent\"") != string::npos);
  345. info.has_surface_bssrdf = (bytecode.find("\"bssrdf\"") != string::npos);
  346. loaded_shaders[hash] = info;
  347. return loaded_shaders.find(hash)->first.c_str();
  348. }
  349. OSLNode *OSLShaderManager::osl_node(const std::string &filepath,
  350. const std::string &bytecode_hash,
  351. const std::string &bytecode)
  352. {
  353. /* create query */
  354. const char *hash;
  355. if (!filepath.empty()) {
  356. hash = shader_load_filepath(filepath);
  357. }
  358. else {
  359. hash = shader_test_loaded(bytecode_hash);
  360. if (!hash)
  361. hash = shader_load_bytecode(bytecode_hash, bytecode);
  362. }
  363. if (!hash) {
  364. return NULL;
  365. }
  366. OSLShaderInfo *info = shader_loaded_info(hash);
  367. /* count number of inputs */
  368. size_t num_inputs = 0;
  369. for (int i = 0; i < info->query.nparams(); i++) {
  370. const OSL::OSLQuery::Parameter *param = info->query.getparam(i);
  371. /* skip unsupported types */
  372. if (param->varlenarray || param->isstruct || param->type.arraylen > 1)
  373. continue;
  374. if (!param->isoutput)
  375. num_inputs++;
  376. }
  377. /* create node */
  378. OSLNode *node = OSLNode::create(num_inputs);
  379. /* add new sockets from parameters */
  380. set<void *> used_sockets;
  381. for (int i = 0; i < info->query.nparams(); i++) {
  382. const OSL::OSLQuery::Parameter *param = info->query.getparam(i);
  383. /* skip unsupported types */
  384. if (param->varlenarray || param->isstruct || param->type.arraylen > 1)
  385. continue;
  386. SocketType::Type socket_type;
  387. if (param->isclosure) {
  388. socket_type = SocketType::CLOSURE;
  389. }
  390. else if (param->type.vecsemantics != TypeDesc::NOSEMANTICS) {
  391. if (param->type.vecsemantics == TypeDesc::COLOR)
  392. socket_type = SocketType::COLOR;
  393. else if (param->type.vecsemantics == TypeDesc::POINT)
  394. socket_type = SocketType::POINT;
  395. else if (param->type.vecsemantics == TypeDesc::VECTOR)
  396. socket_type = SocketType::VECTOR;
  397. else if (param->type.vecsemantics == TypeDesc::NORMAL)
  398. socket_type = SocketType::NORMAL;
  399. else
  400. continue;
  401. if (!param->isoutput && param->validdefault) {
  402. float3 *default_value = (float3 *)node->input_default_value();
  403. default_value->x = param->fdefault[0];
  404. default_value->y = param->fdefault[1];
  405. default_value->z = param->fdefault[2];
  406. }
  407. }
  408. else if (param->type.aggregate == TypeDesc::SCALAR) {
  409. if (param->type.basetype == TypeDesc::INT) {
  410. socket_type = SocketType::INT;
  411. if (!param->isoutput && param->validdefault) {
  412. *(int *)node->input_default_value() = param->idefault[0];
  413. }
  414. }
  415. else if (param->type.basetype == TypeDesc::FLOAT) {
  416. socket_type = SocketType::FLOAT;
  417. if (!param->isoutput && param->validdefault) {
  418. *(float *)node->input_default_value() = param->fdefault[0];
  419. }
  420. }
  421. else if (param->type.basetype == TypeDesc::STRING) {
  422. socket_type = SocketType::STRING;
  423. if (!param->isoutput && param->validdefault) {
  424. *(ustring *)node->input_default_value() = param->sdefault[0];
  425. }
  426. }
  427. else
  428. continue;
  429. }
  430. else
  431. continue;
  432. if (param->isoutput) {
  433. node->add_output(param->name, socket_type);
  434. }
  435. else {
  436. node->add_input(param->name, socket_type);
  437. }
  438. }
  439. /* set bytcode hash or filepath */
  440. if (!bytecode_hash.empty()) {
  441. node->bytecode_hash = bytecode_hash;
  442. }
  443. else {
  444. node->filepath = filepath;
  445. }
  446. /* Generate inputs and outputs */
  447. node->create_inputs_outputs(node->type);
  448. return node;
  449. }
  450. /* Graph Compiler */
  451. OSLCompiler::OSLCompiler(OSLShaderManager *manager,
  452. OSLRenderServices *services,
  453. OSL::ShadingSystem *ss,
  454. ImageManager *image_manager,
  455. LightManager *light_manager)
  456. : image_manager(image_manager),
  457. light_manager(light_manager),
  458. manager(manager),
  459. services(services),
  460. ss(ss)
  461. {
  462. current_type = SHADER_TYPE_SURFACE;
  463. current_shader = NULL;
  464. background = false;
  465. }
  466. string OSLCompiler::id(ShaderNode *node)
  467. {
  468. /* assign layer unique name based on pointer address + bump mode */
  469. stringstream stream;
  470. stream << "node_" << node->type->name << "_" << node;
  471. return stream.str();
  472. }
  473. string OSLCompiler::compatible_name(ShaderNode *node, ShaderInput *input)
  474. {
  475. string sname(input->name().string());
  476. size_t i;
  477. /* strip whitespace */
  478. while ((i = sname.find(" ")) != string::npos)
  479. sname.replace(i, 1, "");
  480. /* if output exists with the same name, add "In" suffix */
  481. foreach (ShaderOutput *output, node->outputs) {
  482. if (input->name() == output->name()) {
  483. sname += "In";
  484. break;
  485. }
  486. }
  487. return sname;
  488. }
  489. string OSLCompiler::compatible_name(ShaderNode *node, ShaderOutput *output)
  490. {
  491. string sname(output->name().string());
  492. size_t i;
  493. /* strip whitespace */
  494. while ((i = sname.find(" ")) != string::npos)
  495. sname.replace(i, 1, "");
  496. /* if input exists with the same name, add "Out" suffix */
  497. foreach (ShaderInput *input, node->inputs) {
  498. if (input->name() == output->name()) {
  499. sname += "Out";
  500. break;
  501. }
  502. }
  503. return sname;
  504. }
  505. bool OSLCompiler::node_skip_input(ShaderNode *node, ShaderInput *input)
  506. {
  507. /* exception for output node, only one input is actually used
  508. * depending on the current shader type */
  509. if (input->flags() & SocketType::SVM_INTERNAL)
  510. return true;
  511. if (node->special_type == SHADER_SPECIAL_TYPE_OUTPUT) {
  512. if (input->name() == "Surface" && current_type != SHADER_TYPE_SURFACE)
  513. return true;
  514. if (input->name() == "Volume" && current_type != SHADER_TYPE_VOLUME)
  515. return true;
  516. if (input->name() == "Displacement" && current_type != SHADER_TYPE_DISPLACEMENT)
  517. return true;
  518. if (input->name() == "Normal" && current_type != SHADER_TYPE_BUMP)
  519. return true;
  520. }
  521. else if (node->special_type == SHADER_SPECIAL_TYPE_BUMP) {
  522. if (input->name() == "Height")
  523. return true;
  524. }
  525. else if (current_type == SHADER_TYPE_DISPLACEMENT && input->link &&
  526. input->link->parent->special_type == SHADER_SPECIAL_TYPE_BUMP)
  527. return true;
  528. return false;
  529. }
  530. void OSLCompiler::add(ShaderNode *node, const char *name, bool isfilepath)
  531. {
  532. /* load filepath */
  533. if (isfilepath) {
  534. name = manager->shader_load_filepath(name);
  535. if (name == NULL)
  536. return;
  537. }
  538. /* pass in fixed parameter values */
  539. foreach (ShaderInput *input, node->inputs) {
  540. if (!input->link) {
  541. /* checks to untangle graphs */
  542. if (node_skip_input(node, input))
  543. continue;
  544. /* already has default value assigned */
  545. else if (input->flags() & SocketType::DEFAULT_LINK_MASK)
  546. continue;
  547. string param_name = compatible_name(node, input);
  548. const SocketType &socket = input->socket_type;
  549. switch (input->type()) {
  550. case SocketType::COLOR:
  551. parameter_color(param_name.c_str(), node->get_float3(socket));
  552. break;
  553. case SocketType::POINT:
  554. parameter_point(param_name.c_str(), node->get_float3(socket));
  555. break;
  556. case SocketType::VECTOR:
  557. parameter_vector(param_name.c_str(), node->get_float3(socket));
  558. break;
  559. case SocketType::NORMAL:
  560. parameter_normal(param_name.c_str(), node->get_float3(socket));
  561. break;
  562. case SocketType::FLOAT:
  563. parameter(param_name.c_str(), node->get_float(socket));
  564. break;
  565. case SocketType::INT:
  566. parameter(param_name.c_str(), node->get_int(socket));
  567. break;
  568. case SocketType::STRING:
  569. parameter(param_name.c_str(), node->get_string(socket));
  570. break;
  571. case SocketType::CLOSURE:
  572. case SocketType::UNDEFINED:
  573. default:
  574. break;
  575. }
  576. }
  577. }
  578. /* create shader of the appropriate type. OSL only distinguishes between "surface"
  579. * and "displacement" atm */
  580. if (current_type == SHADER_TYPE_SURFACE)
  581. ss->Shader("surface", name, id(node).c_str());
  582. else if (current_type == SHADER_TYPE_VOLUME)
  583. ss->Shader("surface", name, id(node).c_str());
  584. else if (current_type == SHADER_TYPE_DISPLACEMENT)
  585. ss->Shader("displacement", name, id(node).c_str());
  586. else if (current_type == SHADER_TYPE_BUMP)
  587. ss->Shader("displacement", name, id(node).c_str());
  588. else
  589. assert(0);
  590. /* link inputs to other nodes */
  591. foreach (ShaderInput *input, node->inputs) {
  592. if (input->link) {
  593. if (node_skip_input(node, input))
  594. continue;
  595. /* connect shaders */
  596. string id_from = id(input->link->parent);
  597. string id_to = id(node);
  598. string param_from = compatible_name(input->link->parent, input->link);
  599. string param_to = compatible_name(node, input);
  600. ss->ConnectShaders(id_from.c_str(), param_from.c_str(), id_to.c_str(), param_to.c_str());
  601. }
  602. }
  603. /* test if we shader contains specific closures */
  604. OSLShaderInfo *info = manager->shader_loaded_info(name);
  605. if (current_type == SHADER_TYPE_SURFACE) {
  606. if (info) {
  607. if (info->has_surface_emission)
  608. current_shader->has_surface_emission = true;
  609. if (info->has_surface_transparent)
  610. current_shader->has_surface_transparent = true;
  611. if (info->has_surface_bssrdf) {
  612. current_shader->has_surface_bssrdf = true;
  613. current_shader->has_bssrdf_bump = true; /* can't detect yet */
  614. }
  615. current_shader->has_bump = true; /* can't detect yet */
  616. }
  617. if (node->has_spatial_varying()) {
  618. current_shader->has_surface_spatial_varying = true;
  619. }
  620. }
  621. else if (current_type == SHADER_TYPE_VOLUME) {
  622. if (node->has_spatial_varying())
  623. current_shader->has_volume_spatial_varying = true;
  624. }
  625. if (node->has_object_dependency()) {
  626. current_shader->has_object_dependency = true;
  627. }
  628. if (node->has_attribute_dependency()) {
  629. current_shader->has_attribute_dependency = true;
  630. }
  631. if (node->has_integrator_dependency()) {
  632. current_shader->has_integrator_dependency = true;
  633. }
  634. }
  635. static TypeDesc array_typedesc(TypeDesc typedesc, int arraylength)
  636. {
  637. return TypeDesc((TypeDesc::BASETYPE)typedesc.basetype,
  638. (TypeDesc::AGGREGATE)typedesc.aggregate,
  639. (TypeDesc::VECSEMANTICS)typedesc.vecsemantics,
  640. arraylength);
  641. }
  642. void OSLCompiler::parameter(ShaderNode *node, const char *name)
  643. {
  644. ustring uname = ustring(name);
  645. const SocketType &socket = *(node->type->find_input(uname));
  646. switch (socket.type) {
  647. case SocketType::BOOLEAN: {
  648. int value = node->get_bool(socket);
  649. ss->Parameter(name, TypeDesc::TypeInt, &value);
  650. break;
  651. }
  652. case SocketType::FLOAT: {
  653. float value = node->get_float(socket);
  654. ss->Parameter(uname, TypeDesc::TypeFloat, &value);
  655. break;
  656. }
  657. case SocketType::INT: {
  658. int value = node->get_int(socket);
  659. ss->Parameter(uname, TypeDesc::TypeInt, &value);
  660. break;
  661. }
  662. case SocketType::COLOR: {
  663. float3 value = node->get_float3(socket);
  664. ss->Parameter(uname, TypeDesc::TypeColor, &value);
  665. break;
  666. }
  667. case SocketType::VECTOR: {
  668. float3 value = node->get_float3(socket);
  669. ss->Parameter(uname, TypeDesc::TypeVector, &value);
  670. break;
  671. }
  672. case SocketType::POINT: {
  673. float3 value = node->get_float3(socket);
  674. ss->Parameter(uname, TypeDesc::TypePoint, &value);
  675. break;
  676. }
  677. case SocketType::NORMAL: {
  678. float3 value = node->get_float3(socket);
  679. ss->Parameter(uname, TypeDesc::TypeNormal, &value);
  680. break;
  681. }
  682. case SocketType::POINT2: {
  683. float2 value = node->get_float2(socket);
  684. ss->Parameter(uname, TypeDesc(TypeDesc::FLOAT, TypeDesc::VEC2, TypeDesc::POINT), &value);
  685. break;
  686. }
  687. case SocketType::STRING: {
  688. ustring value = node->get_string(socket);
  689. ss->Parameter(uname, TypeDesc::TypeString, &value);
  690. break;
  691. }
  692. case SocketType::ENUM: {
  693. ustring value = node->get_string(socket);
  694. ss->Parameter(uname, TypeDesc::TypeString, &value);
  695. break;
  696. }
  697. case SocketType::TRANSFORM: {
  698. Transform value = node->get_transform(socket);
  699. ProjectionTransform projection(value);
  700. projection = projection_transpose(projection);
  701. ss->Parameter(uname, TypeDesc::TypeMatrix, &projection);
  702. break;
  703. }
  704. case SocketType::BOOLEAN_ARRAY: {
  705. // OSL does not support booleans, so convert to int
  706. const array<bool> &value = node->get_bool_array(socket);
  707. array<int> intvalue(value.size());
  708. for (size_t i = 0; i < value.size(); i++)
  709. intvalue[i] = value[i];
  710. ss->Parameter(uname, array_typedesc(TypeDesc::TypeInt, value.size()), intvalue.data());
  711. break;
  712. }
  713. case SocketType::FLOAT_ARRAY: {
  714. const array<float> &value = node->get_float_array(socket);
  715. ss->Parameter(uname, array_typedesc(TypeDesc::TypeFloat, value.size()), value.data());
  716. break;
  717. }
  718. case SocketType::INT_ARRAY: {
  719. const array<int> &value = node->get_int_array(socket);
  720. ss->Parameter(uname, array_typedesc(TypeDesc::TypeInt, value.size()), value.data());
  721. break;
  722. }
  723. case SocketType::COLOR_ARRAY:
  724. case SocketType::VECTOR_ARRAY:
  725. case SocketType::POINT_ARRAY:
  726. case SocketType::NORMAL_ARRAY: {
  727. TypeDesc typedesc;
  728. switch (socket.type) {
  729. case SocketType::COLOR_ARRAY:
  730. typedesc = TypeDesc::TypeColor;
  731. break;
  732. case SocketType::VECTOR_ARRAY:
  733. typedesc = TypeDesc::TypeVector;
  734. break;
  735. case SocketType::POINT_ARRAY:
  736. typedesc = TypeDesc::TypePoint;
  737. break;
  738. case SocketType::NORMAL_ARRAY:
  739. typedesc = TypeDesc::TypeNormal;
  740. break;
  741. default:
  742. assert(0);
  743. break;
  744. }
  745. // convert to tightly packed array since float3 has padding
  746. const array<float3> &value = node->get_float3_array(socket);
  747. array<float> fvalue(value.size() * 3);
  748. for (size_t i = 0, j = 0; i < value.size(); i++) {
  749. fvalue[j++] = value[i].x;
  750. fvalue[j++] = value[i].y;
  751. fvalue[j++] = value[i].z;
  752. }
  753. ss->Parameter(uname, array_typedesc(typedesc, value.size()), fvalue.data());
  754. break;
  755. }
  756. case SocketType::POINT2_ARRAY: {
  757. const array<float2> &value = node->get_float2_array(socket);
  758. ss->Parameter(
  759. uname,
  760. array_typedesc(TypeDesc(TypeDesc::FLOAT, TypeDesc::VEC2, TypeDesc::POINT), value.size()),
  761. value.data());
  762. break;
  763. }
  764. case SocketType::STRING_ARRAY: {
  765. const array<ustring> &value = node->get_string_array(socket);
  766. ss->Parameter(uname, array_typedesc(TypeDesc::TypeString, value.size()), value.data());
  767. break;
  768. }
  769. case SocketType::TRANSFORM_ARRAY: {
  770. const array<Transform> &value = node->get_transform_array(socket);
  771. array<ProjectionTransform> fvalue(value.size());
  772. for (size_t i = 0; i < value.size(); i++) {
  773. fvalue[i] = projection_transpose(ProjectionTransform(value[i]));
  774. }
  775. ss->Parameter(uname, array_typedesc(TypeDesc::TypeMatrix, fvalue.size()), fvalue.data());
  776. break;
  777. }
  778. case SocketType::CLOSURE:
  779. case SocketType::NODE:
  780. case SocketType::NODE_ARRAY:
  781. case SocketType::UNDEFINED:
  782. case SocketType::UINT: {
  783. assert(0);
  784. break;
  785. }
  786. }
  787. }
  788. void OSLCompiler::parameter(const char *name, float f)
  789. {
  790. ss->Parameter(name, TypeDesc::TypeFloat, &f);
  791. }
  792. void OSLCompiler::parameter_color(const char *name, float3 f)
  793. {
  794. ss->Parameter(name, TypeDesc::TypeColor, &f);
  795. }
  796. void OSLCompiler::parameter_point(const char *name, float3 f)
  797. {
  798. ss->Parameter(name, TypeDesc::TypePoint, &f);
  799. }
  800. void OSLCompiler::parameter_normal(const char *name, float3 f)
  801. {
  802. ss->Parameter(name, TypeDesc::TypeNormal, &f);
  803. }
  804. void OSLCompiler::parameter_vector(const char *name, float3 f)
  805. {
  806. ss->Parameter(name, TypeDesc::TypeVector, &f);
  807. }
  808. void OSLCompiler::parameter(const char *name, int f)
  809. {
  810. ss->Parameter(name, TypeDesc::TypeInt, &f);
  811. }
  812. void OSLCompiler::parameter(const char *name, const char *s)
  813. {
  814. ss->Parameter(name, TypeDesc::TypeString, &s);
  815. }
  816. void OSLCompiler::parameter(const char *name, ustring s)
  817. {
  818. const char *str = s.c_str();
  819. ss->Parameter(name, TypeDesc::TypeString, &str);
  820. }
  821. void OSLCompiler::parameter(const char *name, const Transform &tfm)
  822. {
  823. ProjectionTransform projection(tfm);
  824. projection = projection_transpose(projection);
  825. ss->Parameter(name, TypeDesc::TypeMatrix, (float *)&projection);
  826. }
  827. void OSLCompiler::parameter_array(const char *name, const float f[], int arraylen)
  828. {
  829. TypeDesc type = TypeDesc::TypeFloat;
  830. type.arraylen = arraylen;
  831. ss->Parameter(name, type, f);
  832. }
  833. void OSLCompiler::parameter_color_array(const char *name, const array<float3> &f)
  834. {
  835. /* NB: cycles float3 type is actually 4 floats! need to use an explicit array */
  836. array<float[3]> table(f.size());
  837. for (int i = 0; i < f.size(); ++i) {
  838. table[i][0] = f[i].x;
  839. table[i][1] = f[i].y;
  840. table[i][2] = f[i].z;
  841. }
  842. TypeDesc type = TypeDesc::TypeColor;
  843. type.arraylen = table.size();
  844. ss->Parameter(name, type, table.data());
  845. }
  846. void OSLCompiler::parameter_attribute(const char *name, ustring s)
  847. {
  848. if (Attribute::name_standard(s.c_str()))
  849. parameter(name, (string("geom:") + s.c_str()).c_str());
  850. else
  851. parameter(name, s.c_str());
  852. }
  853. void OSLCompiler::find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input)
  854. {
  855. ShaderNode *node = (input->link) ? input->link->parent : NULL;
  856. if (node != NULL && dependencies.find(node) == dependencies.end()) {
  857. foreach (ShaderInput *in, node->inputs)
  858. if (!node_skip_input(node, in))
  859. find_dependencies(dependencies, in);
  860. dependencies.insert(node);
  861. }
  862. }
  863. void OSLCompiler::generate_nodes(const ShaderNodeSet &nodes)
  864. {
  865. ShaderNodeSet done;
  866. bool nodes_done;
  867. do {
  868. nodes_done = true;
  869. foreach (ShaderNode *node, nodes) {
  870. if (done.find(node) == done.end()) {
  871. bool inputs_done = true;
  872. foreach (ShaderInput *input, node->inputs)
  873. if (!node_skip_input(node, input))
  874. if (input->link && done.find(input->link->parent) == done.end())
  875. inputs_done = false;
  876. if (inputs_done) {
  877. node->compile(*this);
  878. done.insert(node);
  879. if (current_type == SHADER_TYPE_SURFACE) {
  880. if (node->has_surface_emission())
  881. current_shader->has_surface_emission = true;
  882. if (node->has_surface_transparent())
  883. current_shader->has_surface_transparent = true;
  884. if (node->has_spatial_varying())
  885. current_shader->has_surface_spatial_varying = true;
  886. if (node->has_surface_bssrdf()) {
  887. current_shader->has_surface_bssrdf = true;
  888. if (node->has_bssrdf_bump())
  889. current_shader->has_bssrdf_bump = true;
  890. }
  891. if (node->has_bump()) {
  892. current_shader->has_bump = true;
  893. }
  894. }
  895. else if (current_type == SHADER_TYPE_VOLUME) {
  896. if (node->has_spatial_varying())
  897. current_shader->has_volume_spatial_varying = true;
  898. }
  899. }
  900. else
  901. nodes_done = false;
  902. }
  903. }
  904. } while (!nodes_done);
  905. }
  906. OSL::ShaderGroupRef OSLCompiler::compile_type(Shader *shader, ShaderGraph *graph, ShaderType type)
  907. {
  908. current_type = type;
  909. OSL::ShaderGroupRef group = ss->ShaderGroupBegin(shader->name.c_str());
  910. ShaderNode *output = graph->output();
  911. ShaderNodeSet dependencies;
  912. if (type == SHADER_TYPE_SURFACE) {
  913. /* generate surface shader */
  914. find_dependencies(dependencies, output->input("Surface"));
  915. generate_nodes(dependencies);
  916. output->compile(*this);
  917. }
  918. else if (type == SHADER_TYPE_BUMP) {
  919. /* generate bump shader */
  920. find_dependencies(dependencies, output->input("Normal"));
  921. generate_nodes(dependencies);
  922. output->compile(*this);
  923. }
  924. else if (type == SHADER_TYPE_VOLUME) {
  925. /* generate volume shader */
  926. find_dependencies(dependencies, output->input("Volume"));
  927. generate_nodes(dependencies);
  928. output->compile(*this);
  929. }
  930. else if (type == SHADER_TYPE_DISPLACEMENT) {
  931. /* generate displacement shader */
  932. find_dependencies(dependencies, output->input("Displacement"));
  933. generate_nodes(dependencies);
  934. output->compile(*this);
  935. }
  936. else
  937. assert(0);
  938. ss->ShaderGroupEnd();
  939. return group;
  940. }
  941. void OSLCompiler::compile(Scene *scene, OSLGlobals *og, Shader *shader)
  942. {
  943. if (shader->need_update) {
  944. ShaderGraph *graph = shader->graph;
  945. ShaderNode *output = (graph) ? graph->output() : NULL;
  946. bool has_bump = (shader->displacement_method != DISPLACE_TRUE) &&
  947. output->input("Surface")->link && output->input("Displacement")->link;
  948. /* finalize */
  949. shader->graph->finalize(scene,
  950. has_bump,
  951. shader->has_integrator_dependency,
  952. shader->displacement_method == DISPLACE_BOTH);
  953. current_shader = shader;
  954. shader->has_surface = false;
  955. shader->has_surface_emission = false;
  956. shader->has_surface_transparent = false;
  957. shader->has_surface_bssrdf = false;
  958. shader->has_bump = has_bump;
  959. shader->has_bssrdf_bump = has_bump;
  960. shader->has_volume = false;
  961. shader->has_displacement = false;
  962. shader->has_surface_spatial_varying = false;
  963. shader->has_volume_spatial_varying = false;
  964. shader->has_object_dependency = false;
  965. shader->has_attribute_dependency = false;
  966. shader->has_integrator_dependency = false;
  967. /* generate surface shader */
  968. if (shader->used && graph && output->input("Surface")->link) {
  969. shader->osl_surface_ref = compile_type(shader, shader->graph, SHADER_TYPE_SURFACE);
  970. if (has_bump)
  971. shader->osl_surface_bump_ref = compile_type(shader, shader->graph, SHADER_TYPE_BUMP);
  972. else
  973. shader->osl_surface_bump_ref = OSL::ShaderGroupRef();
  974. shader->has_surface = true;
  975. }
  976. else {
  977. shader->osl_surface_ref = OSL::ShaderGroupRef();
  978. shader->osl_surface_bump_ref = OSL::ShaderGroupRef();
  979. }
  980. /* generate volume shader */
  981. if (shader->used && graph && output->input("Volume")->link) {
  982. shader->osl_volume_ref = compile_type(shader, shader->graph, SHADER_TYPE_VOLUME);
  983. shader->has_volume = true;
  984. }
  985. else
  986. shader->osl_volume_ref = OSL::ShaderGroupRef();
  987. /* generate displacement shader */
  988. if (shader->used && graph && output->input("Displacement")->link) {
  989. shader->osl_displacement_ref = compile_type(shader, shader->graph, SHADER_TYPE_DISPLACEMENT);
  990. shader->has_displacement = true;
  991. }
  992. else
  993. shader->osl_displacement_ref = OSL::ShaderGroupRef();
  994. }
  995. /* push state to array for lookup */
  996. og->surface_state.push_back(shader->osl_surface_ref);
  997. og->volume_state.push_back(shader->osl_volume_ref);
  998. og->displacement_state.push_back(shader->osl_displacement_ref);
  999. og->bump_state.push_back(shader->osl_surface_bump_ref);
  1000. }
  1001. void OSLCompiler::parameter_texture(const char *name, ustring filename, ustring colorspace)
  1002. {
  1003. /* Textured loaded through the OpenImageIO texture cache. For this
  1004. * case we need to do runtime color space conversion. */
  1005. OSLTextureHandle *handle = new OSLTextureHandle(OSLTextureHandle::OIIO);
  1006. handle->processor = ColorSpaceManager::get_processor(colorspace);
  1007. services->textures.insert(filename, handle);
  1008. parameter(name, filename);
  1009. }
  1010. void OSLCompiler::parameter_texture(const char *name, int svm_slot)
  1011. {
  1012. /* Texture loaded through SVM image texture system. We generate a unique
  1013. * name, which ends up being used in OSLRenderServices::get_texture_handle
  1014. * to get handle again. Note that this name must be unique between multiple
  1015. * render sessions as the render services are shared. */
  1016. ustring filename(string_printf("@svm%d", texture_shared_unique_id++).c_str());
  1017. services->textures.insert(filename, new OSLTextureHandle(OSLTextureHandle::SVM, svm_slot));
  1018. parameter(name, filename);
  1019. }
  1020. void OSLCompiler::parameter_texture_ies(const char *name, int svm_slot)
  1021. {
  1022. /* IES light textures stored in SVM. */
  1023. ustring filename(string_printf("@svm%d", texture_shared_unique_id++).c_str());
  1024. services->textures.insert(filename, new OSLTextureHandle(OSLTextureHandle::IES, svm_slot));
  1025. parameter(name, filename);
  1026. }
  1027. #else
  1028. void OSLCompiler::add(ShaderNode * /*node*/, const char * /*name*/, bool /*isfilepath*/)
  1029. {
  1030. }
  1031. void OSLCompiler::parameter(ShaderNode * /*node*/, const char * /*name*/)
  1032. {
  1033. }
  1034. void OSLCompiler::parameter(const char * /*name*/, float /*f*/)
  1035. {
  1036. }
  1037. void OSLCompiler::parameter_color(const char * /*name*/, float3 /*f*/)
  1038. {
  1039. }
  1040. void OSLCompiler::parameter_vector(const char * /*name*/, float3 /*f*/)
  1041. {
  1042. }
  1043. void OSLCompiler::parameter_point(const char * /*name*/, float3 /*f*/)
  1044. {
  1045. }
  1046. void OSLCompiler::parameter_normal(const char * /*name*/, float3 /*f*/)
  1047. {
  1048. }
  1049. void OSLCompiler::parameter(const char * /*name*/, int /*f*/)
  1050. {
  1051. }
  1052. void OSLCompiler::parameter(const char * /*name*/, const char * /*s*/)
  1053. {
  1054. }
  1055. void OSLCompiler::parameter(const char * /*name*/, ustring /*s*/)
  1056. {
  1057. }
  1058. void OSLCompiler::parameter(const char * /*name*/, const Transform & /*tfm*/)
  1059. {
  1060. }
  1061. void OSLCompiler::parameter_array(const char * /*name*/, const float /*f*/[], int /*arraylen*/)
  1062. {
  1063. }
  1064. void OSLCompiler::parameter_color_array(const char * /*name*/, const array<float3> & /*f*/)
  1065. {
  1066. }
  1067. void OSLCompiler::parameter_texture(const char * /* name */,
  1068. ustring /* filename */,
  1069. ustring /* colorspace */)
  1070. {
  1071. }
  1072. void OSLCompiler::parameter_texture(const char * /* name */, int /* svm_slot */)
  1073. {
  1074. }
  1075. void OSLCompiler::parameter_texture_ies(const char * /* name */, int /* svm_slot */)
  1076. {
  1077. }
  1078. #endif /* WITH_OSL */
  1079. CCL_NAMESPACE_END