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
|
/*
* Copyright 2011, Blender Foundation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <limits.h>
#include "buffers.h"
#include "camera.h"
#include "device.h"
#include "scene.h"
#include "session.h"
#include "util_foreach.h"
#include "util_function.h"
#include "util_time.h"
CCL_NAMESPACE_BEGIN
Session::Session(const SessionParams& params_)
: params(params_),
tile_manager(params.progressive, params.passes, params.tile_size, params.min_size)
{
device_use_gl = ((params.device_type == DEVICE_CUDA || params.device_type == DEVICE_OPENCL) && !params.background);
device = Device::create(params.device_type, params.background, params.threads);
buffers = new RenderBuffers(device);
display = new DisplayBuffer(device);
session_thread = NULL;
scene = NULL;
start_time = 0.0;
reset_time = 0.0;
preview_time = 0.0;
paused_time = 0.0;
pass = 0;
delayed_reset.do_reset = false;
delayed_reset.w = 0;
delayed_reset.h = 0;
delayed_reset.passes = 0;
display_outdated = false;
gpu_draw_ready = false;
gpu_need_tonemap = false;
pause = false;
}
Session::~Session()
{
if(session_thread) {
progress.set_cancel("Exiting");
gpu_need_tonemap = false;
gpu_need_tonemap_cond.notify_all();
{
thread_scoped_lock pause_lock(pause_mutex);
pause = false;
}
pause_cond.notify_all();
wait();
}
if(params.output_path != "") {
tonemap();
progress.set_status("Writing Image", params.output_path);
display->write(device, params.output_path);
}
delete buffers;
delete display;
delete scene;
delete device;
}
void Session::start()
{
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(int w, int h, int passes)
{
/* block for buffer acces 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_(w, h, passes);
gpu_need_tonemap = false;
gpu_need_tonemap_cond.notify_all();
pause_cond.notify_all();
}
bool Session::draw_gpu(int w, int h)
{
/* 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(w == display->width && h == display->height) {
/* for CUDA we need to do tonemapping still, since we can
only access GL buffers from the main thread */
if(gpu_need_tonemap) {
thread_scoped_lock buffers_lock(buffers->mutex);
tonemap();
gpu_need_tonemap = false;
gpu_need_tonemap_cond.notify_all();
}
display->draw(device);
if(display_outdated && (time_dt() - reset_time) > params.text_timeout)
return false;
return true;
}
}
return false;
}
void Session::run_gpu()
{
start_time = time_dt();
reset_time = time_dt();
paused_time = 0.0;
while(!progress.get_cancel()) {
/* advance to next tile */
bool no_tiles = !tile_manager.next();
if(params.background) {
/* if no work left and in background mode, we can stop immediately */
if(no_tiles)
break;
}
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 || no_tiles) {
update_status_time(pause, no_tiles);
while(1) {
double pause_start = time_dt();
pause_cond.wait(pause_lock);
paused_time += time_dt() - pause_start;
update_status_time(pause, no_tiles);
progress.set_update();
if(!pause)
break;
}
}
if(progress.get_cancel())
break;
}
if(!no_tiles) {
/* update scene */
update_scene();
if(progress.get_cancel())
break;
}
if(!no_tiles) {
/* buffers mutex is locked entirely while rendering each
pass, 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();
/* path trace */
foreach(Tile& tile, tile_manager.state.tiles) {
path_trace(tile);
device->task_wait();
if(progress.get_cancel())
break;
}
/* update status and timing */
update_status_time();
gpu_need_tonemap = true;
gpu_draw_ready = true;
progress.set_update();
/* wait for tonemap */
if(!params.background) {
while(gpu_need_tonemap) {
if(progress.get_cancel())
break;
gpu_need_tonemap_cond.wait(buffers_lock);
}
}
if(progress.get_cancel())
break;
}
}
}
/* CPU Session */
void Session::reset_cpu(int w, int h, int passes)
{
thread_scoped_lock reset_lock(delayed_reset.mutex);
display_outdated = true;
reset_time = time_dt();
delayed_reset.w = w;
delayed_reset.h = h;
delayed_reset.passes = passes;
delayed_reset.do_reset = true;
device->task_cancel();
pause_cond.notify_all();
}
bool Session::draw_cpu(int w, int h)
{
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(w == display->width && h == display->height) {
display->draw(device);
if(display_outdated && (time_dt() - reset_time) > params.text_timeout)
return false;
return true;
}
}
return false;
}
void Session::run_cpu()
{
{
/* 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.w, delayed_reset.h, delayed_reset.passes);
delayed_reset.do_reset = false;
}
while(!progress.get_cancel()) {
/* advance to next tile */
bool no_tiles = !tile_manager.next();
bool need_tonemap = false;
if(params.background) {
/* if no work left and in background mode, we can stop immediately */
if(no_tiles)
break;
}
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 || no_tiles) {
update_status_time(pause, no_tiles);
while(1) {
double pause_start = time_dt();
pause_cond.wait(pause_lock);
paused_time += time_dt() - pause_start;
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
pass, and released/reacquired on each iteration to allow
reset and draw in between */
thread_scoped_lock buffers_lock(buffers->mutex);
/* update scene */
update_scene();
if(progress.get_cancel())
break;
/* update status and timing */
update_status_time();
/* path trace */
foreach(Tile& tile, tile_manager.state.tiles)
path_trace(tile);
/* update status and timing */
update_status_time();
if(!params.background)
need_tonemap = true;
}
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.w, delayed_reset.h, delayed_reset.passes);
}
else if(need_tonemap) {
/* tonemap only if we do not reset, we don't we don't
want to show the result of an incomplete pass*/
tonemap();
}
}
progress.set_update();
}
}
void Session::run()
{
/* load kernels */
progress.set_status("Loading render kernels (may take a few minutes)");
if(!device->load_kernels()) {
progress.set_status("Failed loading render kernel, see console for errors");
progress.set_update();
return;
}
/* session thread loop */
progress.set_status("Waiting for render to start");
/* run */
if(!progress.get_cancel()) {
if(device_use_gl)
run_gpu();
else
run_cpu();
}
/* progress update */
if(progress.get_cancel())
progress.set_status(progress.get_cancel_message());
else
progress.set_update();
}
bool Session::draw(int w, int h)
{
if(device_use_gl)
return draw_gpu(w, h);
else
return draw_cpu(w, h);
}
void Session::reset_(int w, int h, int passes)
{
if(w != buffers->width || h != buffers->height) {
gpu_draw_ready = false;
buffers->reset(device, w, h);
display->reset(device, w, h);
}
tile_manager.reset(w, h, passes);
start_time = time_dt();
preview_time = 0.0;
paused_time = 0.0;
pass = 0;
}
void Session::reset(int w, int h, int passes)
{
if(device_use_gl)
reset_gpu(w, h, passes);
else
reset_cpu(w, h, passes);
}
void Session::set_passes(int passes)
{
if(passes != params.passes) {
params.passes = passes;
tile_manager.set_passes(passes);
{
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()
{
session_thread->join();
delete session_thread;
session_thread = NULL;
}
void Session::update_scene()
{
thread_scoped_lock scene_lock(scene->mutex);
progress.set_status("Updating Scene");
/* update camera if dimensions changed for progressive render */
Camera *cam = scene->camera;
int w = tile_manager.state.width;
int h = tile_manager.state.height;
if(cam->width != w || cam->height != h) {
cam->width = w;
cam->height = h;
cam->tag_update();
}
/* update scene */
if(scene->need_update())
scene->device_update(device, progress);
}
void Session::update_status_time(bool show_pause, bool show_done)
{
int pass = tile_manager.state.pass;
int resolution = tile_manager.state.resolution;
/* update status */
string status, substatus;
if(!params.progressive)
substatus = "Path Tracing";
else if(params.passes == INT_MAX)
substatus = string_printf("Path Tracing Pass %d", pass+1);
else
substatus = string_printf("Path Tracing Pass %d/%d", pass+1, params.passes);
if(show_pause)
status = "Paused";
else if(show_done)
status = "Done";
else
status = "Rendering";
progress.set_status(status, substatus);
/* update timing */
if(preview_time == 0.0 && resolution == 1)
preview_time = time_dt();
double total_time = time_dt() - start_time - paused_time;
double pass_time = (pass == 0)? 0.0: (time_dt() - preview_time - paused_time)/(pass);
/* negative can happen when we pause a bit before rendering, can discard that */
if(total_time < 0.0) total_time = 0.0;
if(preview_time < 0.0) preview_time = 0.0;
progress.set_pass(pass + 1, total_time, pass_time);
}
void Session::path_trace(Tile& tile)
{
/* add path trace task */
DeviceTask task(DeviceTask::PATH_TRACE);
task.x = tile.x;
task.y = tile.y;
task.w = tile.w;
task.h = tile.h;
task.buffer = buffers->buffer.device_pointer;
task.rng_state = buffers->rng_state.device_pointer;
task.pass = tile_manager.state.pass;
task.resolution = tile_manager.state.resolution;
device->task_add(task);
}
void Session::tonemap()
{
/* add tonemap task */
DeviceTask task(DeviceTask::TONEMAP);
task.x = 0;
task.y = 0;
task.w = tile_manager.state.width;
task.h = tile_manager.state.height;
task.rgba = display->rgba.device_pointer;
task.buffer = buffers->buffer.device_pointer;
task.pass = tile_manager.state.pass;
task.resolution = tile_manager.state.resolution;
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;
}
CCL_NAMESPACE_END
|