1e62515865244ea2bbd9872e663886c0e57efb08
[blender.git] / intern / cycles / render / session.h
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16
17 #ifndef __SESSION_H__
18 #define __SESSION_H__
19
20 #include "buffers.h"
21 #include "device.h"
22 #include "tile.h"
23
24 #include "util_progress.h"
25 #include "util_stats.h"
26 #include "util_thread.h"
27 #include "util_vector.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         bool progressive_refine;
46         string output_path;
47
48         bool progressive;
49         bool experimental;
50         int samples;
51         int2 tile_size;
52         TileOrder tile_order;
53         int start_resolution;
54         int threads;
55
56         bool display_buffer_linear;
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 = USHRT_MAX;
73                 tile_size = make_int2(64, 64);
74                 start_resolution = INT_MAX;
75                 threads = 0;
76
77                 display_buffer_linear = false;
78
79                 cancel_timeout = 0.1;
80                 reset_timeout = 0.1;
81                 text_timeout = 1.0;
82
83                 shadingsystem = SVM;
84                 tile_order = TILE_CENTER;
85         }
86
87         bool modified(const SessionParams& params)
88         { return !(device.type == params.device.type
89                 && device.id == params.device.id
90                 && background == params.background
91                 && progressive_refine == params.progressive_refine
92                 && output_path == params.output_path
93                 /* && samples == params.samples */
94                 && progressive == params.progressive
95                 && experimental == params.experimental
96                 && tile_size == params.tile_size
97                 && start_resolution == params.start_resolution
98                 && threads == params.threads
99                 && display_buffer_linear == params.display_buffer_linear
100                 && cancel_timeout == params.cancel_timeout
101                 && reset_timeout == params.reset_timeout
102                 && text_timeout == params.text_timeout
103                 && tile_order == params.tile_order
104                 && shadingsystem == params.shadingsystem); }
105
106 };
107
108 /* Session
109  *
110  * This is the class that contains the session thread, running the render
111  * control loop and dispatching tasks. */
112
113 class Session {
114 public:
115         Device *device;
116         Scene *scene;
117         RenderBuffers *buffers;
118         DisplayBuffer *display;
119         Progress progress;
120         SessionParams params;
121         TileManager tile_manager;
122         Stats stats;
123
124         boost::function<void(RenderTile&)> write_render_tile_cb;
125         boost::function<void(RenderTile&)> update_render_tile_cb;
126
127         Session(const SessionParams& params);
128         ~Session();
129
130         void start();
131         bool draw(BufferParams& params, DeviceDrawParams& draw_params);
132         void wait();
133
134         bool ready_to_reset();
135         void reset(BufferParams& params, int samples);
136         void set_samples(int samples);
137         void set_pause(bool pause);
138
139         void update_scene();
140         void device_free();
141 protected:
142         struct DelayedReset {
143                 thread_mutex mutex;
144                 bool do_reset;
145                 BufferParams params;
146                 int samples;
147         } delayed_reset;
148
149         void run();
150
151         void update_status_time(bool show_pause = false, bool show_done = false);
152
153         void tonemap(int sample);
154         void path_trace();
155         void reset_(BufferParams& params, int samples);
156
157         void run_cpu();
158         bool draw_cpu(BufferParams& params, DeviceDrawParams& draw_params);
159         void reset_cpu(BufferParams& params, int samples);
160
161         void run_gpu();
162         bool draw_gpu(BufferParams& params, DeviceDrawParams& draw_params);
163         void reset_gpu(BufferParams& params, int samples);
164
165         bool acquire_tile(Device *tile_device, RenderTile& tile);
166         void update_tile_sample(RenderTile& tile);
167         void release_tile(RenderTile& tile);
168
169         void update_progress_sample();
170
171         bool device_use_gl;
172
173         thread *session_thread;
174
175         volatile bool display_outdated;
176
177         volatile bool gpu_draw_ready;
178         volatile bool gpu_need_tonemap;
179         thread_condition_variable gpu_need_tonemap_cond;
180
181         bool pause;
182         thread_condition_variable pause_cond;
183         thread_mutex pause_mutex;
184         thread_mutex tile_mutex;
185         thread_mutex buffers_mutex;
186         thread_mutex display_mutex;
187
188         bool kernels_loaded;
189
190         double start_time;
191         double reset_time;
192         double preview_time;
193         double paused_time;
194
195         /* progressive refine */
196         double last_update_time;
197         bool update_progressive_refine(bool cancel);
198
199         vector<RenderBuffers *> tile_buffers;
200 };
201
202 CCL_NAMESPACE_END
203
204 #endif /* __SESSION_H__ */
205