session.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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 <string.h>
  17. #include <limits.h>
  18. #include "render/buffers.h"
  19. #include "render/camera.h"
  20. #include "device/device.h"
  21. #include "render/graph.h"
  22. #include "render/integrator.h"
  23. #include "render/light.h"
  24. #include "render/mesh.h"
  25. #include "render/object.h"
  26. #include "render/scene.h"
  27. #include "render/session.h"
  28. #include "render/bake.h"
  29. #include "util/util_foreach.h"
  30. #include "util/util_function.h"
  31. #include "util/util_logging.h"
  32. #include "util/util_math.h"
  33. #include "util/util_opengl.h"
  34. #include "util/util_task.h"
  35. #include "util/util_time.h"
  36. CCL_NAMESPACE_BEGIN
  37. /* Note about preserve_tile_device option for tile manager:
  38. * progressive refine and viewport rendering does requires tiles to
  39. * always be allocated for the same device
  40. */
  41. Session::Session(const SessionParams &params_)
  42. : params(params_),
  43. tile_manager(params.progressive,
  44. params.samples,
  45. params.tile_size,
  46. params.start_resolution,
  47. params.background == false || params.progressive_refine,
  48. params.background,
  49. params.tile_order,
  50. max(params.device.multi_devices.size(), 1),
  51. params.pixel_size),
  52. stats(),
  53. profiler()
  54. {
  55. device_use_gl = ((params.device.type != DEVICE_CPU) && !params.background);
  56. TaskScheduler::init(params.threads);
  57. device = Device::create(params.device, stats, profiler, params.background);
  58. if (params.background && !params.write_render_cb) {
  59. buffers = NULL;
  60. display = NULL;
  61. }
  62. else {
  63. buffers = new RenderBuffers(device);
  64. display = new DisplayBuffer(device, params.display_buffer_linear);
  65. }
  66. session_thread = NULL;
  67. scene = NULL;
  68. reset_time = 0.0;
  69. last_update_time = 0.0;
  70. delayed_reset.do_reset = false;
  71. delayed_reset.samples = 0;
  72. display_outdated = false;
  73. gpu_draw_ready = false;
  74. gpu_need_tonemap = false;
  75. pause = false;
  76. kernels_loaded = false;
  77. /* TODO(sergey): Check if it's indeed optimal value for the split kernel. */
  78. max_closure_global = 1;
  79. }
  80. Session::~Session()
  81. {
  82. if (session_thread) {
  83. /* wait for session thread to end */
  84. progress.set_cancel("Exiting");
  85. gpu_need_tonemap = false;
  86. gpu_need_tonemap_cond.notify_all();
  87. {
  88. thread_scoped_lock pause_lock(pause_mutex);
  89. pause = false;
  90. }
  91. pause_cond.notify_all();
  92. wait();
  93. }
  94. if (params.write_render_cb) {
  95. /* tonemap and write out image if requested */
  96. delete display;
  97. display = new DisplayBuffer(device, false);
  98. display->reset(buffers->params);
  99. tonemap(params.samples);
  100. int w = display->draw_width;
  101. int h = display->draw_height;
  102. uchar4 *pixels = display->rgba_byte.copy_from_device(0, w, h);
  103. params.write_render_cb((uchar *)pixels, w, h, 4);
  104. }
  105. /* clean up */
  106. tile_manager.device_free();
  107. delete buffers;
  108. delete display;
  109. delete scene;
  110. delete device;
  111. TaskScheduler::exit();
  112. }
  113. void Session::start()
  114. {
  115. if (!session_thread) {
  116. session_thread = new thread(function_bind(&Session::run, this));
  117. }
  118. }
  119. bool Session::ready_to_reset()
  120. {
  121. double dt = time_dt() - reset_time;
  122. if (!display_outdated)
  123. return (dt > params.reset_timeout);
  124. else
  125. return (dt > params.cancel_timeout);
  126. }
  127. /* GPU Session */
  128. void Session::reset_gpu(BufferParams &buffer_params, int samples)
  129. {
  130. thread_scoped_lock pause_lock(pause_mutex);
  131. /* block for buffer access and reset immediately. we can't do this
  132. * in the thread, because we need to allocate an OpenGL buffer, and
  133. * that only works in the main thread */
  134. thread_scoped_lock display_lock(display_mutex);
  135. thread_scoped_lock buffers_lock(buffers_mutex);
  136. display_outdated = true;
  137. reset_time = time_dt();
  138. reset_(buffer_params, samples);
  139. gpu_need_tonemap = false;
  140. gpu_need_tonemap_cond.notify_all();
  141. pause_cond.notify_all();
  142. }
  143. bool Session::draw_gpu(BufferParams &buffer_params, DeviceDrawParams &draw_params)
  144. {
  145. /* block for buffer access */
  146. thread_scoped_lock display_lock(display_mutex);
  147. /* first check we already rendered something */
  148. if (gpu_draw_ready) {
  149. /* then verify the buffers have the expected size, so we don't
  150. * draw previous results in a resized window */
  151. if (!buffer_params.modified(display->params)) {
  152. /* for CUDA we need to do tone-mapping still, since we can
  153. * only access GL buffers from the main thread. */
  154. if (gpu_need_tonemap) {
  155. thread_scoped_lock buffers_lock(buffers_mutex);
  156. tonemap(tile_manager.state.sample);
  157. gpu_need_tonemap = false;
  158. gpu_need_tonemap_cond.notify_all();
  159. }
  160. display->draw(device, draw_params);
  161. if (display_outdated && (time_dt() - reset_time) > params.text_timeout)
  162. return false;
  163. return true;
  164. }
  165. }
  166. return false;
  167. }
  168. void Session::run_gpu()
  169. {
  170. bool tiles_written = false;
  171. reset_time = time_dt();
  172. last_update_time = time_dt();
  173. progress.set_render_start_time();
  174. while (!progress.get_cancel()) {
  175. /* advance to next tile */
  176. bool no_tiles = !tile_manager.next();
  177. DeviceKernelStatus kernel_state = DEVICE_KERNEL_UNKNOWN;
  178. if (no_tiles) {
  179. kernel_state = device->get_active_kernel_switch_state();
  180. }
  181. if (params.background) {
  182. /* if no work left and in background mode, we can stop immediately */
  183. if (no_tiles) {
  184. progress.set_status("Finished");
  185. break;
  186. }
  187. }
  188. /* Don't go in pause mode when image was rendered with preview kernels
  189. * When feature kernels become available the session will be resetted. */
  190. else if (no_tiles && kernel_state == DEVICE_KERNEL_WAITING_FOR_FEATURE_KERNEL) {
  191. time_sleep(0.1);
  192. }
  193. else if (no_tiles && kernel_state == DEVICE_KERNEL_FEATURE_KERNEL_AVAILABLE) {
  194. reset_gpu(tile_manager.params, params.samples);
  195. }
  196. else {
  197. /* if in interactive mode, and we are either paused or done for now,
  198. * wait for pause condition notify to wake up again */
  199. thread_scoped_lock pause_lock(pause_mutex);
  200. if (!pause && !tile_manager.done()) {
  201. /* reset could have happened after no_tiles was set, before this lock.
  202. * in this case we shall not wait for pause condition
  203. */
  204. }
  205. else if (pause || no_tiles) {
  206. update_status_time(pause, no_tiles);
  207. while (1) {
  208. scoped_timer pause_timer;
  209. pause_cond.wait(pause_lock);
  210. if (pause) {
  211. progress.add_skip_time(pause_timer, params.background);
  212. }
  213. update_status_time(pause, no_tiles);
  214. progress.set_update();
  215. if (!pause)
  216. break;
  217. }
  218. }
  219. if (progress.get_cancel())
  220. break;
  221. }
  222. if (!no_tiles) {
  223. /* update scene */
  224. scoped_timer update_timer;
  225. if (update_scene()) {
  226. profiler.reset(scene->shaders.size(), scene->objects.size());
  227. }
  228. progress.add_skip_time(update_timer, params.background);
  229. if (!device->error_message().empty())
  230. progress.set_error(device->error_message());
  231. if (progress.get_cancel())
  232. break;
  233. }
  234. if (!no_tiles) {
  235. /* buffers mutex is locked entirely while rendering each
  236. * sample, and released/reacquired on each iteration to allow
  237. * reset and draw in between */
  238. thread_scoped_lock buffers_lock(buffers_mutex);
  239. /* update status and timing */
  240. update_status_time();
  241. /* render */
  242. render();
  243. device->task_wait();
  244. if (!device->error_message().empty())
  245. progress.set_cancel(device->error_message());
  246. /* update status and timing */
  247. update_status_time();
  248. gpu_need_tonemap = true;
  249. gpu_draw_ready = true;
  250. progress.set_update();
  251. /* wait for tonemap */
  252. if (!params.background) {
  253. while (gpu_need_tonemap) {
  254. if (progress.get_cancel())
  255. break;
  256. gpu_need_tonemap_cond.wait(buffers_lock);
  257. }
  258. }
  259. if (!device->error_message().empty())
  260. progress.set_error(device->error_message());
  261. tiles_written = update_progressive_refine(progress.get_cancel());
  262. if (progress.get_cancel())
  263. break;
  264. }
  265. }
  266. if (!tiles_written)
  267. update_progressive_refine(true);
  268. }
  269. /* CPU Session */
  270. void Session::reset_cpu(BufferParams &buffer_params, int samples)
  271. {
  272. thread_scoped_lock reset_lock(delayed_reset.mutex);
  273. thread_scoped_lock pause_lock(pause_mutex);
  274. display_outdated = true;
  275. reset_time = time_dt();
  276. delayed_reset.params = buffer_params;
  277. delayed_reset.samples = samples;
  278. delayed_reset.do_reset = true;
  279. device->task_cancel();
  280. pause_cond.notify_all();
  281. }
  282. bool Session::draw_cpu(BufferParams &buffer_params, DeviceDrawParams &draw_params)
  283. {
  284. thread_scoped_lock display_lock(display_mutex);
  285. /* first check we already rendered something */
  286. if (display->draw_ready()) {
  287. /* then verify the buffers have the expected size, so we don't
  288. * draw previous results in a resized window */
  289. if (!buffer_params.modified(display->params)) {
  290. display->draw(device, draw_params);
  291. if (display_outdated && (time_dt() - reset_time) > params.text_timeout)
  292. return false;
  293. return true;
  294. }
  295. }
  296. return false;
  297. }
  298. bool Session::acquire_tile(Device *tile_device, RenderTile &rtile)
  299. {
  300. if (progress.get_cancel()) {
  301. if (params.progressive_refine == false) {
  302. /* for progressive refine current sample should be finished for all tiles */
  303. return false;
  304. }
  305. }
  306. thread_scoped_lock tile_lock(tile_mutex);
  307. /* get next tile from manager */
  308. Tile *tile;
  309. int device_num = device->device_number(tile_device);
  310. if (!tile_manager.next_tile(tile, device_num))
  311. return false;
  312. /* fill render tile */
  313. rtile.x = tile_manager.state.buffer.full_x + tile->x;
  314. rtile.y = tile_manager.state.buffer.full_y + tile->y;
  315. rtile.w = tile->w;
  316. rtile.h = tile->h;
  317. rtile.start_sample = tile_manager.state.sample;
  318. rtile.num_samples = tile_manager.state.num_samples;
  319. rtile.resolution = tile_manager.state.resolution_divider;
  320. rtile.tile_index = tile->index;
  321. rtile.task = (tile->state == Tile::DENOISE) ? RenderTile::DENOISE : RenderTile::PATH_TRACE;
  322. tile_lock.unlock();
  323. /* in case of a permanent buffer, return it, otherwise we will allocate
  324. * a new temporary buffer */
  325. if (buffers) {
  326. tile_manager.state.buffer.get_offset_stride(rtile.offset, rtile.stride);
  327. rtile.buffer = buffers->buffer.device_pointer;
  328. rtile.buffers = buffers;
  329. device->map_tile(tile_device, rtile);
  330. return true;
  331. }
  332. if (tile->buffers == NULL) {
  333. /* fill buffer parameters */
  334. BufferParams buffer_params = tile_manager.params;
  335. buffer_params.full_x = rtile.x;
  336. buffer_params.full_y = rtile.y;
  337. buffer_params.width = rtile.w;
  338. buffer_params.height = rtile.h;
  339. /* allocate buffers */
  340. tile->buffers = new RenderBuffers(tile_device);
  341. tile->buffers->reset(buffer_params);
  342. }
  343. tile->buffers->params.get_offset_stride(rtile.offset, rtile.stride);
  344. rtile.buffer = tile->buffers->buffer.device_pointer;
  345. rtile.buffers = tile->buffers;
  346. rtile.sample = tile_manager.state.sample;
  347. /* this will tag tile as IN PROGRESS in blender-side render pipeline,
  348. * which is needed to highlight currently rendering tile before first
  349. * sample was processed for it
  350. */
  351. update_tile_sample(rtile);
  352. return true;
  353. }
  354. void Session::update_tile_sample(RenderTile &rtile)
  355. {
  356. thread_scoped_lock tile_lock(tile_mutex);
  357. if (update_render_tile_cb) {
  358. if (params.progressive_refine == false) {
  359. /* todo: optimize this by making it thread safe and removing lock */
  360. update_render_tile_cb(rtile, true);
  361. }
  362. }
  363. update_status_time();
  364. }
  365. void Session::release_tile(RenderTile &rtile)
  366. {
  367. thread_scoped_lock tile_lock(tile_mutex);
  368. progress.add_finished_tile(rtile.task == RenderTile::DENOISE);
  369. bool delete_tile;
  370. if (tile_manager.finish_tile(rtile.tile_index, delete_tile)) {
  371. if (write_render_tile_cb && params.progressive_refine == false) {
  372. write_render_tile_cb(rtile);
  373. }
  374. if (delete_tile) {
  375. delete rtile.buffers;
  376. tile_manager.state.tiles[rtile.tile_index].buffers = NULL;
  377. }
  378. }
  379. else {
  380. if (update_render_tile_cb && params.progressive_refine == false) {
  381. update_render_tile_cb(rtile, false);
  382. }
  383. }
  384. update_status_time();
  385. }
  386. void Session::map_neighbor_tiles(RenderTile *tiles, Device *tile_device)
  387. {
  388. thread_scoped_lock tile_lock(tile_mutex);
  389. int center_idx = tiles[4].tile_index;
  390. assert(tile_manager.state.tiles[center_idx].state == Tile::DENOISE);
  391. BufferParams buffer_params = tile_manager.params;
  392. int4 image_region = make_int4(buffer_params.full_x,
  393. buffer_params.full_y,
  394. buffer_params.full_x + buffer_params.width,
  395. buffer_params.full_y + buffer_params.height);
  396. for (int dy = -1, i = 0; dy <= 1; dy++) {
  397. for (int dx = -1; dx <= 1; dx++, i++) {
  398. int px = tiles[4].x + dx * params.tile_size.x;
  399. int py = tiles[4].y + dy * params.tile_size.y;
  400. if (px >= image_region.x && py >= image_region.y && px < image_region.z &&
  401. py < image_region.w) {
  402. int tile_index = center_idx + dy * tile_manager.state.tile_stride + dx;
  403. Tile *tile = &tile_manager.state.tiles[tile_index];
  404. assert(tile->buffers);
  405. tiles[i].buffer = tile->buffers->buffer.device_pointer;
  406. tiles[i].x = tile_manager.state.buffer.full_x + tile->x;
  407. tiles[i].y = tile_manager.state.buffer.full_y + tile->y;
  408. tiles[i].w = tile->w;
  409. tiles[i].h = tile->h;
  410. tiles[i].buffers = tile->buffers;
  411. tile->buffers->params.get_offset_stride(tiles[i].offset, tiles[i].stride);
  412. }
  413. else {
  414. tiles[i].buffer = (device_ptr)NULL;
  415. tiles[i].buffers = NULL;
  416. tiles[i].x = clamp(px, image_region.x, image_region.z);
  417. tiles[i].y = clamp(py, image_region.y, image_region.w);
  418. tiles[i].w = tiles[i].h = 0;
  419. }
  420. }
  421. }
  422. assert(tiles[4].buffers);
  423. device->map_neighbor_tiles(tile_device, tiles);
  424. /* The denoised result is written back to the original tile. */
  425. tiles[9] = tiles[4];
  426. }
  427. void Session::unmap_neighbor_tiles(RenderTile *tiles, Device *tile_device)
  428. {
  429. thread_scoped_lock tile_lock(tile_mutex);
  430. device->unmap_neighbor_tiles(tile_device, tiles);
  431. }
  432. void Session::run_cpu()
  433. {
  434. bool tiles_written = false;
  435. last_update_time = time_dt();
  436. {
  437. /* reset once to start */
  438. thread_scoped_lock reset_lock(delayed_reset.mutex);
  439. thread_scoped_lock buffers_lock(buffers_mutex);
  440. thread_scoped_lock display_lock(display_mutex);
  441. reset_(delayed_reset.params, delayed_reset.samples);
  442. delayed_reset.do_reset = false;
  443. }
  444. while (!progress.get_cancel()) {
  445. /* advance to next tile */
  446. bool no_tiles = !tile_manager.next();
  447. bool need_tonemap = false;
  448. DeviceKernelStatus kernel_state = DEVICE_KERNEL_UNKNOWN;
  449. if (no_tiles) {
  450. kernel_state = device->get_active_kernel_switch_state();
  451. }
  452. if (params.background) {
  453. /* if no work left and in background mode, we can stop immediately */
  454. if (no_tiles) {
  455. progress.set_status("Finished");
  456. break;
  457. }
  458. }
  459. /* Don't go in pause mode when preview kernels are used
  460. * When feature kernels become available the session will be resetted. */
  461. else if (no_tiles && kernel_state == DEVICE_KERNEL_WAITING_FOR_FEATURE_KERNEL) {
  462. time_sleep(0.1);
  463. }
  464. else if (no_tiles && kernel_state == DEVICE_KERNEL_FEATURE_KERNEL_AVAILABLE) {
  465. reset_cpu(tile_manager.params, params.samples);
  466. }
  467. else {
  468. /* if in interactive mode, and we are either paused or done for now,
  469. * wait for pause condition notify to wake up again */
  470. thread_scoped_lock pause_lock(pause_mutex);
  471. if (!pause && delayed_reset.do_reset) {
  472. /* reset once to start */
  473. thread_scoped_lock reset_lock(delayed_reset.mutex);
  474. thread_scoped_lock buffers_lock(buffers_mutex);
  475. thread_scoped_lock display_lock(display_mutex);
  476. reset_(delayed_reset.params, delayed_reset.samples);
  477. delayed_reset.do_reset = false;
  478. }
  479. else if (pause || no_tiles) {
  480. update_status_time(pause, no_tiles);
  481. while (1) {
  482. scoped_timer pause_timer;
  483. pause_cond.wait(pause_lock);
  484. if (pause) {
  485. progress.add_skip_time(pause_timer, params.background);
  486. }
  487. update_status_time(pause, no_tiles);
  488. progress.set_update();
  489. if (!pause)
  490. break;
  491. }
  492. }
  493. if (progress.get_cancel())
  494. break;
  495. }
  496. if (!no_tiles) {
  497. /* buffers mutex is locked entirely while rendering each
  498. * sample, and released/reacquired on each iteration to allow
  499. * reset and draw in between */
  500. thread_scoped_lock buffers_lock(buffers_mutex);
  501. /* update scene */
  502. scoped_timer update_timer;
  503. if (update_scene()) {
  504. profiler.reset(scene->shaders.size(), scene->objects.size());
  505. }
  506. progress.add_skip_time(update_timer, params.background);
  507. if (!device->error_message().empty())
  508. progress.set_error(device->error_message());
  509. if (progress.get_cancel())
  510. break;
  511. /* update status and timing */
  512. update_status_time();
  513. /* render */
  514. render();
  515. /* update status and timing */
  516. update_status_time();
  517. if (!params.background)
  518. need_tonemap = true;
  519. if (!device->error_message().empty())
  520. progress.set_error(device->error_message());
  521. }
  522. device->task_wait();
  523. {
  524. thread_scoped_lock reset_lock(delayed_reset.mutex);
  525. thread_scoped_lock buffers_lock(buffers_mutex);
  526. thread_scoped_lock display_lock(display_mutex);
  527. if (delayed_reset.do_reset) {
  528. /* reset rendering if request from main thread */
  529. delayed_reset.do_reset = false;
  530. reset_(delayed_reset.params, delayed_reset.samples);
  531. }
  532. else if (need_tonemap) {
  533. /* tonemap only if we do not reset, we don't we don't
  534. * want to show the result of an incomplete sample */
  535. tonemap(tile_manager.state.sample);
  536. }
  537. if (!device->error_message().empty())
  538. progress.set_error(device->error_message());
  539. tiles_written = update_progressive_refine(progress.get_cancel());
  540. }
  541. progress.set_update();
  542. }
  543. if (!tiles_written)
  544. update_progressive_refine(true);
  545. }
  546. DeviceRequestedFeatures Session::get_requested_device_features()
  547. {
  548. /* TODO(sergey): Consider moving this to the Scene level. */
  549. DeviceRequestedFeatures requested_features;
  550. requested_features.experimental = params.experimental;
  551. scene->shader_manager->get_requested_features(scene, &requested_features);
  552. /* This features are not being tweaked as often as shaders,
  553. * so could be done selective magic for the viewport as well.
  554. */
  555. bool use_motion = scene->need_motion() == Scene::MotionType::MOTION_BLUR;
  556. requested_features.use_hair = false;
  557. requested_features.use_object_motion = false;
  558. requested_features.use_camera_motion = use_motion && scene->camera->use_motion();
  559. foreach (Object *object, scene->objects) {
  560. Mesh *mesh = object->mesh;
  561. if (mesh->num_curves()) {
  562. requested_features.use_hair = true;
  563. }
  564. if (use_motion) {
  565. requested_features.use_object_motion |= object->use_motion() | mesh->use_motion_blur;
  566. requested_features.use_camera_motion |= mesh->use_motion_blur;
  567. }
  568. #ifdef WITH_OPENSUBDIV
  569. if (mesh->subdivision_type != Mesh::SUBDIVISION_NONE) {
  570. requested_features.use_patch_evaluation = true;
  571. }
  572. #endif
  573. if (object->is_shadow_catcher) {
  574. requested_features.use_shadow_tricks = true;
  575. }
  576. requested_features.use_true_displacement |= mesh->has_true_displacement();
  577. }
  578. requested_features.use_background_light = scene->light_manager->has_background_light(scene);
  579. BakeManager *bake_manager = scene->bake_manager;
  580. requested_features.use_baking = bake_manager->get_baking();
  581. requested_features.use_integrator_branched = (scene->integrator->method ==
  582. Integrator::BRANCHED_PATH);
  583. if (params.run_denoising) {
  584. requested_features.use_denoising = true;
  585. requested_features.use_shadow_tricks = true;
  586. }
  587. return requested_features;
  588. }
  589. bool Session::load_kernels(bool lock_scene)
  590. {
  591. thread_scoped_lock scene_lock;
  592. if (lock_scene) {
  593. scene_lock = thread_scoped_lock(scene->mutex);
  594. }
  595. DeviceRequestedFeatures requested_features = get_requested_device_features();
  596. if (!kernels_loaded || loaded_kernel_features.modified(requested_features)) {
  597. progress.set_status("Loading render kernels (may take a few minutes the first time)");
  598. scoped_timer timer;
  599. VLOG(2) << "Requested features:\n" << requested_features;
  600. if (!device->load_kernels(requested_features)) {
  601. string message = device->error_message();
  602. if (message.empty())
  603. message = "Failed loading render kernel, see console for errors";
  604. progress.set_error(message);
  605. progress.set_status("Error", message);
  606. progress.set_update();
  607. return false;
  608. }
  609. progress.add_skip_time(timer, false);
  610. VLOG(1) << "Total time spent loading kernels: " << time_dt() - timer.get_start();
  611. kernels_loaded = true;
  612. loaded_kernel_features = requested_features;
  613. return true;
  614. }
  615. return false;
  616. }
  617. void Session::run()
  618. {
  619. if (params.use_profiling && (params.device.type == DEVICE_CPU)) {
  620. profiler.start();
  621. }
  622. /* session thread loop */
  623. progress.set_status("Waiting for render to start");
  624. /* run */
  625. if (!progress.get_cancel()) {
  626. /* reset number of rendered samples */
  627. progress.reset_sample();
  628. if (device_use_gl)
  629. run_gpu();
  630. else
  631. run_cpu();
  632. }
  633. profiler.stop();
  634. /* progress update */
  635. if (progress.get_cancel())
  636. progress.set_status("Cancel", progress.get_cancel_message());
  637. else
  638. progress.set_update();
  639. }
  640. bool Session::draw(BufferParams &buffer_params, DeviceDrawParams &draw_params)
  641. {
  642. if (device_use_gl)
  643. return draw_gpu(buffer_params, draw_params);
  644. else
  645. return draw_cpu(buffer_params, draw_params);
  646. }
  647. void Session::reset_(BufferParams &buffer_params, int samples)
  648. {
  649. if (buffers && buffer_params.modified(tile_manager.params)) {
  650. gpu_draw_ready = false;
  651. buffers->reset(buffer_params);
  652. if (display) {
  653. display->reset(buffer_params);
  654. }
  655. }
  656. tile_manager.reset(buffer_params, samples);
  657. progress.reset_sample();
  658. bool show_progress = params.background || tile_manager.get_num_effective_samples() != INT_MAX;
  659. progress.set_total_pixel_samples(show_progress ? tile_manager.state.total_pixel_samples : 0);
  660. if (!params.background)
  661. progress.set_start_time();
  662. progress.set_render_start_time();
  663. }
  664. void Session::reset(BufferParams &buffer_params, int samples)
  665. {
  666. if (device_use_gl)
  667. reset_gpu(buffer_params, samples);
  668. else
  669. reset_cpu(buffer_params, samples);
  670. }
  671. void Session::set_samples(int samples)
  672. {
  673. if (samples != params.samples) {
  674. params.samples = samples;
  675. tile_manager.set_samples(samples);
  676. {
  677. thread_scoped_lock pause_lock(pause_mutex);
  678. }
  679. pause_cond.notify_all();
  680. }
  681. }
  682. void Session::set_pause(bool pause_)
  683. {
  684. bool notify = false;
  685. {
  686. thread_scoped_lock pause_lock(pause_mutex);
  687. if (pause != pause_) {
  688. pause = pause_;
  689. notify = true;
  690. }
  691. }
  692. if (notify)
  693. pause_cond.notify_all();
  694. }
  695. void Session::wait()
  696. {
  697. if (session_thread) {
  698. session_thread->join();
  699. delete session_thread;
  700. }
  701. session_thread = NULL;
  702. }
  703. bool Session::update_scene()
  704. {
  705. thread_scoped_lock scene_lock(scene->mutex);
  706. /* update camera if dimensions changed for progressive render. the camera
  707. * knows nothing about progressive or cropped rendering, it just gets the
  708. * image dimensions passed in */
  709. Camera *cam = scene->camera;
  710. int width = tile_manager.state.buffer.full_width;
  711. int height = tile_manager.state.buffer.full_height;
  712. int resolution = tile_manager.state.resolution_divider;
  713. if (width != cam->width || height != cam->height) {
  714. cam->width = width;
  715. cam->height = height;
  716. cam->resolution = resolution;
  717. cam->tag_update();
  718. }
  719. /* number of samples is needed by multi jittered
  720. * sampling pattern and by baking */
  721. Integrator *integrator = scene->integrator;
  722. BakeManager *bake_manager = scene->bake_manager;
  723. if (integrator->sampling_pattern == SAMPLING_PATTERN_CMJ || bake_manager->get_baking()) {
  724. int aa_samples = tile_manager.num_samples;
  725. if (aa_samples != integrator->aa_samples) {
  726. integrator->aa_samples = aa_samples;
  727. integrator->tag_update(scene);
  728. }
  729. }
  730. /* update scene */
  731. if (scene->need_update()) {
  732. bool new_kernels_needed = load_kernels(false);
  733. /* Update max_closures. */
  734. KernelIntegrator *kintegrator = &scene->dscene.data.integrator;
  735. if (params.background) {
  736. kintegrator->max_closures = get_max_closure_count();
  737. }
  738. else {
  739. /* Currently viewport render is faster with higher max_closures, needs investigating. */
  740. kintegrator->max_closures = MAX_CLOSURE;
  741. }
  742. progress.set_status("Updating Scene");
  743. MEM_GUARDED_CALL(&progress, scene->device_update, device, progress);
  744. DeviceKernelStatus kernel_switch_status = device->get_active_kernel_switch_state();
  745. bool kernel_switch_needed = kernel_switch_status == DEVICE_KERNEL_FEATURE_KERNEL_AVAILABLE ||
  746. kernel_switch_status == DEVICE_KERNEL_FEATURE_KERNEL_INVALID;
  747. if (kernel_switch_status == DEVICE_KERNEL_WAITING_FOR_FEATURE_KERNEL) {
  748. progress.set_kernel_status("Compiling render kernels");
  749. }
  750. if (new_kernels_needed || kernel_switch_needed) {
  751. progress.set_kernel_status("Compiling render kernels");
  752. device->wait_for_availability(loaded_kernel_features);
  753. progress.set_kernel_status("");
  754. }
  755. if (kernel_switch_needed) {
  756. reset(tile_manager.params, params.samples);
  757. }
  758. return true;
  759. }
  760. return false;
  761. }
  762. void Session::update_status_time(bool show_pause, bool show_done)
  763. {
  764. int progressive_sample = tile_manager.state.sample;
  765. int num_samples = tile_manager.get_num_effective_samples();
  766. int tile = progress.get_rendered_tiles();
  767. int num_tiles = tile_manager.state.num_tiles;
  768. /* update status */
  769. string status, substatus;
  770. if (!params.progressive) {
  771. const bool is_cpu = params.device.type == DEVICE_CPU;
  772. const bool rendering_finished = (tile == num_tiles);
  773. const bool is_last_tile = (tile + 1) == num_tiles;
  774. substatus = string_printf("Rendered %d/%d Tiles", tile, num_tiles);
  775. if (!rendering_finished && (device->show_samples() || (is_cpu && is_last_tile))) {
  776. /* Some devices automatically support showing the sample number:
  777. * - CUDADevice
  778. * - OpenCLDevice when using the megakernel (the split kernel renders multiple
  779. * samples at the same time, so the current sample isn't really defined)
  780. * - CPUDevice when using one thread
  781. * For these devices, the current sample is always shown.
  782. *
  783. * The other option is when the last tile is currently being rendered by the CPU.
  784. */
  785. substatus += string_printf(", Sample %d/%d", progress.get_current_sample(), num_samples);
  786. }
  787. if (params.full_denoising) {
  788. substatus += string_printf(", Denoised %d tiles", progress.get_denoised_tiles());
  789. }
  790. else if (params.run_denoising) {
  791. substatus += string_printf(", Prefiltered %d tiles", progress.get_denoised_tiles());
  792. }
  793. }
  794. else if (tile_manager.num_samples == INT_MAX)
  795. substatus = string_printf("Path Tracing Sample %d", progressive_sample + 1);
  796. else
  797. substatus = string_printf("Path Tracing Sample %d/%d", progressive_sample + 1, num_samples);
  798. if (show_pause) {
  799. status = "Rendering Paused";
  800. }
  801. else if (show_done) {
  802. status = "Rendering Done";
  803. progress.set_end_time(); /* Save end time so that further calls to get_time are accurate. */
  804. }
  805. else {
  806. status = substatus;
  807. substatus.clear();
  808. }
  809. progress.set_status(status, substatus);
  810. }
  811. void Session::render()
  812. {
  813. /* Clear buffers. */
  814. if (buffers && tile_manager.state.sample == tile_manager.range_start_sample) {
  815. buffers->zero();
  816. }
  817. /* Add path trace task. */
  818. DeviceTask task(DeviceTask::RENDER);
  819. task.acquire_tile = function_bind(&Session::acquire_tile, this, _1, _2);
  820. task.release_tile = function_bind(&Session::release_tile, this, _1);
  821. task.map_neighbor_tiles = function_bind(&Session::map_neighbor_tiles, this, _1, _2);
  822. task.unmap_neighbor_tiles = function_bind(&Session::unmap_neighbor_tiles, this, _1, _2);
  823. task.get_cancel = function_bind(&Progress::get_cancel, &this->progress);
  824. task.update_tile_sample = function_bind(&Session::update_tile_sample, this, _1);
  825. task.update_progress_sample = function_bind(&Progress::add_samples, &this->progress, _1, _2);
  826. task.need_finish_queue = params.progressive_refine;
  827. task.integrator_branched = scene->integrator->method == Integrator::BRANCHED_PATH;
  828. task.requested_tile_size = params.tile_size;
  829. task.passes_size = tile_manager.params.get_passes_size();
  830. if (params.run_denoising) {
  831. task.denoising = params.denoising;
  832. assert(!scene->film->need_update);
  833. task.pass_stride = scene->film->pass_stride;
  834. task.target_pass_stride = task.pass_stride;
  835. task.pass_denoising_data = scene->film->denoising_data_offset;
  836. task.pass_denoising_clean = scene->film->denoising_clean_offset;
  837. task.denoising_from_render = true;
  838. task.denoising_do_filter = params.full_denoising;
  839. task.denoising_write_passes = params.write_denoising_passes;
  840. }
  841. device->task_add(task);
  842. }
  843. void Session::tonemap(int sample)
  844. {
  845. /* add tonemap task */
  846. DeviceTask task(DeviceTask::FILM_CONVERT);
  847. task.x = tile_manager.state.buffer.full_x;
  848. task.y = tile_manager.state.buffer.full_y;
  849. task.w = tile_manager.state.buffer.width;
  850. task.h = tile_manager.state.buffer.height;
  851. task.rgba_byte = display->rgba_byte.device_pointer;
  852. task.rgba_half = display->rgba_half.device_pointer;
  853. task.buffer = buffers->buffer.device_pointer;
  854. task.sample = sample;
  855. tile_manager.state.buffer.get_offset_stride(task.offset, task.stride);
  856. if (task.w > 0 && task.h > 0) {
  857. device->task_add(task);
  858. device->task_wait();
  859. /* set display to new size */
  860. display->draw_set(task.w, task.h);
  861. }
  862. display_outdated = false;
  863. }
  864. bool Session::update_progressive_refine(bool cancel)
  865. {
  866. int sample = tile_manager.state.sample + 1;
  867. bool write = sample == tile_manager.num_samples || cancel;
  868. double current_time = time_dt();
  869. if (current_time - last_update_time < params.progressive_update_timeout) {
  870. /* if last sample was processed, we need to write buffers anyway */
  871. if (!write && sample != 1)
  872. return false;
  873. }
  874. if (params.progressive_refine) {
  875. foreach (Tile &tile, tile_manager.state.tiles) {
  876. if (!tile.buffers) {
  877. continue;
  878. }
  879. RenderTile rtile;
  880. rtile.x = tile_manager.state.buffer.full_x + tile.x;
  881. rtile.y = tile_manager.state.buffer.full_y + tile.y;
  882. rtile.w = tile.w;
  883. rtile.h = tile.h;
  884. rtile.sample = sample;
  885. rtile.buffers = tile.buffers;
  886. if (write) {
  887. if (write_render_tile_cb)
  888. write_render_tile_cb(rtile);
  889. }
  890. else {
  891. if (update_render_tile_cb)
  892. update_render_tile_cb(rtile, true);
  893. }
  894. }
  895. }
  896. last_update_time = current_time;
  897. return write;
  898. }
  899. void Session::device_free()
  900. {
  901. scene->device_free();
  902. tile_manager.device_free();
  903. /* used from background render only, so no need to
  904. * re-create render/display buffers here
  905. */
  906. }
  907. void Session::collect_statistics(RenderStats *render_stats)
  908. {
  909. scene->collect_statistics(render_stats);
  910. if (params.use_profiling && (params.device.type == DEVICE_CPU)) {
  911. render_stats->collect_profiling(scene, profiler);
  912. }
  913. }
  914. int Session::get_max_closure_count()
  915. {
  916. if (scene->shader_manager->use_osl()) {
  917. /* OSL always needs the maximum as we can't predict the
  918. * number of closures a shader might generate. */
  919. return MAX_CLOSURE;
  920. }
  921. int max_closures = 0;
  922. for (int i = 0; i < scene->shaders.size(); i++) {
  923. int num_closures = scene->shaders[i]->graph->get_num_closures();
  924. max_closures = max(max_closures, num_closures);
  925. }
  926. max_closure_global = max(max_closure_global, max_closures);
  927. if (max_closure_global > MAX_CLOSURE) {
  928. /* This is usually harmless as more complex shader tend to get many
  929. * closures discarded due to mixing or low weights. We need to limit
  930. * to MAX_CLOSURE as this is hardcoded in CPU/mega kernels, and it
  931. * avoids excessive memory usage for split kernels. */
  932. VLOG(2) << "Maximum number of closures exceeded: " << max_closure_global << " > "
  933. << MAX_CLOSURE;
  934. max_closure_global = MAX_CLOSURE;
  935. }
  936. return max_closure_global;
  937. }
  938. CCL_NAMESPACE_END