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