Cycles: memory usage report
[blender.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_stats.h"
28 #include "util_thread.h"
29 #include "util_vector.h"
30
31 CCL_NAMESPACE_BEGIN
32
33 class BufferParams;
34 class Device;
35 class DeviceScene;
36 class DisplayBuffer;
37 class Progress;
38 class RenderBuffers;
39 class Scene;
40
41 /* Session Parameters */
42
43 class SessionParams {
44 public:
45         DeviceInfo device;
46         bool background;
47         bool progressive_refine;
48         string output_path;
49
50         bool progressive;
51         bool experimental;
52         int samples;
53         int2 tile_size;
54         int start_resolution;
55         int threads;
56
57         double cancel_timeout;
58         double reset_timeout;
59         double text_timeout;
60
61         enum { OSL, SVM } shadingsystem;
62
63         SessionParams()
64         {
65                 background = false;
66                 progressive_refine = false;
67                 output_path = "";
68
69                 progressive = false;
70                 experimental = false;
71                 samples = INT_MAX;
72                 tile_size = make_int2(64, 64);
73                 start_resolution = INT_MAX;
74                 threads = 0;
75
76                 cancel_timeout = 0.1;
77                 reset_timeout = 0.1;
78                 text_timeout = 1.0;
79
80                 shadingsystem = SVM;
81         }
82
83         bool modified(const SessionParams& params)
84         { return !(device.type == params.device.type
85                 && device.id == params.device.id
86                 && background == params.background
87                 && progressive_refine == params.progressive_refine
88                 && output_path == params.output_path
89                 /* && samples == params.samples */
90                 && progressive == params.progressive
91                 && experimental == params.experimental
92                 && tile_size == params.tile_size
93                 && start_resolution == params.start_resolution
94                 && threads == params.threads
95                 && cancel_timeout == params.cancel_timeout
96                 && reset_timeout == params.reset_timeout
97                 && text_timeout == params.text_timeout
98                 && shadingsystem == params.shadingsystem); }
99
100 };
101
102 /* Session
103  *
104  * This is the class that contains the session thread, running the render
105  * control loop and dispatching tasks. */
106
107 class Session {
108 public:
109         Device *device;
110         Scene *scene;
111         RenderBuffers *buffers;
112         DisplayBuffer *display;
113         Progress progress;
114         SessionParams params;
115         TileManager tile_manager;
116         Stats stats;
117
118         boost::function<void(RenderTile&)> write_render_tile_cb;
119         boost::function<void(RenderTile&)> update_render_tile_cb;
120
121         Session(const SessionParams& params);
122         ~Session();
123
124         void start();
125         bool draw(BufferParams& params);
126         void wait();
127
128         bool ready_to_reset();
129         void reset(BufferParams& params, int samples);
130         void set_samples(int samples);
131         void set_pause(bool pause);
132
133 protected:
134         struct DelayedReset {
135                 thread_mutex mutex;
136                 bool do_reset;
137                 BufferParams params;
138                 int samples;
139         } delayed_reset;
140
141         void run();
142
143         void update_scene();
144         void update_status_time(bool show_pause = false, bool show_done = false);
145
146         void tonemap();
147         void path_trace();
148         void reset_(BufferParams& params, int samples);
149
150         void run_cpu();
151         bool draw_cpu(BufferParams& params);
152         void reset_cpu(BufferParams& params, int samples);
153
154         void run_gpu();
155         bool draw_gpu(BufferParams& params);
156         void reset_gpu(BufferParams& params, int samples);
157
158         bool acquire_tile(Device *tile_device, RenderTile& tile);
159         void update_tile_sample(RenderTile& tile);
160         void release_tile(RenderTile& tile);
161
162         void update_progress_sample();
163
164         bool device_use_gl;
165
166         thread *session_thread;
167
168         volatile bool display_outdated;
169
170         volatile bool gpu_draw_ready;
171         volatile bool gpu_need_tonemap;
172         thread_condition_variable gpu_need_tonemap_cond;
173
174         bool pause;
175         thread_condition_variable pause_cond;
176         thread_mutex pause_mutex;
177         thread_mutex tile_mutex;
178         thread_mutex buffers_mutex;
179         thread_mutex display_mutex;
180
181         bool kernels_loaded;
182
183         double start_time;
184         double reset_time;
185         double preview_time;
186         double paused_time;
187
188         /* progressive refine */
189         double last_update_time;
190         bool update_progressive_refine(bool cancel);
191
192         vector<RenderBuffers *> tile_buffers;
193 };
194
195 CCL_NAMESPACE_END
196
197 #endif /* __SESSION_H__ */
198