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