History

use full 'present' time for FPS calculation

also, refactor LOD adjust logic

Andrew Meadows committed 8 years ago

Commit 8e0d3671 parent 5c84c5f3 files · +67 −166Browse files

Showing the changes to interface/src/LODManager.cpp (1 of 3 files). Show all

Unified · Split

interface/src/LODManager.cpp+55−132
@@ -19,6 +19,11 @@ · 18 unmodified lines
19 19
20#include "LODManager.h"20#include "LODManager.h"
21 21
22const uint64_t LOD_AUTO_ADJUST_PERIOD = 500 * USECS_PER_MSEC;
23const float LOD_AUTO_ADJUST_DECREMENT_FACTOR = 0.8f;
24const float LOD_AUTO_ADJUST_INCREMENT_FACTOR = 1.2f;
25
26
22Setting::Handle<float> desktopLODDecreaseFPS("desktopLODDecreaseFPS", DEFAULT_DESKTOP_LOD_DOWN_FPS);27Setting::Handle<float> desktopLODDecreaseFPS("desktopLODDecreaseFPS", DEFAULT_DESKTOP_LOD_DOWN_FPS);
23Setting::Handle<float> hmdLODDecreaseFPS("hmdLODDecreaseFPS", DEFAULT_HMD_LOD_DOWN_FPS);28Setting::Handle<float> hmdLODDecreaseFPS("hmdLODDecreaseFPS", DEFAULT_HMD_LOD_DOWN_FPS);
24 29
@@ -39,156 +44,74 @@ float LODManager::getLODIncreaseFPS() { · 14 unmodified lines
39 return getDesktopLODIncreaseFPS();44 return getDesktopLODIncreaseFPS();
40}45}
41 46
42void LODManager::autoAdjustLOD(float batchTime, float engineRunTime, float deltaTimeSec) {47void LODManager::autoAdjustLOD(float presentTime, float engineRunTime, float deltaTimeSec) {
43
44 // NOTE: our first ~100 samples at app startup are completely all over the place, and we don't
45 // really want to count them in our average, so we will ignore the real frame rates and stuff
46 // our moving average with simulated good data
47 const int IGNORE_THESE_SAMPLES = 100;
48 if (_fpsAverageUpWindow.getSampleCount() < IGNORE_THESE_SAMPLES) {
49 _lastStable = _lastUpShift = _lastDownShift = usecTimestampNow();
50 }
51
52 // compute time-weighted running average renderTime48 // compute time-weighted running average renderTime
53 const float OVERLAY_AND_SWAP_TIME_BUDGET = 2.0f; // msec49 float maxTime = glm::max(presentTime, engineRunTime);
54 float renderTime = batchTime + OVERLAY_AND_SWAP_TIME_BUDGET;50 const float LOD_ADJUST_TIMESCALE = 0.1f; // sec
55 float maxTime = glm::max(renderTime, engineRunTime);51 float blend = (deltaTimeSec < LOD_ADJUST_TIMESCALE) ? deltaTimeSec / LOD_ADJUST_TIMESCALE : 1.0f;
56 const float BLEND_TIMESCALE = 0.3f; // sec
57 const float MIN_DELTA_TIME = 0.001f;
58 const float safeDeltaTime = glm::max(deltaTimeSec, MIN_DELTA_TIME);
59 float blend = BLEND_TIMESCALE / safeDeltaTime;
60 if (blend > 1.0f) {
61 blend = 1.0f;
62 }
63 _avgRenderTime = (1.0f - blend) * _avgRenderTime + blend * maxTime; // msec52 _avgRenderTime = (1.0f - blend) * _avgRenderTime + blend * maxTime; // msec
53 if (!_automaticLODAdjust) {
54 // early exit
55 return;
56 }
64 57
65 // translate into fps for legacy implementation58 float oldOctreeSizeScale = _octreeSizeScale;
66 float currentFPS = (float)MSECS_PER_SECOND / _avgRenderTime;59 float currentFPS = (float)MSECS_PER_SECOND / _avgRenderTime;
67 60 uint64_t now = usecTimestampNow();
68 _fpsAverageStartWindow.updateAverage(currentFPS);61 if (currentFPS < getLODDecreaseFPS()) {
69 _fpsAverageDownWindow.updateAverage(currentFPS);62 if (now > _decreaseFPSExpiry) {
70 _fpsAverageUpWindow.updateAverage(currentFPS);63 _decreaseFPSExpiry = now + LOD_AUTO_ADJUST_PERIOD;
71
72 quint64 now = usecTimestampNow();
73
74 quint64 elapsedSinceDownShift = now - _lastDownShift;
75 quint64 elapsedSinceUpShift = now - _lastUpShift;
76
77 quint64 lastStableOrUpshift = glm::max(_lastUpShift, _lastStable);
78 quint64 elapsedSinceStableOrUpShift = now - lastStableOrUpshift;
79
80 if (_automaticLODAdjust) {
81 bool changed = false;
82
83 // LOD Downward adjustment
84 // If we've been downshifting, we watch a shorter downshift window so that we will quickly move toward our
85 // target frame rate. But if we haven't just done a downshift (either because our last shift was an upshift,
86 // or because we've just started out) then we look at a much longer window to consider whether or not to start
87 // downshifting.
88 bool doDownShift = false;
89
90 if (_isDownshifting) {
91 // only consider things if our DOWN_SHIFT time has elapsed...
92 if (elapsedSinceDownShift > DOWN_SHIFT_ELPASED) {
93 doDownShift = _fpsAverageDownWindow.getAverage() < getLODDecreaseFPS();
94
95 if (!doDownShift) {
96 qCDebug(interfaceapp) << "---- WE APPEAR TO BE DONE DOWN SHIFTING -----";
97 _isDownshifting = false;
98 _lastStable = now;
99 }
100 }
101 } else {
102 doDownShift = (elapsedSinceStableOrUpShift > START_SHIFT_ELPASED
103 && _fpsAverageStartWindow.getAverage() < getLODDecreaseFPS());
104 }
105
106 if (doDownShift) {
107
108 // Octree items... stepwise adjustment
109 if (_octreeSizeScale > ADJUST_LOD_MIN_SIZE_SCALE) {64 if (_octreeSizeScale > ADJUST_LOD_MIN_SIZE_SCALE) {
110 _octreeSizeScale *= ADJUST_LOD_DOWN_BY;65 _octreeSizeScale *= LOD_AUTO_ADJUST_DECREMENT_FACTOR;
111 if (_octreeSizeScale < ADJUST_LOD_MIN_SIZE_SCALE) {66 if (_octreeSizeScale < ADJUST_LOD_MIN_SIZE_SCALE) {
112 _octreeSizeScale = ADJUST_LOD_MIN_SIZE_SCALE;67 _octreeSizeScale = ADJUST_LOD_MIN_SIZE_SCALE;
113 }68 }
114 changed = true;69 qCDebug(interfaceapp) << "adjusting LOD UP"
70 << "fps =" << currentFPS
71 << "targetFPS =" << getLODDecreaseFPS();
115 }72 }
116 73 _decreaseFPSExpiry = now + LOD_AUTO_ADJUST_PERIOD;
117 if (changed) {74 }
118 if (_isDownshifting) {75 _increaseFPSExpiry = now + LOD_AUTO_ADJUST_PERIOD;
119 // subsequent downshift76 } else if (currentFPS > getLODIncreaseFPS()) {
120 qCDebug(interfaceapp) << "adjusting LOD DOWN..."77 if (now > _increaseFPSExpiry) {
121 << "average fps for last "<< DOWN_SHIFT_WINDOW_IN_SECS <<"seconds was "78 _increaseFPSExpiry = now + LOD_AUTO_ADJUST_PERIOD;
122 << _fpsAverageDownWindow.getAverage()79 if (_octreeSizeScale < ADJUST_LOD_MAX_SIZE_SCALE) {
123 << "minimum is:" << getLODDecreaseFPS()80 if (_octreeSizeScale < ADJUST_LOD_MIN_SIZE_SCALE) {
124 << "elapsedSinceDownShift:" << elapsedSinceDownShift81 _octreeSizeScale = ADJUST_LOD_MIN_SIZE_SCALE;
125 << " NEW _octreeSizeScale=" << _octreeSizeScale;
126 } else {82 } else {
127 // first downshift83 _octreeSizeScale *= LOD_AUTO_ADJUST_INCREMENT_FACTOR;
128 qCDebug(interfaceapp) << "adjusting LOD DOWN after initial delay..."
129 << "average fps for last "<< START_DELAY_WINDOW_IN_SECS <<"seconds was "
130 << _fpsAverageStartWindow.getAverage()
131 << "minimum is:" << getLODDecreaseFPS()
132 << "elapsedSinceUpShift:" << elapsedSinceUpShift
133 << " NEW _octreeSizeScale=" << _octreeSizeScale;
134 }
135
136 _lastDownShift = now;
137 _isDownshifting = true;
138
139 emit LODDecreased();
140 }
141 } else {
142
143 // LOD Upward adjustment
144 if (elapsedSinceUpShift > UP_SHIFT_ELPASED) {
145
146 if (_fpsAverageUpWindow.getAverage() > getLODIncreaseFPS()) {
147
148 // Octee items... stepwise adjustment
149 if (_octreeSizeScale < ADJUST_LOD_MAX_SIZE_SCALE) {
150 if (_octreeSizeScale < ADJUST_LOD_MIN_SIZE_SCALE) {
151 _octreeSizeScale = ADJUST_LOD_MIN_SIZE_SCALE;
152 } else {
153 _octreeSizeScale *= ADJUST_LOD_UP_BY;
154 }
155 if (_octreeSizeScale > ADJUST_LOD_MAX_SIZE_SCALE) {
156 _octreeSizeScale = ADJUST_LOD_MAX_SIZE_SCALE;
157 }
158 changed = true;
159 }
160 }84 }
161 85 if (_octreeSizeScale > ADJUST_LOD_MAX_SIZE_SCALE) {
162 if (changed) {86 _octreeSizeScale = ADJUST_LOD_MAX_SIZE_SCALE;
163 qCDebug(interfaceapp) << "adjusting LOD UP... average fps for last "<< UP_SHIFT_WINDOW_IN_SECS <<"seconds was "
164 << _fpsAverageUpWindow.getAverage()
165 << "upshift point is:" << getLODIncreaseFPS()
166 << "elapsedSinceUpShift:" << elapsedSinceUpShift
167 << " NEW _octreeSizeScale=" << _octreeSizeScale;
168
169 _lastUpShift = now;
170 _isDownshifting = false;
171
172 emit LODIncreased();
173 }87 }
88 qCDebug(interfaceapp) << "adjusting LOD DOWN"
89 << "fps =" << currentFPS
90 << "targetFPS =" << getLODDecreaseFPS();
174 }91 }
92 _increaseFPSExpiry = now + LOD_AUTO_ADJUST_PERIOD;
175 }93 }
94 _decreaseFPSExpiry = now + LOD_AUTO_ADJUST_PERIOD;
95 } else {
96 _increaseFPSExpiry = now + LOD_AUTO_ADJUST_PERIOD;
97 _decreaseFPSExpiry = _increaseFPSExpiry;
98 }
176 99
177 if (changed) {100 if (oldOctreeSizeScale != _octreeSizeScale) {
178 auto lodToolsDialog = DependencyManager::get<DialogsManager>()->getLodToolsDialog();101 auto lodToolsDialog = DependencyManager::get<DialogsManager>()->getLodToolsDialog();
179 if (lodToolsDialog) {102 if (lodToolsDialog) {
180 lodToolsDialog->reloadSliders();103 lodToolsDialog->reloadSliders();
181 }
182 }104 }
105 // Assuming the LOD adjustment will work: we optimistically reset _avgRenderTime
106 // to be at middle of target zone. It will drift close to its true value within
107 // about three few LOD_ADJUST_TIMESCALEs and we'll adjust again as necessary.
108 float expectedFPS = 0.5f * (getLODIncreaseFPS() + getLODDecreaseFPS());
109 _avgRenderTime = MSECS_PER_SECOND / expectedFPS;
183 }110 }
184}111}
185 112
186void LODManager::resetLODAdjust() {113void LODManager::resetLODAdjust() {
187 _fpsAverageStartWindow.reset();114 _decreaseFPSExpiry = _increaseFPSExpiry = usecTimestampNow() + LOD_AUTO_ADJUST_PERIOD;
188 _fpsAverageDownWindow.reset();
189 _fpsAverageUpWindow.reset();
190 _lastUpShift = _lastDownShift = usecTimestampNow();
191 _isDownshifting = false;
192}115}
193 116
194const float MIN_DECREASE_FPS = 0.5f;117const float MIN_DECREASE_FPS = 0.5f;
@@ -206,7 +129,7 @@ float LODManager::getDesktopLODDecreaseFPS() const { · 11 unmodified lines
206}129}
207 130
208float LODManager::getDesktopLODIncreaseFPS() const {131float LODManager::getDesktopLODIncreaseFPS() const {
209 return glm::max(((float)MSECS_PER_SECOND / _desktopMaxRenderTime) + INCREASE_LOD_GAP, MAX_LIKELY_DESKTOP_FPS);132 return glm::max(((float)MSECS_PER_SECOND / _desktopMaxRenderTime) + INCREASE_LOD_GAP_FPS, MAX_LIKELY_DESKTOP_FPS);
210}133}
211 134
212void LODManager::setHMDLODDecreaseFPS(float fps) {135void LODManager::setHMDLODDecreaseFPS(float fps) {
@@ -222,7 +145,7 @@ float LODManager::getHMDLODDecreaseFPS() const { · 9 unmodified lines
222}145}
223 146
224float LODManager::getHMDLODIncreaseFPS() const {147float LODManager::getHMDLODIncreaseFPS() const {
225 return glm::max(((float)MSECS_PER_SECOND / _hmdMaxRenderTime) + INCREASE_LOD_GAP, MAX_LIKELY_HMD_FPS);148 return glm::max(((float)MSECS_PER_SECOND / _hmdMaxRenderTime) + INCREASE_LOD_GAP_FPS, MAX_LIKELY_HMD_FPS);
226}149}
227 150
228QString LODManager::getLODFeedbackText() {151QString LODManager::getLODFeedbackText() {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t