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