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