shader.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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/background.h"
  18. #include "render/camera.h"
  19. #include "render/colorspace.h"
  20. #include "render/graph.h"
  21. #include "render/integrator.h"
  22. #include "render/light.h"
  23. #include "render/mesh.h"
  24. #include "render/nodes.h"
  25. #include "render/object.h"
  26. #include "render/osl.h"
  27. #include "render/scene.h"
  28. #include "render/shader.h"
  29. #include "render/svm.h"
  30. #include "render/tables.h"
  31. #include "util/util_foreach.h"
  32. #include "util/util_murmurhash.h"
  33. #ifdef WITH_OCIO
  34. # include <OpenColorIO/OpenColorIO.h>
  35. namespace OCIO = OCIO_NAMESPACE;
  36. #endif
  37. CCL_NAMESPACE_BEGIN
  38. thread_mutex ShaderManager::lookup_table_mutex;
  39. vector<float> ShaderManager::beckmann_table;
  40. bool ShaderManager::beckmann_table_ready = false;
  41. /* Beckmann sampling precomputed table, see bsdf_microfacet.h */
  42. /* 2D slope distribution (alpha = 1.0) */
  43. static float beckmann_table_P22(const float slope_x, const float slope_y)
  44. {
  45. return expf(-(slope_x * slope_x + slope_y * slope_y));
  46. }
  47. /* maximal slope amplitude (range that contains 99.99% of the distribution) */
  48. static float beckmann_table_slope_max()
  49. {
  50. return 6.0;
  51. }
  52. /* MSVC 2015 needs this ugly hack to prevent a codegen bug on x86
  53. * see T50176 for details
  54. */
  55. #if defined(_MSC_VER) && (_MSC_VER == 1900)
  56. # define MSVC_VOLATILE volatile
  57. #else
  58. # define MSVC_VOLATILE
  59. #endif
  60. /* Paper used: Importance Sampling Microfacet-Based BSDFs with the
  61. * Distribution of Visible Normals. Supplemental Material 2/2.
  62. *
  63. * http://hal.inria.fr/docs/01/00/66/20/ANNEX/supplemental2.pdf
  64. */
  65. static void beckmann_table_rows(float *table, int row_from, int row_to)
  66. {
  67. /* allocate temporary data */
  68. const int DATA_TMP_SIZE = 512;
  69. vector<double> slope_x(DATA_TMP_SIZE);
  70. vector<double> CDF_P22_omega_i(DATA_TMP_SIZE);
  71. /* loop over incident directions */
  72. for (int index_theta = row_from; index_theta < row_to; index_theta++) {
  73. /* incident vector */
  74. const float cos_theta = index_theta / (BECKMANN_TABLE_SIZE - 1.0f);
  75. const float sin_theta = safe_sqrtf(1.0f - cos_theta * cos_theta);
  76. /* for a given incident vector
  77. * integrate P22_{omega_i}(x_slope, 1, 1), Eq. (10) */
  78. slope_x[0] = (double)-beckmann_table_slope_max();
  79. CDF_P22_omega_i[0] = 0;
  80. for (MSVC_VOLATILE int index_slope_x = 1; index_slope_x < DATA_TMP_SIZE; ++index_slope_x) {
  81. /* slope_x */
  82. slope_x[index_slope_x] = (double)(-beckmann_table_slope_max() +
  83. 2.0f * beckmann_table_slope_max() * index_slope_x /
  84. (DATA_TMP_SIZE - 1.0f));
  85. /* dot product with incident vector */
  86. float dot_product = fmaxf(0.0f, -(float)slope_x[index_slope_x] * sin_theta + cos_theta);
  87. /* marginalize P22_{omega_i}(x_slope, 1, 1), Eq. (10) */
  88. float P22_omega_i = 0.0f;
  89. for (int j = 0; j < 100; ++j) {
  90. float slope_y = -beckmann_table_slope_max() +
  91. 2.0f * beckmann_table_slope_max() * j * (1.0f / 99.0f);
  92. P22_omega_i += dot_product * beckmann_table_P22((float)slope_x[index_slope_x], slope_y);
  93. }
  94. /* CDF of P22_{omega_i}(x_slope, 1, 1), Eq. (10) */
  95. CDF_P22_omega_i[index_slope_x] = CDF_P22_omega_i[index_slope_x - 1] + (double)P22_omega_i;
  96. }
  97. /* renormalize CDF_P22_omega_i */
  98. for (int index_slope_x = 1; index_slope_x < DATA_TMP_SIZE; ++index_slope_x)
  99. CDF_P22_omega_i[index_slope_x] /= CDF_P22_omega_i[DATA_TMP_SIZE - 1];
  100. /* loop over random number U1 */
  101. int index_slope_x = 0;
  102. for (int index_U = 0; index_U < BECKMANN_TABLE_SIZE; ++index_U) {
  103. const double U = 0.0000001 + 0.9999998 * index_U / (double)(BECKMANN_TABLE_SIZE - 1);
  104. /* inverse CDF_P22_omega_i, solve Eq.(11) */
  105. while (CDF_P22_omega_i[index_slope_x] <= U)
  106. ++index_slope_x;
  107. const double interp = (CDF_P22_omega_i[index_slope_x] - U) /
  108. (CDF_P22_omega_i[index_slope_x] - CDF_P22_omega_i[index_slope_x - 1]);
  109. /* store value */
  110. table[index_U + index_theta * BECKMANN_TABLE_SIZE] =
  111. (float)(interp * slope_x[index_slope_x - 1] + (1.0 - interp) * slope_x[index_slope_x]);
  112. }
  113. }
  114. }
  115. #undef MSVC_VOLATILE
  116. static void beckmann_table_build(vector<float> &table)
  117. {
  118. table.resize(BECKMANN_TABLE_SIZE * BECKMANN_TABLE_SIZE);
  119. /* multithreaded build */
  120. TaskPool pool;
  121. for (int i = 0; i < BECKMANN_TABLE_SIZE; i += 8)
  122. pool.push(function_bind(&beckmann_table_rows, &table[0], i, i + 8));
  123. pool.wait_work();
  124. }
  125. /* Shader */
  126. NODE_DEFINE(Shader)
  127. {
  128. NodeType *type = NodeType::add("shader", create);
  129. SOCKET_BOOLEAN(use_mis, "Use MIS", true);
  130. SOCKET_BOOLEAN(use_transparent_shadow, "Use Transparent Shadow", true);
  131. SOCKET_BOOLEAN(heterogeneous_volume, "Heterogeneous Volume", true);
  132. static NodeEnum volume_sampling_method_enum;
  133. volume_sampling_method_enum.insert("distance", VOLUME_SAMPLING_DISTANCE);
  134. volume_sampling_method_enum.insert("equiangular", VOLUME_SAMPLING_EQUIANGULAR);
  135. volume_sampling_method_enum.insert("multiple_importance", VOLUME_SAMPLING_MULTIPLE_IMPORTANCE);
  136. SOCKET_ENUM(volume_sampling_method,
  137. "Volume Sampling Method",
  138. volume_sampling_method_enum,
  139. VOLUME_SAMPLING_DISTANCE);
  140. static NodeEnum volume_interpolation_method_enum;
  141. volume_interpolation_method_enum.insert("linear", VOLUME_INTERPOLATION_LINEAR);
  142. volume_interpolation_method_enum.insert("cubic", VOLUME_INTERPOLATION_CUBIC);
  143. SOCKET_ENUM(volume_interpolation_method,
  144. "Volume Interpolation Method",
  145. volume_interpolation_method_enum,
  146. VOLUME_INTERPOLATION_LINEAR);
  147. static NodeEnum displacement_method_enum;
  148. displacement_method_enum.insert("bump", DISPLACE_BUMP);
  149. displacement_method_enum.insert("true", DISPLACE_TRUE);
  150. displacement_method_enum.insert("both", DISPLACE_BOTH);
  151. SOCKET_ENUM(displacement_method, "Displacement Method", displacement_method_enum, DISPLACE_BUMP);
  152. return type;
  153. }
  154. Shader::Shader() : Node(node_type)
  155. {
  156. pass_id = 0;
  157. graph = NULL;
  158. has_surface = false;
  159. has_surface_transparent = false;
  160. has_surface_emission = false;
  161. has_surface_bssrdf = false;
  162. has_volume = false;
  163. has_displacement = false;
  164. has_bump = false;
  165. has_bssrdf_bump = false;
  166. has_surface_spatial_varying = false;
  167. has_volume_spatial_varying = false;
  168. has_object_dependency = false;
  169. has_attribute_dependency = false;
  170. has_integrator_dependency = false;
  171. has_volume_connected = false;
  172. displacement_method = DISPLACE_BUMP;
  173. id = -1;
  174. used = false;
  175. need_update = true;
  176. need_update_mesh = true;
  177. need_sync_object = false;
  178. }
  179. Shader::~Shader()
  180. {
  181. delete graph;
  182. }
  183. bool Shader::is_constant_emission(float3 *emission)
  184. {
  185. ShaderInput *surf = graph->output()->input("Surface");
  186. if (surf->link == NULL) {
  187. return false;
  188. }
  189. if (surf->link->parent->type == EmissionNode::node_type) {
  190. EmissionNode *node = (EmissionNode *)surf->link->parent;
  191. assert(node->input("Color"));
  192. assert(node->input("Strength"));
  193. if (node->input("Color")->link || node->input("Strength")->link) {
  194. return false;
  195. }
  196. *emission = node->color * node->strength;
  197. }
  198. else if (surf->link->parent->type == BackgroundNode::node_type) {
  199. BackgroundNode *node = (BackgroundNode *)surf->link->parent;
  200. assert(node->input("Color"));
  201. assert(node->input("Strength"));
  202. if (node->input("Color")->link || node->input("Strength")->link) {
  203. return false;
  204. }
  205. *emission = node->color * node->strength;
  206. }
  207. else {
  208. return false;
  209. }
  210. return true;
  211. }
  212. void Shader::set_graph(ShaderGraph *graph_)
  213. {
  214. /* do this here already so that we can detect if mesh or object attributes
  215. * are needed, since the node attribute callbacks check if their sockets
  216. * are connected but proxy nodes should not count */
  217. if (graph_) {
  218. graph_->remove_proxy_nodes();
  219. if (displacement_method != DISPLACE_BUMP) {
  220. graph_->compute_displacement_hash();
  221. }
  222. }
  223. /* update geometry if displacement changed */
  224. if (displacement_method != DISPLACE_BUMP) {
  225. const char *old_hash = (graph) ? graph->displacement_hash.c_str() : "";
  226. const char *new_hash = (graph_) ? graph_->displacement_hash.c_str() : "";
  227. if (strcmp(old_hash, new_hash) != 0) {
  228. need_update_mesh = true;
  229. }
  230. }
  231. /* assign graph */
  232. delete graph;
  233. graph = graph_;
  234. /* Store info here before graph optimization to make sure that
  235. * nodes that get optimized away still count. */
  236. has_volume_connected = (graph->output()->input("Volume")->link != NULL);
  237. }
  238. void Shader::tag_update(Scene *scene)
  239. {
  240. /* update tag */
  241. need_update = true;
  242. scene->shader_manager->need_update = true;
  243. /* if the shader previously was emissive, update light distribution,
  244. * if the new shader is emissive, a light manager update tag will be
  245. * done in the shader manager device update. */
  246. if (use_mis && has_surface_emission)
  247. scene->light_manager->need_update = true;
  248. /* Special handle of background MIS light for now: for some reason it
  249. * has use_mis set to false. We are quite close to release now, so
  250. * better to be safe.
  251. */
  252. if (this == scene->default_background && scene->light_manager->has_background_light(scene)) {
  253. scene->light_manager->need_update = true;
  254. }
  255. /* quick detection of which kind of shaders we have to avoid loading
  256. * e.g. surface attributes when there is only a volume shader. this could
  257. * be more fine grained but it's better than nothing */
  258. OutputNode *output = graph->output();
  259. bool prev_has_volume = has_volume;
  260. has_surface = has_surface || output->input("Surface")->link;
  261. has_volume = has_volume || output->input("Volume")->link;
  262. has_displacement = has_displacement || output->input("Displacement")->link;
  263. /* get requested attributes. this could be optimized by pruning unused
  264. * nodes here already, but that's the job of the shader manager currently,
  265. * and may not be so great for interactive rendering where you temporarily
  266. * disconnect a node */
  267. AttributeRequestSet prev_attributes = attributes;
  268. attributes.clear();
  269. foreach (ShaderNode *node, graph->nodes)
  270. node->attributes(this, &attributes);
  271. if (has_displacement && displacement_method == DISPLACE_BOTH) {
  272. attributes.add(ATTR_STD_POSITION_UNDISPLACED);
  273. }
  274. /* compare if the attributes changed, mesh manager will check
  275. * need_update_mesh, update the relevant meshes and clear it. */
  276. if (attributes.modified(prev_attributes)) {
  277. need_update_mesh = true;
  278. scene->mesh_manager->need_update = true;
  279. }
  280. if (has_volume != prev_has_volume) {
  281. scene->mesh_manager->need_flags_update = true;
  282. scene->object_manager->need_flags_update = true;
  283. }
  284. }
  285. void Shader::tag_used(Scene *scene)
  286. {
  287. /* if an unused shader suddenly gets used somewhere, it needs to be
  288. * recompiled because it was skipped for compilation before */
  289. if (!used) {
  290. need_update = true;
  291. scene->shader_manager->need_update = true;
  292. }
  293. }
  294. /* Shader Manager */
  295. ShaderManager::ShaderManager()
  296. {
  297. need_update = true;
  298. beckmann_table_offset = TABLE_OFFSET_INVALID;
  299. xyz_to_r = make_float3(3.2404542f, -1.5371385f, -0.4985314f);
  300. xyz_to_g = make_float3(-0.9692660f, 1.8760108f, 0.0415560f);
  301. xyz_to_b = make_float3(0.0556434f, -0.2040259f, 1.0572252f);
  302. rgb_to_y = make_float3(0.2126729f, 0.7151522f, 0.0721750f);
  303. #ifdef WITH_OCIO
  304. OCIO::ConstConfigRcPtr config = OCIO::GetCurrentConfig();
  305. if (config) {
  306. if (config->hasRole("XYZ") && config->hasRole("scene_linear")) {
  307. OCIO::ConstProcessorRcPtr to_rgb_processor = config->getProcessor("XYZ", "scene_linear");
  308. OCIO::ConstProcessorRcPtr to_xyz_processor = config->getProcessor("scene_linear", "XYZ");
  309. if (to_rgb_processor && to_xyz_processor) {
  310. float r[] = {1.0f, 0.0f, 0.0f};
  311. float g[] = {0.0f, 1.0f, 0.0f};
  312. float b[] = {0.0f, 0.0f, 1.0f};
  313. to_xyz_processor->applyRGB(r);
  314. to_xyz_processor->applyRGB(g);
  315. to_xyz_processor->applyRGB(b);
  316. rgb_to_y = make_float3(r[1], g[1], b[1]);
  317. float x[] = {1.0f, 0.0f, 0.0f};
  318. float y[] = {0.0f, 1.0f, 0.0f};
  319. float z[] = {0.0f, 0.0f, 1.0f};
  320. to_rgb_processor->applyRGB(x);
  321. to_rgb_processor->applyRGB(y);
  322. to_rgb_processor->applyRGB(z);
  323. xyz_to_r = make_float3(x[0], y[0], z[0]);
  324. xyz_to_g = make_float3(x[1], y[1], z[1]);
  325. xyz_to_b = make_float3(x[2], y[2], z[2]);
  326. }
  327. }
  328. }
  329. #endif
  330. }
  331. ShaderManager::~ShaderManager()
  332. {
  333. }
  334. ShaderManager *ShaderManager::create(Scene *scene, int shadingsystem)
  335. {
  336. ShaderManager *manager;
  337. (void)shadingsystem; /* Ignored when built without OSL. */
  338. #ifdef WITH_OSL
  339. if (shadingsystem == SHADINGSYSTEM_OSL) {
  340. manager = new OSLShaderManager();
  341. }
  342. else
  343. #endif
  344. {
  345. manager = new SVMShaderManager();
  346. }
  347. add_default(scene);
  348. return manager;
  349. }
  350. uint ShaderManager::get_attribute_id(ustring name)
  351. {
  352. thread_scoped_spin_lock lock(attribute_lock_);
  353. /* get a unique id for each name, for SVM attribute lookup */
  354. AttributeIDMap::iterator it = unique_attribute_id.find(name);
  355. if (it != unique_attribute_id.end())
  356. return it->second;
  357. uint id = (uint)ATTR_STD_NUM + unique_attribute_id.size();
  358. unique_attribute_id[name] = id;
  359. return id;
  360. }
  361. uint ShaderManager::get_attribute_id(AttributeStandard std)
  362. {
  363. return (uint)std;
  364. }
  365. int ShaderManager::get_shader_id(Shader *shader, bool smooth)
  366. {
  367. /* get a shader id to pass to the kernel */
  368. int id = shader->id;
  369. /* smooth flag */
  370. if (smooth)
  371. id |= SHADER_SMOOTH_NORMAL;
  372. /* default flags */
  373. id |= SHADER_CAST_SHADOW | SHADER_AREA_LIGHT;
  374. return id;
  375. }
  376. void ShaderManager::device_update_shaders_used(Scene *scene)
  377. {
  378. /* figure out which shaders are in use, so SVM/OSL can skip compiling them
  379. * for speed and avoid loading image textures into memory */
  380. uint id = 0;
  381. foreach (Shader *shader, scene->shaders) {
  382. shader->used = false;
  383. shader->id = id++;
  384. }
  385. scene->default_surface->used = true;
  386. scene->default_light->used = true;
  387. scene->default_background->used = true;
  388. scene->default_empty->used = true;
  389. if (scene->background->shader)
  390. scene->background->shader->used = true;
  391. foreach (Mesh *mesh, scene->meshes)
  392. foreach (Shader *shader, mesh->used_shaders)
  393. shader->used = true;
  394. foreach (Light *light, scene->lights)
  395. if (light->shader)
  396. light->shader->used = true;
  397. }
  398. void ShaderManager::device_update_common(Device *device,
  399. DeviceScene *dscene,
  400. Scene *scene,
  401. Progress & /*progress*/)
  402. {
  403. dscene->shaders.free();
  404. if (scene->shaders.size() == 0)
  405. return;
  406. KernelShader *kshader = dscene->shaders.alloc(scene->shaders.size());
  407. bool has_volumes = false;
  408. bool has_transparent_shadow = false;
  409. foreach (Shader *shader, scene->shaders) {
  410. uint flag = 0;
  411. if (shader->use_mis)
  412. flag |= SD_USE_MIS;
  413. if (shader->has_surface_transparent && shader->use_transparent_shadow)
  414. flag |= SD_HAS_TRANSPARENT_SHADOW;
  415. if (shader->has_volume) {
  416. flag |= SD_HAS_VOLUME;
  417. has_volumes = true;
  418. /* todo: this could check more fine grained, to skip useless volumes
  419. * enclosed inside an opaque bsdf.
  420. */
  421. flag |= SD_HAS_TRANSPARENT_SHADOW;
  422. }
  423. /* in this case we can assume transparent surface */
  424. if (shader->has_volume_connected && !shader->has_surface)
  425. flag |= SD_HAS_ONLY_VOLUME;
  426. if (shader->heterogeneous_volume && shader->has_volume_spatial_varying)
  427. flag |= SD_HETEROGENEOUS_VOLUME;
  428. if (shader->has_attribute_dependency)
  429. flag |= SD_NEED_ATTRIBUTES;
  430. if (shader->has_bssrdf_bump)
  431. flag |= SD_HAS_BSSRDF_BUMP;
  432. if (device->info.has_volume_decoupled) {
  433. if (shader->volume_sampling_method == VOLUME_SAMPLING_EQUIANGULAR)
  434. flag |= SD_VOLUME_EQUIANGULAR;
  435. if (shader->volume_sampling_method == VOLUME_SAMPLING_MULTIPLE_IMPORTANCE)
  436. flag |= SD_VOLUME_MIS;
  437. }
  438. if (shader->volume_interpolation_method == VOLUME_INTERPOLATION_CUBIC)
  439. flag |= SD_VOLUME_CUBIC;
  440. if (shader->has_bump)
  441. flag |= SD_HAS_BUMP;
  442. if (shader->displacement_method != DISPLACE_BUMP)
  443. flag |= SD_HAS_DISPLACEMENT;
  444. /* constant emission check */
  445. float3 constant_emission = make_float3(0.0f, 0.0f, 0.0f);
  446. if (shader->is_constant_emission(&constant_emission))
  447. flag |= SD_HAS_CONSTANT_EMISSION;
  448. uint32_t cryptomatte_id = util_murmur_hash3(shader->name.c_str(), shader->name.length(), 0);
  449. /* regular shader */
  450. kshader->flags = flag;
  451. kshader->pass_id = shader->pass_id;
  452. kshader->constant_emission[0] = constant_emission.x;
  453. kshader->constant_emission[1] = constant_emission.y;
  454. kshader->constant_emission[2] = constant_emission.z;
  455. kshader->cryptomatte_id = util_hash_to_float(cryptomatte_id);
  456. kshader++;
  457. has_transparent_shadow |= (flag & SD_HAS_TRANSPARENT_SHADOW) != 0;
  458. }
  459. dscene->shaders.copy_to_device();
  460. /* lookup tables */
  461. KernelTables *ktables = &dscene->data.tables;
  462. /* beckmann lookup table */
  463. if (beckmann_table_offset == TABLE_OFFSET_INVALID) {
  464. if (!beckmann_table_ready) {
  465. thread_scoped_lock lock(lookup_table_mutex);
  466. if (!beckmann_table_ready) {
  467. beckmann_table_build(beckmann_table);
  468. beckmann_table_ready = true;
  469. }
  470. }
  471. beckmann_table_offset = scene->lookup_tables->add_table(dscene, beckmann_table);
  472. }
  473. ktables->beckmann_offset = (int)beckmann_table_offset;
  474. /* integrator */
  475. KernelIntegrator *kintegrator = &dscene->data.integrator;
  476. kintegrator->use_volumes = has_volumes;
  477. /* TODO(sergey): De-duplicate with flags set in integrator.cpp. */
  478. kintegrator->transparent_shadows = has_transparent_shadow;
  479. /* film */
  480. KernelFilm *kfilm = &dscene->data.film;
  481. /* color space, needs to be here because e.g. displacement shaders could depend on it */
  482. kfilm->xyz_to_r = float3_to_float4(xyz_to_r);
  483. kfilm->xyz_to_g = float3_to_float4(xyz_to_g);
  484. kfilm->xyz_to_b = float3_to_float4(xyz_to_b);
  485. kfilm->rgb_to_y = float3_to_float4(rgb_to_y);
  486. }
  487. void ShaderManager::device_free_common(Device *, DeviceScene *dscene, Scene *scene)
  488. {
  489. scene->lookup_tables->remove_table(&beckmann_table_offset);
  490. dscene->shaders.free();
  491. }
  492. void ShaderManager::add_default(Scene *scene)
  493. {
  494. /* default surface */
  495. {
  496. ShaderGraph *graph = new ShaderGraph();
  497. DiffuseBsdfNode *diffuse = new DiffuseBsdfNode();
  498. diffuse->color = make_float3(0.8f, 0.8f, 0.8f);
  499. graph->add(diffuse);
  500. graph->connect(diffuse->output("BSDF"), graph->output()->input("Surface"));
  501. Shader *shader = new Shader();
  502. shader->name = "default_surface";
  503. shader->graph = graph;
  504. scene->shaders.push_back(shader);
  505. scene->default_surface = shader;
  506. }
  507. /* default light */
  508. {
  509. ShaderGraph *graph = new ShaderGraph();
  510. EmissionNode *emission = new EmissionNode();
  511. emission->color = make_float3(0.8f, 0.8f, 0.8f);
  512. emission->strength = 0.0f;
  513. graph->add(emission);
  514. graph->connect(emission->output("Emission"), graph->output()->input("Surface"));
  515. Shader *shader = new Shader();
  516. shader->name = "default_light";
  517. shader->graph = graph;
  518. scene->shaders.push_back(shader);
  519. scene->default_light = shader;
  520. }
  521. /* default background */
  522. {
  523. ShaderGraph *graph = new ShaderGraph();
  524. Shader *shader = new Shader();
  525. shader->name = "default_background";
  526. shader->graph = graph;
  527. scene->shaders.push_back(shader);
  528. scene->default_background = shader;
  529. }
  530. /* default empty */
  531. {
  532. ShaderGraph *graph = new ShaderGraph();
  533. Shader *shader = new Shader();
  534. shader->name = "default_empty";
  535. shader->graph = graph;
  536. scene->shaders.push_back(shader);
  537. scene->default_empty = shader;
  538. }
  539. }
  540. void ShaderManager::get_requested_graph_features(ShaderGraph *graph,
  541. DeviceRequestedFeatures *requested_features)
  542. {
  543. foreach (ShaderNode *node, graph->nodes) {
  544. requested_features->max_nodes_group = max(requested_features->max_nodes_group,
  545. node->get_group());
  546. requested_features->nodes_features |= node->get_feature();
  547. if (node->special_type == SHADER_SPECIAL_TYPE_CLOSURE) {
  548. BsdfBaseNode *bsdf_node = static_cast<BsdfBaseNode *>(node);
  549. if (CLOSURE_IS_VOLUME(bsdf_node->closure)) {
  550. requested_features->nodes_features |= NODE_FEATURE_VOLUME;
  551. }
  552. else if (CLOSURE_IS_PRINCIPLED(bsdf_node->closure)) {
  553. requested_features->use_principled = true;
  554. }
  555. }
  556. if (node->has_surface_bssrdf()) {
  557. requested_features->use_subsurface = true;
  558. }
  559. if (node->has_surface_transparent()) {
  560. requested_features->use_transparent = true;
  561. }
  562. if (node->has_raytrace()) {
  563. requested_features->use_shader_raytrace = true;
  564. }
  565. }
  566. }
  567. void ShaderManager::get_requested_features(Scene *scene,
  568. DeviceRequestedFeatures *requested_features)
  569. {
  570. requested_features->max_nodes_group = NODE_GROUP_LEVEL_0;
  571. requested_features->nodes_features = 0;
  572. for (int i = 0; i < scene->shaders.size(); i++) {
  573. Shader *shader = scene->shaders[i];
  574. /* Gather requested features from all the nodes from the graph nodes. */
  575. get_requested_graph_features(shader->graph, requested_features);
  576. ShaderNode *output_node = shader->graph->output();
  577. if (output_node->input("Displacement")->link != NULL) {
  578. requested_features->nodes_features |= NODE_FEATURE_BUMP;
  579. if (shader->displacement_method == DISPLACE_BOTH) {
  580. requested_features->nodes_features |= NODE_FEATURE_BUMP_STATE;
  581. }
  582. }
  583. /* On top of volume nodes, also check if we need volume sampling because
  584. * e.g. an Emission node would slip through the NODE_FEATURE_VOLUME check */
  585. if (shader->has_volume)
  586. requested_features->use_volume |= true;
  587. }
  588. }
  589. void ShaderManager::free_memory()
  590. {
  591. beckmann_table.free_memory();
  592. #ifdef WITH_OSL
  593. OSLShaderManager::free_memory();
  594. #endif
  595. ColorSpaceManager::free_memory();
  596. }
  597. float ShaderManager::linear_rgb_to_gray(float3 c)
  598. {
  599. return dot(c, rgb_to_y);
  600. }
  601. string ShaderManager::get_cryptomatte_materials(Scene *scene)
  602. {
  603. string manifest = "{";
  604. unordered_set<ustring, ustringHash> materials;
  605. foreach (Shader *shader, scene->shaders) {
  606. if (materials.count(shader->name)) {
  607. continue;
  608. }
  609. materials.insert(shader->name);
  610. uint32_t cryptomatte_id = util_murmur_hash3(shader->name.c_str(), shader->name.length(), 0);
  611. manifest += string_printf("\"%s\":\"%08x\",", shader->name.c_str(), cryptomatte_id);
  612. }
  613. manifest[manifest.size() - 1] = '}';
  614. return manifest;
  615. }
  616. CCL_NAMESPACE_END