OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2009, Google Inc. | 2 * Copyright 2009, Google Inc. |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
7 * met: | 7 * met: |
8 * | 8 * |
9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 * @param {!function(): void} decreaseQuality a function to decrease | 49 * @param {!function(): void} decreaseQuality a function to decrease |
50 * quality to try to raise our rendering speed. | 50 * quality to try to raise our rendering speed. |
51 * @param {!o3djs.performance.PerformanceMonitor.Options} opt_options Options. | 51 * @param {!o3djs.performance.PerformanceMonitor.Options} opt_options Options. |
52 * @return {!o3djs.performance.PerformanceMonitor} The created | 52 * @return {!o3djs.performance.PerformanceMonitor} The created |
53 * PerformanceMonitor. | 53 * PerformanceMonitor. |
54 */ | 54 */ |
55 o3djs.performance.createPerformanceMonitor = function( | 55 o3djs.performance.createPerformanceMonitor = function( |
56 targetFPSMin, targetFPSMax, increaseQuality, decreaseQuality, opt_options) { | 56 targetFPSMin, targetFPSMax, increaseQuality, decreaseQuality, opt_options) { |
57 return new o3djs.performance.PerformanceMonitor(targetFPSMin, targetFPSMax, | 57 return new o3djs.performance.PerformanceMonitor(targetFPSMin, targetFPSMax, |
58 increaseQuality, decreaseQuality, opt_options); | 58 increaseQuality, decreaseQuality, opt_options); |
59 } | 59 }; |
60 | 60 |
61 /** | 61 /** |
62 * A class that monitors performance [in terms of FPS] and helps to adjust the | 62 * A class that monitors performance [in terms of FPS] and helps to adjust the |
63 * rendered scene accordingly. | 63 * rendered scene accordingly. |
64 * @constructor | 64 * @constructor |
65 * @param {number} targetFPSMin the minimum acceptable frame rate; if we're | 65 * @param {number} targetFPSMin the minimum acceptable frame rate; if we're |
66 * under this, try to decrease quality to improve performance. | 66 * under this, try to decrease quality to improve performance. |
67 * @param {number} targetFPSMax if we're over this, try to increase quality. | 67 * @param {number} targetFPSMax if we're over this, try to increase quality. |
68 * @param {function(): void} increaseQuality a function to increase | 68 * @param {function(): void} increaseQuality a function to increase |
69 * quality/lower FPS. | 69 * quality/lower FPS. |
70 * @param {function(): void} decreaseQuality a function to decrease | 70 * @param {function(): void} decreaseQuality a function to decrease |
71 * quality/raise FPS. | 71 * quality/raise FPS. |
72 * @param {!o3djs.performance.PerformanceMonitor.Options} opt_options Options. | 72 * @param {!o3djs.performance.PerformanceMonitor.Options} opt_options Options. |
73 */ | 73 */ |
74 o3djs.performance.PerformanceMonitor = function( | 74 o3djs.performance.PerformanceMonitor = function( |
75 targetFPSMin, targetFPSMax, increaseQuality, decreaseQuality, opt_options) { | 75 targetFPSMin, targetFPSMax, increaseQuality, decreaseQuality, opt_options) { |
76 opt_options = opt_options || {}; | 76 opt_options = opt_options || {}; |
77 | 77 |
78 /** | 78 /** |
79 * A function to increase quality/lower FPS. | 79 * A function to increase quality/lower FPS. |
80 * @type {function(): void} | 80 * @type {function(): void} |
81 */ | 81 */ |
82 this.increaseQuality = increaseQuality; | 82 this.increaseQuality = increaseQuality; |
83 | 83 |
84 /** | 84 /** |
85 * A function to decrease quality/raise FPS. | 85 * A function to decrease quality/raise FPS. |
86 * @type {function(): void} | 86 * @type {function(): void} |
87 */ | 87 */ |
88 this.decreaseQuality = decreaseQuality; | 88 this.decreaseQuality = decreaseQuality; |
89 | 89 |
90 /** | 90 /** |
91 * The mean time taken per frame so far, in seconds. This is only valid once | 91 * The mean time taken per frame so far, in seconds. This is only valid once |
92 * we've collected at least minSamples samples. | 92 * we've collected at least minSamples samples. |
93 * @type {number} | 93 * @type {number} |
94 */ | 94 */ |
95 this.meanFrameTime = 0; | 95 this.meanFrameTime = 0; |
96 | 96 |
97 /** | 97 /** |
98 * The number of samples we've collected so far, when that number is less than | 98 * The number of samples we've collected so far, when that number is less than |
99 * or equal to this.damping. After that point, we no longer update | 99 * or equal to this.damping. After that point, we no longer update |
100 * this.sampleCount, so it will clip at this.damping. | 100 * this.sampleCount, so it will clip at this.damping. |
101 * | 101 * |
102 * @type {number} | 102 * @type {number} |
103 */ | 103 */ |
104 this.sampleCount = 0; | 104 this.sampleCount = 0; |
105 | 105 |
106 /** | 106 /** |
107 * The minimum number of samples to collect before trying to adjust quality. | 107 * The minimum number of samples to collect before trying to adjust quality. |
108 * | 108 * |
109 * @type {number} | 109 * @type {number} |
110 */ | 110 */ |
111 this.minSamples = opt_options.opt_minSamples || 60; | 111 this.minSamples = opt_options.opt_minSamples || 60; |
112 | 112 |
113 /** | 113 /** |
114 * A number that controls the rate at which the effects of any given sample | 114 * A number that controls the rate at which the effects of any given sample |
115 * fade away. Higher is slower, but also means that each individual sample | 115 * fade away. Higher is slower, but also means that each individual sample |
116 * counts for less at its most-influential. Damping defaults to 120; anywhere | 116 * counts for less at its most-influential. Damping defaults to 120; anywhere |
117 * between 60 and 600 are probably reasonable values, depending on your needs, | 117 * between 60 and 600 are probably reasonable values, depending on your needs, |
118 * but the number must be no less than minSamples. | 118 * but the number must be no less than minSamples. |
119 * | 119 * |
120 * @type {number} | 120 * @type {number} |
121 */ | 121 */ |
122 this.damping = opt_options.opt_damping || 120; | 122 this.damping = opt_options.opt_damping || 120; |
123 | 123 |
124 /** | 124 /** |
125 * The minimum number of samples to take in between adjustments, to cut down | 125 * The minimum number of samples to take in between adjustments, to cut down |
126 * on overshoot. It defaults to 2 * minSamples. | 126 * on overshoot. It defaults to 2 * minSamples. |
127 * | 127 * |
128 * @type {number} | 128 * @type {number} |
129 */ | 129 */ |
130 this.delayCycles = opt_options.opt_delayCycles || 2 * this.minSamples; | 130 this.delayCycles = opt_options.opt_delayCycles || 2 * this.minSamples; |
131 | 131 |
132 this.targetFrameTimeMax_ = 1 / targetFPSMin; | 132 this.targetFrameTimeMax_ = 1 / targetFPSMin; |
133 this.targetFrameTimeMin_ = 1 / targetFPSMax; | 133 this.targetFrameTimeMin_ = 1 / targetFPSMax; |
134 this.scaleInput_ = 1 / this.minSamples; | 134 this.scaleInput_ = 1 / this.minSamples; |
135 this.scaleMean_ = 1; | 135 this.scaleMean_ = 1; |
136 this.delayCyclesLeft_ = 0; | 136 this.delayCyclesLeft_ = 0; |
137 if (this.damping < this.minSamples) { | 137 if (this.damping < this.minSamples) { |
138 throw Error('Damping must be at least minSamples.'); | 138 throw Error('Damping must be at least minSamples.'); |
139 } | 139 } |
140 } | 140 }; |
141 | 141 |
142 /** | 142 /** |
143 * Options for the PerformanceMonitor. | 143 * Options for the PerformanceMonitor. |
144 * | 144 * |
145 * opt_minSamples is the minimum number of samples to take before making any | 145 * opt_minSamples is the minimum number of samples to take before making any |
146 * performance adjustments. | 146 * performance adjustments. |
147 * opt_damping is a number that controls the rate at which the effects of any | 147 * opt_damping is a number that controls the rate at which the effects of any |
148 * given sample fade away. Higher is slower, but also means that each | 148 * given sample fade away. Higher is slower, but also means that each |
149 * individual sample counts for less at its most-influential. Damping | 149 * individual sample counts for less at its most-influential. Damping |
150 * defaults to 120; anywhere between 60 and 600 are probably reasonable values, | 150 * defaults to 120; anywhere between 60 and 600 are probably reasonable values, |
(...skipping 13 matching lines...) Expand all Loading... |
164 * Call this once per frame with the elapsed time since the last call, and it | 164 * Call this once per frame with the elapsed time since the last call, and it |
165 * will attempt to adjust your rendering quality as needed. | 165 * will attempt to adjust your rendering quality as needed. |
166 * | 166 * |
167 * @param {number} seconds the elapsed time since the last frame was rendered, | 167 * @param {number} seconds the elapsed time since the last frame was rendered, |
168 * in seconds. | 168 * in seconds. |
169 */ | 169 */ |
170 o3djs.performance.PerformanceMonitor.prototype.onRender = function(seconds) { | 170 o3djs.performance.PerformanceMonitor.prototype.onRender = function(seconds) { |
171 var test = true; | 171 var test = true; |
172 if (this.sampleCount < this.damping) { | 172 if (this.sampleCount < this.damping) { |
173 if (this.sampleCount >= this.minSamples) { | 173 if (this.sampleCount >= this.minSamples) { |
174 this.scaleInput_ = 1 / (this.sampleCount + 1); | 174 this.scaleInput_ = 1 / (this.sampleCount + 1); |
175 this.scaleMean_ = this.sampleCount * this.scaleInput_; | 175 this.scaleMean_ = this.sampleCount * this.scaleInput_; |
176 } else { | 176 } else { |
177 test = false; | 177 test = false; |
178 } | 178 } |
179 this.sampleCount += 1; | 179 this.sampleCount += 1; |
180 } | 180 } |
181 this.meanFrameTime = this.meanFrameTime * this.scaleMean_ + | 181 this.meanFrameTime = this.meanFrameTime * this.scaleMean_ + |
182 seconds * this.scaleInput_; | 182 seconds * this.scaleInput_; |
183 if (this.delayCyclesLeft_ > 0) { | 183 if (this.delayCyclesLeft_ > 0) { |
184 this.delayCyclesLeft_ -= 1; | 184 this.delayCyclesLeft_ -= 1; |
185 } else if (test) { | 185 } else if (test) { |
186 if (this.meanFrameTime < this.targetFrameTimeMin_) { | 186 if (this.meanFrameTime < this.targetFrameTimeMin_) { |
187 this.increaseQuality(); | 187 this.increaseQuality(); |
188 this.delayCyclesLeft_ = this.delayCycles; | 188 this.delayCyclesLeft_ = this.delayCycles; |
189 } else if (this.meanFrameTime > this.targetFrameTimeMax_) { | 189 } else if (this.meanFrameTime > this.targetFrameTimeMax_) { |
190 this.decreaseQuality(); | 190 this.decreaseQuality(); |
191 this.delayCyclesLeft_ = this.delayCycles; | 191 this.delayCyclesLeft_ = this.delayCycles; |
192 } | 192 } |
193 } | 193 } |
194 } | 194 }; |
195 | 195 |
| 196 |
OLD | NEW |