Cycles: merge of changes from tomato branch.
[blender-staging.git] / intern / cycles / render / session.h
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #ifndef __SESSION_H__
20 #define __SESSION_H__
21
22 #include "buffers.h"
23 #include "device.h"
24 #include "tile.h"
25
26 #include "util_progress.h"
27 #include "util_thread.h"
28
29 CCL_NAMESPACE_BEGIN
30
31 class BufferParams;
32 class Device;
33 class DeviceScene;
34 class DisplayBuffer;
35 class Progress;
36 class RenderBuffers;
37 class Scene;
38
39 /* Session Parameters */
40
41 class SessionParams {
42 public:
43         DeviceInfo device;
44         bool background;
45         string output_path;
46
47         bool progressive;
48         bool experimental;
49         int samples;
50         int2 tile_size;
51         int resolution;
52         int threads;
53
54         double cancel_timeout;
55         double reset_timeout;
56         double text_timeout;
57
58         SessionParams()
59         {
60                 background = false;
61                 output_path = "";
62
63                 progressive = false;
64                 experimental = false;
65                 samples = INT_MAX;
66                 tile_size = make_int2(64, 64);
67                 resolution = 4;
68                 threads = 0;
69
70                 cancel_timeout = 0.1;
71                 reset_timeout = 0.1;
72                 text_timeout = 1.0;
73         }
74
75         bool modified(const SessionParams& params)
76         { return !(device.type == params.device.type
77                 && device.id == params.device.id
78                 && background == params.background
79                 && output_path == params.output_path
80                 /* && samples == params.samples */
81                 && progressive == params.progressive
82                 && experimental == params.experimental
83                 && tile_size == params.tile_size
84                 && resolution == params.resolution
85                 && threads == params.threads
86                 && cancel_timeout == params.cancel_timeout
87                 && reset_timeout == params.reset_timeout
88                 && text_timeout == params.text_timeout); }
89
90 };
91
92 /* Session
93  *
94  * This is the class that contains the session thread, running the render
95  * control loop and dispatching tasks. */
96
97 class Session {
98 public:
99         Device *device;
100         Scene *scene;
101         RenderBuffers *buffers;
102         DisplayBuffer *display;
103         Progress progress;
104         SessionParams params;
105         TileManager tile_manager;
106
107         boost::function<void(RenderTile&)> write_render_tile_cb;
108         boost::function<void(RenderTile&)> update_render_tile_cb;
109
110         Session(const SessionParams& params);
111         ~Session();
112
113         void start();
114         bool draw(BufferParams& params);
115         void wait();
116
117         bool ready_to_reset();
118         void reset(BufferParams& params, int samples);
119         void set_samples(int samples);
120         void set_pause(bool pause);
121
122 protected:
123         struct DelayedReset {
124                 thread_mutex mutex;
125                 bool do_reset;
126                 BufferParams params;
127                 int samples;
128         } delayed_reset;
129
130         void run();
131
132         void update_scene();
133         void update_status_time(bool show_pause = false, bool show_done = false);
134
135         void tonemap();
136         void path_trace();
137         void reset_(BufferParams& params, int samples);
138
139         void run_cpu();
140         bool draw_cpu(BufferParams& params);
141         void reset_cpu(BufferParams& params, int samples);
142
143         void run_gpu();
144         bool draw_gpu(BufferParams& params);
145         void reset_gpu(BufferParams& params, int samples);
146
147         bool acquire_tile(Device *tile_device, RenderTile& tile);
148         void update_tile_sample(RenderTile& tile);
149         void release_tile(RenderTile& tile);
150
151         void update_progress_sample();
152
153         bool device_use_gl;
154
155         thread *session_thread;
156
157         volatile bool display_outdated;
158
159         volatile bool gpu_draw_ready;
160         volatile bool gpu_need_tonemap;
161         thread_condition_variable gpu_need_tonemap_cond;
162
163         bool pause;
164         thread_condition_variable pause_cond;
165         thread_mutex pause_mutex;
166         thread_mutex tile_mutex;
167         thread_mutex buffers_mutex;
168         thread_mutex display_mutex;
169
170         bool kernels_loaded;
171
172         double start_time;
173         double reset_time;
174         double preview_time;
175         double paused_time;
176 };
177
178 CCL_NAMESPACE_END
179
180 #endif /* __SESSION_H__ */
181