Merge branch 'master' into blender2.8
[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 "render/buffers.h"
21 #include "device/device.h"
22 #include "render/shader.h"
23 #include "render/stats.h"
24 #include "render/tile.h"
25
26 #include "util/util_progress.h"
27 #include "util/util_stats.h"
28 #include "util/util_thread.h"
29 #include "util/util_vector.h"
30
31 CCL_NAMESPACE_BEGIN
32
33 class BufferParams;
34 class Device;
35 class DeviceScene;
36 class DeviceRequestedFeatures;
37 class DisplayBuffer;
38 class Progress;
39 class RenderBuffers;
40 class Scene;
41
42 /* Session Parameters */
43
44 class SessionParams {
45 public:
46         DeviceInfo device;
47         bool background;
48         bool progressive_refine;
49
50         bool progressive;
51         bool experimental;
52         int samples;
53         int2 tile_size;
54         TileOrder tile_order;
55         int start_resolution;
56         int pixel_size;
57         int threads;
58
59         bool use_profiling;
60
61         bool display_buffer_linear;
62
63         bool use_denoising;
64         bool denoising_passes;
65         int denoising_radius;
66         float denoising_strength;
67         float denoising_feature_strength;
68         bool denoising_relative_pca;
69
70         double cancel_timeout;
71         double reset_timeout;
72         double text_timeout;
73         double progressive_update_timeout;
74
75         ShadingSystem shadingsystem;
76
77         function<bool(const uchar *pixels,
78                       int width,
79                       int height,
80                       int channels)> write_render_cb;
81
82         SessionParams()
83         {
84                 background = false;
85                 progressive_refine = false;
86
87                 progressive = false;
88                 experimental = false;
89                 samples = INT_MAX;
90                 tile_size = make_int2(64, 64);
91                 start_resolution = INT_MAX;
92                 pixel_size = 1;
93                 threads = 0;
94
95                 use_profiling = false;
96
97                 use_denoising = false;
98                 denoising_passes = false;
99                 denoising_radius = 8;
100                 denoising_strength = 0.0f;
101                 denoising_feature_strength = 0.0f;
102                 denoising_relative_pca = false;
103
104                 display_buffer_linear = false;
105
106                 cancel_timeout = 0.1;
107                 reset_timeout = 0.1;
108                 text_timeout = 1.0;
109                 progressive_update_timeout = 1.0;
110
111                 shadingsystem = SHADINGSYSTEM_SVM;
112                 tile_order = TILE_CENTER;
113         }
114
115         bool modified(const SessionParams& params)
116         { return !(device == params.device
117                 && background == params.background
118                 && progressive_refine == params.progressive_refine
119                 /* && samples == params.samples */
120                 && progressive == params.progressive
121                 && experimental == params.experimental
122                 && tile_size == params.tile_size
123                 && start_resolution == params.start_resolution
124                 && pixel_size == params.pixel_size
125                 && threads == params.threads
126                 && use_profiling == params.use_profiling
127                 && display_buffer_linear == params.display_buffer_linear
128                 && cancel_timeout == params.cancel_timeout
129                 && reset_timeout == params.reset_timeout
130                 && text_timeout == params.text_timeout
131                 && progressive_update_timeout == params.progressive_update_timeout
132                 && tile_order == params.tile_order
133                 && shadingsystem == params.shadingsystem); }
134
135 };
136
137 /* Session
138  *
139  * This is the class that contains the session thread, running the render
140  * control loop and dispatching tasks. */
141
142 class Session {
143 public:
144         Device *device;
145         Scene *scene;
146         RenderBuffers *buffers;
147         DisplayBuffer *display;
148         Progress progress;
149         SessionParams params;
150         TileManager tile_manager;
151         Stats stats;
152
153         function<void(RenderTile&)> write_render_tile_cb;
154         function<void(RenderTile&, bool)> update_render_tile_cb;
155
156         explicit Session(const SessionParams& params);
157         ~Session();
158
159         void start();
160         bool draw(BufferParams& params, DeviceDrawParams& draw_params);
161         void wait();
162
163         bool ready_to_reset();
164         void reset(BufferParams& params, int samples);
165         void set_samples(int samples);
166         void set_pause(bool pause);
167
168         bool update_scene();
169         void load_kernels(bool lock_scene=true);
170
171         void device_free();
172
173         /* Returns the rendering progress or 0 if no progress can be determined
174          * (for example, when rendering with unlimited samples). */
175         float get_progress();
176
177         void collect_statistics(RenderStats *stats);
178
179 protected:
180         struct DelayedReset {
181                 thread_mutex mutex;
182                 bool do_reset;
183                 BufferParams params;
184                 int samples;
185         } delayed_reset;
186
187         void run();
188
189         void update_status_time(bool show_pause = false, bool show_done = false);
190
191         void tonemap(int sample);
192         void render();
193         void reset_(BufferParams& params, int samples);
194
195         void run_cpu();
196         bool draw_cpu(BufferParams& params, DeviceDrawParams& draw_params);
197         void reset_cpu(BufferParams& params, int samples);
198
199         void run_gpu();
200         bool draw_gpu(BufferParams& params, DeviceDrawParams& draw_params);
201         void reset_gpu(BufferParams& params, int samples);
202
203         bool acquire_tile(Device *tile_device, RenderTile& tile);
204         void update_tile_sample(RenderTile& tile);
205         void release_tile(RenderTile& tile);
206
207         void map_neighbor_tiles(RenderTile *tiles, Device *tile_device);
208         void unmap_neighbor_tiles(RenderTile *tiles, Device *tile_device);
209
210         bool device_use_gl;
211
212         thread *session_thread;
213
214         volatile bool display_outdated;
215
216         volatile bool gpu_draw_ready;
217         volatile bool gpu_need_tonemap;
218         thread_condition_variable gpu_need_tonemap_cond;
219
220         bool pause;
221         thread_condition_variable pause_cond;
222         thread_mutex pause_mutex;
223         thread_mutex tile_mutex;
224         thread_mutex buffers_mutex;
225         thread_mutex display_mutex;
226
227         bool kernels_loaded;
228         DeviceRequestedFeatures loaded_kernel_features;
229
230         double reset_time;
231
232         /* progressive refine */
233         double last_update_time;
234         bool update_progressive_refine(bool cancel);
235
236         DeviceRequestedFeatures get_requested_device_features();
237
238         /* ** Split kernel routines ** */
239
240         /* Maximumnumber of closure during session lifetime. */
241         int max_closure_global;
242
243         /* Get maximum number of closures to be used in kernel. */
244         int get_max_closure_count();
245 };
246
247 CCL_NAMESPACE_END
248
249 #endif  /* __SESSION_H__ */