Blender V5.3
scene_fps.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <algorithm>
12#include <cmath>
13
14#include "BLI_math_base_c.hh"
15
16#include "DNA_scene_types.h"
17
18#include "ED_scene.hh"
19
20#include "MEM_guardedalloc.h"
21
22namespace blender {
23
47
49{
50 if (scene->fps_info == nullptr) {
51 return;
52 }
53 MEM_delete(static_cast<ScreenFrameRateInfo *>(scene->fps_info));
54 scene->fps_info = nullptr;
55}
56
57void ED_scene_fps_average_accumulate(Scene *scene, const short fps_samples, const double ltime)
58{
59 const float fps_target = float(scene->frames_per_second());
60 /* Averaging intervals needs +1 timestamps.
61 * Needed because the intervals are computed *between* the time-steps. */
62 const int times_num = ((fps_samples > 0) ? fps_samples : std::max(1, int(ceilf(fps_target)))) +
63 1;
64
65 ScreenFrameRateInfo *fpsi = static_cast<ScreenFrameRateInfo *>(scene->fps_info);
66 if (fpsi) {
67 /* Reset when the target FPS changes.
68 * Needed redraw times from when a different FPS was set do not contribute
69 * to an average that is over/under the new target. */
70 if ((fpsi->fps_target != fps_target) || (fpsi->times_num != times_num)) {
71 MEM_delete(fpsi);
72 fpsi = nullptr;
73 scene->fps_info = nullptr;
74 }
75 }
76
77 /* If there isn't any info, initialize it first. */
78 if (fpsi == nullptr) {
80 (sizeof(ScreenFrameRateInfo::times[0]) * times_num),
81 __func__);
82 fpsi = static_cast<ScreenFrameRateInfo *>(scene->fps_info);
83 fpsi->fps_target = fps_target;
84 fpsi->times_num = times_num;
85
86 /* Use 100 for 2 decimal places (currently used for FPS display), could be configurable. */
87 const double decimal_places = 100.0;
88 fpsi->fps_target_is_fractional = std::round(fps_target) !=
89 (std::round(fps_target * decimal_places) / decimal_places);
90 }
91
92 fpsi->times[fpsi->times_index] = ltime;
93 fpsi->times_index = (fpsi->times_index + 1) % fpsi->times_num;
94 if (fpsi->times_num_set < fpsi->times_num) {
95 fpsi->times_num_set++;
96 }
97
98 /* Mark as outdated. */
99 fpsi->fps_average = -1.0f;
100}
101
103{
104 ScreenFrameRateInfo *fpsi = static_cast<ScreenFrameRateInfo *>(scene->fps_info);
105 if (fpsi == nullptr) {
106 return false;
107 }
108
109 /* Need at least two timestamps to define an interval. */
110 if (fpsi->times_num_set < 2) [[unlikely]] {
111 return false;
112 }
113
114 if (fpsi->fps_average == -1.0f) {
115 const int newest_index = ((fpsi->times_index - 1) + fpsi->times_num) % fpsi->times_num;
116 const int oldest_index = (fpsi->times_num_set < fpsi->times_num) ? 0 : fpsi->times_index;
117 const double delta = fpsi->times[newest_index] - fpsi->times[oldest_index];
118 BLI_assert(delta >= 0.0);
119 fpsi->fps_average = (delta > 0.0) ? float(double(fpsi->times_num_set - 1) / delta) : 0.0f;
120 }
121
122 r_state->fps_average = fpsi->fps_average;
123 r_state->fps_target = fpsi->fps_target;
125 return true;
126}
127
128} // namespace blender
Read Blender memory allocation and freeing.
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void ED_scene_fps_average_accumulate(Scene *scene, short fps_samples, double ltime) ATTR_NONNULL(1)
Definition scene_fps.cc:57
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
bool ED_scene_fps_average_calc(const Scene *scene, SceneFPS_State *r_state) ATTR_NONNULL(1
Definition scene_fps.cc:102
bool void ED_scene_fps_average_clear(Scene *scene) ATTR_NONNULL(1)
Definition scene_fps.cc:48
#define ceilf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float