Blender V4.3
progress.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#ifndef __UTIL_PROGRESS_H__
6#define __UTIL_PROGRESS_H__
7
8/* Progress
9 *
10 * Simple class to communicate progress status messages, timing information,
11 * update notifications from a job running in another thread. All methods
12 * except for the constructor/destructor are thread safe. */
13
14#include "util/function.h"
15#include "util/string.h"
16#include "util/thread.h"
17#include "util/time.h"
18
20
21class Progress {
22 public:
24 {
25 pixel_samples = 0;
32 time_limit = 0.0;
33 end_time = 0.0;
34 status = "Initializing";
35 substatus = "";
36 sync_status = "";
37 sync_substatus = "";
39 cancel = false;
40 cancel_message = "";
41 error = false;
42 error_message = "";
44 }
45
46 Progress(Progress &progress)
47 {
48 *this = progress;
49 }
50
52 {
54
55 progress.get_status(status, substatus);
56
60
61 return *this;
62 }
63
64 void reset()
65 {
66 pixel_samples = 0;
73 time_limit = 0.0;
74 end_time = 0.0;
75 status = "Initializing";
76 substatus = "";
77 sync_status = "";
78 sync_substatus = "";
79 cancel = false;
80 cancel_message = "";
81 error = false;
82 error_message = "";
83 }
84
85 /* cancel */
86 void set_cancel(const string &cancel_message_)
87 {
89 cancel_message = cancel_message_;
90 cancel = true;
91 }
92
93 bool get_cancel() const
94 {
95 if (!cancel && cancel_cb)
96 cancel_cb();
97
98 return cancel;
99 }
100
101 string get_cancel_message() const
102 {
104 return cancel_message;
105 }
106
107 void set_cancel_callback(function<void()> function)
108 {
109 cancel_cb = function;
110 }
111
112 /* error */
113 void set_error(const string &error_message_)
114 {
116 error_message = error_message_;
117 error = true;
118 /* If error happens we also stop rendering. */
119 cancel_message = error_message_;
120 cancel = true;
121 }
122
123 bool get_error() const
124 {
125 return error;
126 }
127
128 string get_error_message() const
129 {
131 return error_message;
132 }
133
134 /* tile and timing information */
135
137 {
139
141 end_time = 0.0;
142 }
143
150
151 void set_time_limit(double time_limit_)
152 {
154
155 time_limit = time_limit_;
156 }
157
158 void add_skip_time(const scoped_timer &start_timer, bool only_render)
159 {
160 double skip_time = time_dt() - start_timer.get_start();
161
162 render_start_time += skip_time;
163 if (!only_render) {
164 start_time += skip_time;
165 }
166 }
167
168 void get_time(double &total_time_, double &render_time_) const
169 {
171
172 double time = (end_time > 0) ? end_time : time_dt();
173
174 total_time_ = time - start_time;
175 render_time_ = time - render_start_time;
176 }
177
179 {
180 end_time = time_dt();
181 }
182
192
193 void set_total_pixel_samples(uint64_t total_pixel_samples_)
194 {
196
197 total_pixel_samples = total_pixel_samples_;
198 }
199
200 double get_progress() const
201 {
203
204 if (pixel_samples > 0) {
205 double progress_percent = (double)pixel_samples / (double)total_pixel_samples;
206 if (time_limit != 0.0) {
207 double time_since_render_start = time_dt() - render_start_time;
208 progress_percent = max(progress_percent, time_since_render_start / time_limit);
209 }
210 return min(1.0, progress_percent);
211 }
212 return 0.0;
213 }
214
215 void add_samples(uint64_t pixel_samples_, int tile_sample)
216 {
218
219 pixel_samples += pixel_samples_;
220 current_tile_sample = tile_sample;
221 }
222
223 void add_samples_update(uint64_t pixel_samples_, int tile_sample)
224 {
225 add_samples(pixel_samples_, tile_sample);
226 set_update();
227 }
228
229 void add_finished_tile(bool denoised)
230 {
232
233 if (denoised) {
235 }
236 else {
238 }
239 }
240
242 {
244 /* Note that the value here always belongs to the last tile that updated,
245 * so it's only useful if there is only one active tile. */
246 return current_tile_sample;
247 }
248
250 {
252 return rendered_tiles;
253 }
254
256 {
258 return denoised_tiles;
259 }
260
261 /* status messages */
262
263 void set_status(const string &status_, const string &substatus_ = "")
264 {
265 {
267 status = status_;
268 substatus = substatus_;
269 }
270
271 set_update();
272 }
273
274 void set_substatus(const string &substatus_)
275 {
276 {
278 substatus = substatus_;
279 }
280
281 set_update();
282 }
283
284 void set_sync_status(const string &status_, const string &substatus_ = "")
285 {
286 {
288 sync_status = status_;
289 sync_substatus = substatus_;
290 }
291
292 set_update();
293 }
294
295 void set_sync_substatus(const string &substatus_)
296 {
297 {
299 sync_substatus = substatus_;
300 }
301
302 set_update();
303 }
304
305 void get_status(string &status_, string &substatus_) const
306 {
308
309 if (sync_status != "") {
310 status_ = sync_status;
311 substatus_ = sync_substatus;
312 }
313 else {
314 status_ = status;
315 substatus_ = substatus;
316 }
317 }
318
319 /* callback */
320
322 {
323 if (update_cb) {
325 update_cb();
326 }
327 }
328
329 void set_update_callback(function<void()> function)
330 {
331 update_cb = function;
332 }
333
334 protected:
337 function<void()> update_cb;
338 function<void()> cancel_cb;
339
340 /* pixel_samples counts how many samples have been rendered over all pixel, not just per pixel.
341 * This makes the progress estimate more accurate when tiles with different sizes are used.
342 *
343 * total_pixel_samples is the total amount of pixel samples that will be rendered. */
345 /* Stores the current sample count of the last tile that called the update function.
346 * It's used to display the sample count if only one tile is active. */
348 /* Stores the number of tiles that's already finished.
349 * Used to determine whether all but the last tile are finished rendering,
350 * in which case the current_tile_sample is displayed. */
352
354 /* End time written when render is done, so it doesn't keep increasing on redraws. */
355 double end_time;
356
357 string status;
358 string substatus;
359
362
363 volatile bool cancel;
365
366 volatile bool error;
368};
369
371
372#endif /* __UTIL_PROGRESS_H__ */
typedef double(DMatrix)[4][4]
volatile int lock
Progress()
Definition progress.h:23
double time_limit
Definition progress.h:353
function< void()> update_cb
Definition progress.h:337
void set_cancel_callback(function< void()> function)
Definition progress.h:107
int denoised_tiles
Definition progress.h:351
string sync_substatus
Definition progress.h:361
void get_status(string &status_, string &substatus_) const
Definition progress.h:305
int get_current_sample() const
Definition progress.h:241
Progress(Progress &progress)
Definition progress.h:46
void set_total_pixel_samples(uint64_t total_pixel_samples_)
Definition progress.h:193
void set_cancel(const string &cancel_message_)
Definition progress.h:86
string status
Definition progress.h:357
volatile bool error
Definition progress.h:366
int rendered_tiles
Definition progress.h:351
void set_end_time()
Definition progress.h:178
int current_tile_sample
Definition progress.h:347
string get_cancel_message() const
Definition progress.h:101
void set_substatus(const string &substatus_)
Definition progress.h:274
void get_time(double &total_time_, double &render_time_) const
Definition progress.h:168
void set_sync_substatus(const string &substatus_)
Definition progress.h:295
bool get_cancel() const
Definition progress.h:93
double start_time
Definition progress.h:353
string error_message
Definition progress.h:367
thread_mutex update_mutex
Definition progress.h:336
void reset_sample()
Definition progress.h:183
void set_status(const string &status_, const string &substatus_="")
Definition progress.h:263
double render_start_time
Definition progress.h:353
string substatus
Definition progress.h:358
void set_time_limit(double time_limit_)
Definition progress.h:151
void set_update()
Definition progress.h:321
int get_denoised_tiles() const
Definition progress.h:255
string cancel_message
Definition progress.h:364
void set_sync_status(const string &status_, const string &substatus_="")
Definition progress.h:284
volatile bool cancel
Definition progress.h:363
void reset()
Definition progress.h:64
void add_samples(uint64_t pixel_samples_, int tile_sample)
Definition progress.h:215
uint64_t total_pixel_samples
Definition progress.h:344
void set_error(const string &error_message_)
Definition progress.h:113
double get_progress() const
Definition progress.h:200
thread_mutex progress_mutex
Definition progress.h:335
bool get_error() const
Definition progress.h:123
double end_time
Definition progress.h:355
void add_samples_update(uint64_t pixel_samples_, int tile_sample)
Definition progress.h:223
void set_render_start_time()
Definition progress.h:144
void add_finished_tile(bool denoised)
Definition progress.h:229
uint64_t pixel_samples
Definition progress.h:344
string get_error_message() const
Definition progress.h:128
string sync_status
Definition progress.h:360
void add_skip_time(const scoped_timer &start_timer, bool only_render)
Definition progress.h:158
int get_rendered_tiles() const
Definition progress.h:249
Progress & operator=(Progress &progress)
Definition progress.h:51
void set_start_time()
Definition progress.h:136
function< void()> cancel_cb
Definition progress.h:338
void set_update_callback(function< void()> function)
Definition progress.h:329
double get_start() const
Definition time.h:37
#define CCL_NAMESPACE_END
#define function_null
Definition function.h:13
#define min(a, b)
Definition sort.c:32
unsigned __int64 uint64_t
Definition stdint.h:90
std::unique_lock< std::mutex > thread_scoped_lock
Definition thread.h:30
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition thread.h:29
CCL_NAMESPACE_BEGIN double time_dt()
Definition time.cpp:36
float max