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