film.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 "render/camera.h"
  17. #include "device/device.h"
  18. #include "render/film.h"
  19. #include "render/integrator.h"
  20. #include "render/mesh.h"
  21. #include "render/scene.h"
  22. #include "render/tables.h"
  23. #include "util/util_algorithm.h"
  24. #include "util/util_foreach.h"
  25. #include "util/util_math.h"
  26. #include "util/util_math_cdf.h"
  27. CCL_NAMESPACE_BEGIN
  28. /* Pass */
  29. static bool compare_pass_order(const Pass &a, const Pass &b)
  30. {
  31. if (a.components == b.components)
  32. return (a.type < b.type);
  33. return (a.components > b.components);
  34. }
  35. void Pass::add(PassType type, vector<Pass> &passes, const char *name)
  36. {
  37. for (size_t i = 0; i < passes.size(); i++) {
  38. if (passes[i].type == type && (name ? (passes[i].name == name) : passes[i].name.empty())) {
  39. return;
  40. }
  41. }
  42. Pass pass;
  43. pass.type = type;
  44. pass.filter = true;
  45. pass.exposure = false;
  46. pass.divide_type = PASS_NONE;
  47. if (name) {
  48. pass.name = name;
  49. }
  50. switch (type) {
  51. case PASS_NONE:
  52. pass.components = 0;
  53. break;
  54. case PASS_COMBINED:
  55. pass.components = 4;
  56. pass.exposure = true;
  57. break;
  58. case PASS_DEPTH:
  59. pass.components = 1;
  60. pass.filter = false;
  61. break;
  62. case PASS_MIST:
  63. pass.components = 1;
  64. break;
  65. case PASS_NORMAL:
  66. pass.components = 4;
  67. break;
  68. case PASS_UV:
  69. pass.components = 4;
  70. break;
  71. case PASS_MOTION:
  72. pass.components = 4;
  73. pass.divide_type = PASS_MOTION_WEIGHT;
  74. break;
  75. case PASS_MOTION_WEIGHT:
  76. pass.components = 1;
  77. break;
  78. case PASS_OBJECT_ID:
  79. case PASS_MATERIAL_ID:
  80. pass.components = 1;
  81. pass.filter = false;
  82. break;
  83. case PASS_EMISSION:
  84. case PASS_BACKGROUND:
  85. pass.components = 4;
  86. pass.exposure = true;
  87. break;
  88. case PASS_AO:
  89. pass.components = 4;
  90. break;
  91. case PASS_SHADOW:
  92. pass.components = 4;
  93. pass.exposure = false;
  94. break;
  95. case PASS_LIGHT:
  96. /* This isn't a real pass, used by baking to see whether
  97. * light data is needed or not.
  98. *
  99. * Set components to 0 so pass sort below happens in a
  100. * determined way.
  101. */
  102. pass.components = 0;
  103. break;
  104. #ifdef WITH_CYCLES_DEBUG
  105. case PASS_BVH_TRAVERSED_NODES:
  106. case PASS_BVH_TRAVERSED_INSTANCES:
  107. case PASS_BVH_INTERSECTIONS:
  108. case PASS_RAY_BOUNCES:
  109. pass.components = 1;
  110. pass.exposure = false;
  111. break;
  112. #endif
  113. case PASS_RENDER_TIME:
  114. /* This pass is handled entirely on the host side. */
  115. pass.components = 0;
  116. break;
  117. case PASS_DIFFUSE_COLOR:
  118. case PASS_GLOSSY_COLOR:
  119. case PASS_TRANSMISSION_COLOR:
  120. case PASS_SUBSURFACE_COLOR:
  121. pass.components = 4;
  122. break;
  123. case PASS_DIFFUSE_DIRECT:
  124. case PASS_DIFFUSE_INDIRECT:
  125. pass.components = 4;
  126. pass.exposure = true;
  127. pass.divide_type = PASS_DIFFUSE_COLOR;
  128. break;
  129. case PASS_GLOSSY_DIRECT:
  130. case PASS_GLOSSY_INDIRECT:
  131. pass.components = 4;
  132. pass.exposure = true;
  133. pass.divide_type = PASS_GLOSSY_COLOR;
  134. break;
  135. case PASS_TRANSMISSION_DIRECT:
  136. case PASS_TRANSMISSION_INDIRECT:
  137. pass.components = 4;
  138. pass.exposure = true;
  139. pass.divide_type = PASS_TRANSMISSION_COLOR;
  140. break;
  141. case PASS_SUBSURFACE_DIRECT:
  142. case PASS_SUBSURFACE_INDIRECT:
  143. pass.components = 4;
  144. pass.exposure = true;
  145. pass.divide_type = PASS_SUBSURFACE_COLOR;
  146. break;
  147. case PASS_VOLUME_DIRECT:
  148. case PASS_VOLUME_INDIRECT:
  149. pass.components = 4;
  150. pass.exposure = true;
  151. break;
  152. case PASS_CRYPTOMATTE:
  153. pass.components = 4;
  154. break;
  155. default:
  156. assert(false);
  157. break;
  158. }
  159. passes.push_back(pass);
  160. /* order from by components, to ensure alignment so passes with size 4
  161. * come first and then passes with size 1 */
  162. sort(&passes[0], &passes[0] + passes.size(), compare_pass_order);
  163. if (pass.divide_type != PASS_NONE)
  164. Pass::add(pass.divide_type, passes);
  165. }
  166. bool Pass::equals(const vector<Pass> &A, const vector<Pass> &B)
  167. {
  168. if (A.size() != B.size())
  169. return false;
  170. for (int i = 0; i < A.size(); i++)
  171. if (A[i].type != B[i].type || A[i].name != B[i].name)
  172. return false;
  173. return true;
  174. }
  175. bool Pass::contains(const vector<Pass> &passes, PassType type)
  176. {
  177. for (size_t i = 0; i < passes.size(); i++)
  178. if (passes[i].type == type)
  179. return true;
  180. return false;
  181. }
  182. /* Pixel Filter */
  183. static float filter_func_box(float /*v*/, float /*width*/)
  184. {
  185. return 1.0f;
  186. }
  187. static float filter_func_gaussian(float v, float width)
  188. {
  189. v *= 6.0f / width;
  190. return expf(-2.0f * v * v);
  191. }
  192. static float filter_func_blackman_harris(float v, float width)
  193. {
  194. v = M_2PI_F * (v / width + 0.5f);
  195. return 0.35875f - 0.48829f * cosf(v) + 0.14128f * cosf(2.0f * v) - 0.01168f * cosf(3.0f * v);
  196. }
  197. static vector<float> filter_table(FilterType type, float width)
  198. {
  199. vector<float> filter_table(FILTER_TABLE_SIZE);
  200. float (*filter_func)(float, float) = NULL;
  201. switch (type) {
  202. case FILTER_BOX:
  203. filter_func = filter_func_box;
  204. break;
  205. case FILTER_GAUSSIAN:
  206. filter_func = filter_func_gaussian;
  207. width *= 3.0f;
  208. break;
  209. case FILTER_BLACKMAN_HARRIS:
  210. filter_func = filter_func_blackman_harris;
  211. width *= 2.0f;
  212. break;
  213. default:
  214. assert(0);
  215. }
  216. /* Create importance sampling table. */
  217. /* TODO(sergey): With the even filter table size resolution we can not
  218. * really make it nice symmetric importance map without sampling full range
  219. * (meaning, we would need to sample full filter range and not use the
  220. * make_symmetric argument).
  221. *
  222. * Current code matches exactly initial filter table code, but we should
  223. * consider either making FILTER_TABLE_SIZE odd value or sample full filter.
  224. */
  225. util_cdf_inverted(FILTER_TABLE_SIZE,
  226. 0.0f,
  227. width * 0.5f,
  228. function_bind(filter_func, _1, width),
  229. true,
  230. filter_table);
  231. return filter_table;
  232. }
  233. /* Film */
  234. NODE_DEFINE(Film)
  235. {
  236. NodeType *type = NodeType::add("film", create);
  237. SOCKET_FLOAT(exposure, "Exposure", 0.8f);
  238. SOCKET_FLOAT(pass_alpha_threshold, "Pass Alpha Threshold", 0.5f);
  239. static NodeEnum filter_enum;
  240. filter_enum.insert("box", FILTER_BOX);
  241. filter_enum.insert("gaussian", FILTER_GAUSSIAN);
  242. filter_enum.insert("blackman_harris", FILTER_BLACKMAN_HARRIS);
  243. SOCKET_ENUM(filter_type, "Filter Type", filter_enum, FILTER_BOX);
  244. SOCKET_FLOAT(filter_width, "Filter Width", 1.0f);
  245. SOCKET_FLOAT(mist_start, "Mist Start", 0.0f);
  246. SOCKET_FLOAT(mist_depth, "Mist Depth", 100.0f);
  247. SOCKET_FLOAT(mist_falloff, "Mist Falloff", 1.0f);
  248. SOCKET_BOOLEAN(use_sample_clamp, "Use Sample Clamp", false);
  249. SOCKET_BOOLEAN(denoising_data_pass, "Generate Denoising Data Pass", false);
  250. SOCKET_BOOLEAN(denoising_clean_pass, "Generate Denoising Clean Pass", false);
  251. SOCKET_BOOLEAN(denoising_prefiltered_pass, "Generate Denoising Prefiltered Pass", false);
  252. SOCKET_INT(denoising_flags, "Denoising Flags", 0);
  253. return type;
  254. }
  255. Film::Film() : Node(node_type)
  256. {
  257. Pass::add(PASS_COMBINED, passes);
  258. use_light_visibility = false;
  259. filter_table_offset = TABLE_OFFSET_INVALID;
  260. cryptomatte_passes = CRYPT_NONE;
  261. need_update = true;
  262. }
  263. Film::~Film()
  264. {
  265. }
  266. void Film::device_update(Device *device, DeviceScene *dscene, Scene *scene)
  267. {
  268. if (!need_update)
  269. return;
  270. device_free(device, dscene, scene);
  271. KernelFilm *kfilm = &dscene->data.film;
  272. /* update __data */
  273. kfilm->exposure = exposure;
  274. kfilm->pass_flag = 0;
  275. kfilm->light_pass_flag = 0;
  276. kfilm->pass_stride = 0;
  277. kfilm->use_light_pass = use_light_visibility || use_sample_clamp;
  278. bool have_cryptomatte = false;
  279. for (size_t i = 0; i < passes.size(); i++) {
  280. Pass &pass = passes[i];
  281. if (pass.type == PASS_NONE) {
  282. continue;
  283. }
  284. /* Can't do motion pass if no motion vectors are available. */
  285. if (pass.type == PASS_MOTION || pass.type == PASS_MOTION_WEIGHT) {
  286. if (scene->need_motion() != Scene::MOTION_PASS) {
  287. kfilm->pass_stride += pass.components;
  288. continue;
  289. }
  290. }
  291. int pass_flag = (1 << (pass.type % 32));
  292. if (pass.type <= PASS_CATEGORY_MAIN_END) {
  293. kfilm->pass_flag |= pass_flag;
  294. }
  295. else {
  296. assert(pass.type <= PASS_CATEGORY_LIGHT_END);
  297. kfilm->use_light_pass = 1;
  298. kfilm->light_pass_flag |= pass_flag;
  299. }
  300. switch (pass.type) {
  301. case PASS_COMBINED:
  302. kfilm->pass_combined = kfilm->pass_stride;
  303. break;
  304. case PASS_DEPTH:
  305. kfilm->pass_depth = kfilm->pass_stride;
  306. break;
  307. case PASS_NORMAL:
  308. kfilm->pass_normal = kfilm->pass_stride;
  309. break;
  310. case PASS_UV:
  311. kfilm->pass_uv = kfilm->pass_stride;
  312. break;
  313. case PASS_MOTION:
  314. kfilm->pass_motion = kfilm->pass_stride;
  315. break;
  316. case PASS_MOTION_WEIGHT:
  317. kfilm->pass_motion_weight = kfilm->pass_stride;
  318. break;
  319. case PASS_OBJECT_ID:
  320. kfilm->pass_object_id = kfilm->pass_stride;
  321. break;
  322. case PASS_MATERIAL_ID:
  323. kfilm->pass_material_id = kfilm->pass_stride;
  324. break;
  325. case PASS_MIST:
  326. kfilm->pass_mist = kfilm->pass_stride;
  327. break;
  328. case PASS_EMISSION:
  329. kfilm->pass_emission = kfilm->pass_stride;
  330. break;
  331. case PASS_BACKGROUND:
  332. kfilm->pass_background = kfilm->pass_stride;
  333. break;
  334. case PASS_AO:
  335. kfilm->pass_ao = kfilm->pass_stride;
  336. break;
  337. case PASS_SHADOW:
  338. kfilm->pass_shadow = kfilm->pass_stride;
  339. break;
  340. case PASS_LIGHT:
  341. break;
  342. case PASS_DIFFUSE_COLOR:
  343. kfilm->pass_diffuse_color = kfilm->pass_stride;
  344. break;
  345. case PASS_GLOSSY_COLOR:
  346. kfilm->pass_glossy_color = kfilm->pass_stride;
  347. break;
  348. case PASS_TRANSMISSION_COLOR:
  349. kfilm->pass_transmission_color = kfilm->pass_stride;
  350. break;
  351. case PASS_SUBSURFACE_COLOR:
  352. kfilm->pass_subsurface_color = kfilm->pass_stride;
  353. break;
  354. case PASS_DIFFUSE_INDIRECT:
  355. kfilm->pass_diffuse_indirect = kfilm->pass_stride;
  356. break;
  357. case PASS_GLOSSY_INDIRECT:
  358. kfilm->pass_glossy_indirect = kfilm->pass_stride;
  359. break;
  360. case PASS_TRANSMISSION_INDIRECT:
  361. kfilm->pass_transmission_indirect = kfilm->pass_stride;
  362. break;
  363. case PASS_SUBSURFACE_INDIRECT:
  364. kfilm->pass_subsurface_indirect = kfilm->pass_stride;
  365. break;
  366. case PASS_VOLUME_INDIRECT:
  367. kfilm->pass_volume_indirect = kfilm->pass_stride;
  368. break;
  369. case PASS_DIFFUSE_DIRECT:
  370. kfilm->pass_diffuse_direct = kfilm->pass_stride;
  371. break;
  372. case PASS_GLOSSY_DIRECT:
  373. kfilm->pass_glossy_direct = kfilm->pass_stride;
  374. break;
  375. case PASS_TRANSMISSION_DIRECT:
  376. kfilm->pass_transmission_direct = kfilm->pass_stride;
  377. break;
  378. case PASS_SUBSURFACE_DIRECT:
  379. kfilm->pass_subsurface_direct = kfilm->pass_stride;
  380. break;
  381. case PASS_VOLUME_DIRECT:
  382. kfilm->pass_volume_direct = kfilm->pass_stride;
  383. break;
  384. #ifdef WITH_CYCLES_DEBUG
  385. case PASS_BVH_TRAVERSED_NODES:
  386. kfilm->pass_bvh_traversed_nodes = kfilm->pass_stride;
  387. break;
  388. case PASS_BVH_TRAVERSED_INSTANCES:
  389. kfilm->pass_bvh_traversed_instances = kfilm->pass_stride;
  390. break;
  391. case PASS_BVH_INTERSECTIONS:
  392. kfilm->pass_bvh_intersections = kfilm->pass_stride;
  393. break;
  394. case PASS_RAY_BOUNCES:
  395. kfilm->pass_ray_bounces = kfilm->pass_stride;
  396. break;
  397. #endif
  398. case PASS_RENDER_TIME:
  399. break;
  400. case PASS_CRYPTOMATTE:
  401. kfilm->pass_cryptomatte = have_cryptomatte ?
  402. min(kfilm->pass_cryptomatte, kfilm->pass_stride) :
  403. kfilm->pass_stride;
  404. have_cryptomatte = true;
  405. break;
  406. default:
  407. assert(false);
  408. break;
  409. }
  410. kfilm->pass_stride += pass.components;
  411. }
  412. kfilm->pass_denoising_data = 0;
  413. kfilm->pass_denoising_clean = 0;
  414. kfilm->denoising_flags = 0;
  415. if (denoising_data_pass) {
  416. kfilm->pass_denoising_data = kfilm->pass_stride;
  417. kfilm->pass_stride += DENOISING_PASS_SIZE_BASE;
  418. kfilm->denoising_flags = denoising_flags;
  419. if (denoising_clean_pass) {
  420. kfilm->pass_denoising_clean = kfilm->pass_stride;
  421. kfilm->pass_stride += DENOISING_PASS_SIZE_CLEAN;
  422. kfilm->use_light_pass = 1;
  423. }
  424. if (denoising_prefiltered_pass) {
  425. kfilm->pass_stride += DENOISING_PASS_SIZE_PREFILTERED;
  426. }
  427. }
  428. kfilm->pass_stride = align_up(kfilm->pass_stride, 4);
  429. kfilm->pass_alpha_threshold = pass_alpha_threshold;
  430. /* update filter table */
  431. vector<float> table = filter_table(filter_type, filter_width);
  432. scene->lookup_tables->remove_table(&filter_table_offset);
  433. filter_table_offset = scene->lookup_tables->add_table(dscene, table);
  434. kfilm->filter_table_offset = (int)filter_table_offset;
  435. /* mist pass parameters */
  436. kfilm->mist_start = mist_start;
  437. kfilm->mist_inv_depth = (mist_depth > 0.0f) ? 1.0f / mist_depth : 0.0f;
  438. kfilm->mist_falloff = mist_falloff;
  439. kfilm->cryptomatte_passes = cryptomatte_passes;
  440. kfilm->cryptomatte_depth = cryptomatte_depth;
  441. pass_stride = kfilm->pass_stride;
  442. denoising_data_offset = kfilm->pass_denoising_data;
  443. denoising_clean_offset = kfilm->pass_denoising_clean;
  444. need_update = false;
  445. }
  446. void Film::device_free(Device * /*device*/, DeviceScene * /*dscene*/, Scene *scene)
  447. {
  448. scene->lookup_tables->remove_table(&filter_table_offset);
  449. }
  450. bool Film::modified(const Film &film)
  451. {
  452. return !Node::equals(film) || !Pass::equals(passes, film.passes);
  453. }
  454. void Film::tag_passes_update(Scene *scene, const vector<Pass> &passes_)
  455. {
  456. if (Pass::contains(passes, PASS_UV) != Pass::contains(passes_, PASS_UV)) {
  457. scene->mesh_manager->tag_update(scene);
  458. foreach (Shader *shader, scene->shaders)
  459. shader->need_update_mesh = true;
  460. }
  461. else if (Pass::contains(passes, PASS_MOTION) != Pass::contains(passes_, PASS_MOTION))
  462. scene->mesh_manager->tag_update(scene);
  463. passes = passes_;
  464. }
  465. void Film::tag_update(Scene * /*scene*/)
  466. {
  467. need_update = true;
  468. }
  469. CCL_NAMESPACE_END