History

Merge branch 'master' of https://github.com/highfidelity/hifi into spectator-camera

Zach Fox committed 9 years ago

Commit ee866382 parents 8c36e5c 1ccf0da18 files · +392 −241Browse files

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

Unified · Split

interface/src/Application.cpp+315−120
@@ -25,6 +25,7 @@ · 24 unmodified lines
2525 #include <QtCore/QCommandLineParser>
2626 #include <QtCore/QMimeData>
2727 #include <QtCore/QThreadPool>
28+#include <QtConcurrent/QtConcurrentRun>
2829
2930 #include <QtGui/QScreen>
3031 #include <QtGui/QWindow>
@@ -481,6 +482,12 @@ bool setupEssentials(int& argc, char** argv, bool runningMarkerExisted) { · 450 unmodified lines
481482 static const auto SUPPRESS_SETTINGS_RESET = "--suppress-settings-reset";
482483 bool suppressPrompt = cmdOptionExists(argc, const_cast<const char**>(argv), SUPPRESS_SETTINGS_RESET);
483484 bool previousSessionCrashed = CrashHandler::checkForResetSettings(runningMarkerExisted, suppressPrompt);
485+ // get dir to use for cache
486+ static const auto CACHE_SWITCH = "--cache";
487+ QString cacheDir = getCmdOption(argc, const_cast<const char**>(argv), CACHE_SWITCH);
488+ if (!cacheDir.isEmpty()) {
489+ qApp->setProperty(hifi::properties::APP_LOCAL_DATA_PATH, cacheDir);
490+ }
484491
485492 Setting::init();
486493
@@ -1219,8 +1226,8 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer, bo · 732 unmodified lines
12191226 settingsTimer->stop();
12201227 // Delete it (this will trigger the thread destruction
12211228 settingsTimer->deleteLater();
1222− // Mark the settings thread as finished, so we know we can safely save in the main application
1223− // shutdown code
1229+ // Mark the settings thread as finished, so we know we can safely save in the main application
1230+ // shutdown code
12241231 _settingsGuard.trigger();
12251232 });
12261233
@@ -2166,48 +2173,74 @@ void Application::paintGL() { · 939 unmodified lines
21662173 return;
21672174 }
21682175
2169− auto displayPlugin = getActiveDisplayPlugin();
2170− // FIXME not needed anymore?
2171− _offscreenContext->makeCurrent();
2176+ DisplayPluginPointer displayPlugin;
2177+ {
2178+ PROFILE_RANGE(render, "/getActiveDisplayPlugin");
2179+ displayPlugin = getActiveDisplayPlugin();
2180+ }
21722181
2173− // If a display plugin loses it's underlying support, it
2174− // needs to be able to signal us to not use it
2175− if (!displayPlugin->beginFrameRender(_frameCount)) {
2176− _inPaint = false;
2177− updateDisplayMode();
2178− return;
2182+ {
2183+ PROFILE_RANGE(render, "/offscreenMakeCurrent");
2184+ // FIXME not needed anymore?
2185+ _offscreenContext->makeCurrent();
2186+ }
2187+
2188+ {
2189+ PROFILE_RANGE(render, "/pluginBeginFrameRender");
2190+ // If a display plugin loses it's underlying support, it
2191+ // needs to be able to signal us to not use it
2192+ if (!displayPlugin->beginFrameRender(_frameCount)) {
2193+ _inPaint = false;
2194+ updateDisplayMode();
2195+ return;
2196+ }
21792197 }
21802198
21812199 // update the avatar with a fresh HMD pose
2182− getMyAvatar()->updateFromHMDSensorMatrix(getHMDSensorPose());
2200+ {
2201+ PROFILE_RANGE(render, "/updateAvatar");
2202+ getMyAvatar()->updateFromHMDSensorMatrix(getHMDSensorPose());
2203+ }
21832204
21842205 auto lodManager = DependencyManager::get<LODManager>();
21852206
2207+ RenderArgs renderArgs;
21862208 {
2187− QMutexLocker viewLocker(&_viewMutex);
2188− _viewFrustum.calculate();
2209+ PROFILE_RANGE(render, "/buildFrustrumAndArgs");
2210+ {
2211+ QMutexLocker viewLocker(&_viewMutex);
2212+ _viewFrustum.calculate();
2213+ }
2214+ renderArgs = RenderArgs(_gpuContext, getEntities(), lodManager->getOctreeSizeScale(),
2215+ lodManager->getBoundaryLevelAdjust(), RenderArgs::DEFAULT_RENDER_MODE,
2216+ RenderArgs::MONO, RenderArgs::RENDER_DEBUG_NONE);
2217+ {
2218+ QMutexLocker viewLocker(&_viewMutex);
2219+ renderArgs.setViewFrustum(_viewFrustum);
2220+ }
21892221 }
2190− RenderArgs renderArgs(_gpuContext, getEntities(), lodManager->getOctreeSizeScale(),
2191− lodManager->getBoundaryLevelAdjust(), RenderArgs::DEFAULT_RENDER_MODE,
2192− RenderArgs::MONO, RenderArgs::RENDER_DEBUG_NONE);
2222+
21932223 {
2194− QMutexLocker viewLocker(&_viewMutex);
2195− renderArgs.setViewFrustum(_viewFrustum);
2224+ PROFILE_RANGE(render, "/resizeGL");
2225+ PerformanceWarning::setSuppressShortTimings(Menu::getInstance()->isOptionChecked(MenuOption::SuppressShortTimings));
2226+ bool showWarnings = Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings);
2227+ PerformanceWarning warn(showWarnings, "Application::paintGL()");
2228+ resizeGL();
21962229 }
21972230
2198− PerformanceWarning::setSuppressShortTimings(Menu::getInstance()->isOptionChecked(MenuOption::SuppressShortTimings));
2199− bool showWarnings = Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings);
2200− PerformanceWarning warn(showWarnings, "Application::paintGL()");
2201− resizeGL();
2231+ {
2232+ PROFILE_RANGE(render, "/gpuContextReset");
2233+ _gpuContext->beginFrame(getHMDSensorPose());
2234+ // Reset the gpu::Context Stages
2235+ // Back to the default framebuffer;
2236+ gpu::doInBatch(_gpuContext, [&](gpu::Batch& batch) {
2237+ batch.resetStages();
2238+ });
2239+ }
22022240
2203− _gpuContext->beginFrame(getHMDSensorPose());
2204− // Reset the gpu::Context Stages
2205− // Back to the default framebuffer;
2206− gpu::doInBatch(_gpuContext, [&](gpu::Batch& batch) {
2207− batch.resetStages();
2208− });
22092241
22102242 {
2243+ PROFILE_RANGE(render, "/renderOverlay");
22112244 PerformanceTimer perfTimer("renderOverlay");
22122245 // NOTE: There is no batch associated with this renderArgs
22132246 // the ApplicationOverlay class assumes it's viewport is setup to be the device size
@@ -2218,114 +2251,127 @@ void Application::paintGL() { · 4 unmodified lines
22182251
22192252 glm::vec3 boomOffset;
22202253 {
2221− PerformanceTimer perfTimer("CameraUpdates");
2254+ PROFILE_RANGE(render, "/updateCamera");
2255+ {
2256+ PerformanceTimer perfTimer("CameraUpdates");
22222257
2223− auto myAvatar = getMyAvatar();
2224− boomOffset = myAvatar->getScale() * myAvatar->getBoomLength() * -IDENTITY_FORWARD;
2258+ auto myAvatar = getMyAvatar();
2259+ boomOffset = myAvatar->getScale() * myAvatar->getBoomLength() * -IDENTITY_FORWARD;
22252260
2226− if (_myCamera.getMode() == CAMERA_MODE_FIRST_PERSON || _myCamera.getMode() == CAMERA_MODE_THIRD_PERSON) {
2227− Menu::getInstance()->setIsOptionChecked(MenuOption::FirstPerson, myAvatar->getBoomLength() <= MyAvatar::ZOOM_MIN);
2228− Menu::getInstance()->setIsOptionChecked(MenuOption::ThirdPerson, !(myAvatar->getBoomLength() <= MyAvatar::ZOOM_MIN));
2229− cameraMenuChanged();
2230− }
2261+ if (_myCamera.getMode() == CAMERA_MODE_FIRST_PERSON || _myCamera.getMode() == CAMERA_MODE_THIRD_PERSON) {
2262+ Menu::getInstance()->setIsOptionChecked(MenuOption::FirstPerson, myAvatar->getBoomLength() <= MyAvatar::ZOOM_MIN);
2263+ Menu::getInstance()->setIsOptionChecked(MenuOption::ThirdPerson, !(myAvatar->getBoomLength() <= MyAvatar::ZOOM_MIN));
2264+ cameraMenuChanged();
2265+ }
22312266
2232− // The render mode is default or mirror if the camera is in mirror mode, assigned further below
2233− renderArgs._renderMode = RenderArgs::DEFAULT_RENDER_MODE;
2267+ // The render mode is default or mirror if the camera is in mirror mode, assigned further below
2268+ renderArgs._renderMode = RenderArgs::DEFAULT_RENDER_MODE;
22342269
2235− // Always use the default eye position, not the actual head eye position.
2236− // Using the latter will cause the camera to wobble with idle animations,
2237− // or with changes from the face tracker
2238− if (_myCamera.getMode() == CAMERA_MODE_FIRST_PERSON) {
2239− if (isHMDMode()) {
2240− mat4 camMat = myAvatar->getSensorToWorldMatrix() * myAvatar->getHMDSensorMatrix();
2241− _myCamera.setPosition(extractTranslation(camMat));
2242− _myCamera.setOrientation(glm::quat_cast(camMat));
2243− } else {
2244− _myCamera.setPosition(myAvatar->getDefaultEyePosition());
2245− _myCamera.setOrientation(myAvatar->getMyHead()->getHeadOrientation());
2246− }
2247− } else if (_myCamera.getMode() == CAMERA_MODE_THIRD_PERSON) {
2248− if (isHMDMode()) {
2249− auto hmdWorldMat = myAvatar->getSensorToWorldMatrix() * myAvatar->getHMDSensorMatrix();
2250− _myCamera.setOrientation(glm::normalize(glm::quat_cast(hmdWorldMat)));
2251− _myCamera.setPosition(extractTranslation(hmdWorldMat) +
2252− myAvatar->getOrientation() * boomOffset);
2253− } else {
2254− _myCamera.setOrientation(myAvatar->getHead()->getOrientation());
2255− if (Menu::getInstance()->isOptionChecked(MenuOption::CenterPlayerInView)) {
2256− _myCamera.setPosition(myAvatar->getDefaultEyePosition()
2257− + _myCamera.getOrientation() * boomOffset);
2270+ // Always use the default eye position, not the actual head eye position.
2271+ // Using the latter will cause the camera to wobble with idle animations,
2272+ // or with changes from the face tracker
2273+ if (_myCamera.getMode() == CAMERA_MODE_FIRST_PERSON) {
2274+ if (isHMDMode()) {
2275+ mat4 camMat = myAvatar->getSensorToWorldMatrix() * myAvatar->getHMDSensorMatrix();
2276+ _myCamera.setPosition(extractTranslation(camMat));
2277+ _myCamera.setOrientation(glm::quat_cast(camMat));
22582278 } else {
2259− _myCamera.setPosition(myAvatar->getDefaultEyePosition()
2260− + myAvatar->getOrientation() * boomOffset);
2279+ _myCamera.setPosition(myAvatar->getDefaultEyePosition());
2280+ _myCamera.setOrientation(myAvatar->getMyHead()->getHeadOrientation());
22612281 }
2262− }
2263− } else if (_myCamera.getMode() == CAMERA_MODE_MIRROR) {
2264− if (isHMDMode()) {
2265− auto mirrorBodyOrientation = myAvatar->getWorldAlignedOrientation() * glm::quat(glm::vec3(0.0f, PI + _rotateMirror, 0.0f));
2266−
2267− glm::quat hmdRotation = extractRotation(myAvatar->getHMDSensorMatrix());
2268− // Mirror HMD yaw and roll
2269− glm::vec3 mirrorHmdEulers = glm::eulerAngles(hmdRotation);
2270− mirrorHmdEulers.y = -mirrorHmdEulers.y;
2271− mirrorHmdEulers.z = -mirrorHmdEulers.z;
2272− glm::quat mirrorHmdRotation = glm::quat(mirrorHmdEulers);
2282+ } else if (_myCamera.getMode() == CAMERA_MODE_THIRD_PERSON) {
2283+ if (isHMDMode()) {
2284+ auto hmdWorldMat = myAvatar->getSensorToWorldMatrix() * myAvatar->getHMDSensorMatrix();
2285+ _myCamera.setOrientation(glm::normalize(glm::quat_cast(hmdWorldMat)));
2286+ _myCamera.setPosition(extractTranslation(hmdWorldMat) +
2287+ myAvatar->getOrientation() * boomOffset);
2288+ } else {
2289+ _myCamera.setOrientation(myAvatar->getHead()->getOrientation());
2290+ if (Menu::getInstance()->isOptionChecked(MenuOption::CenterPlayerInView)) {
2291+ _myCamera.setPosition(myAvatar->getDefaultEyePosition()
2292+ + _myCamera.getOrientation() * boomOffset);
2293+ } else {
2294+ _myCamera.setPosition(myAvatar->getDefaultEyePosition()
2295+ + myAvatar->getOrientation() * boomOffset);
2296+ }
2297+ }
2298+ } else if (_myCamera.getMode() == CAMERA_MODE_MIRROR) {
2299+ if (isHMDMode()) {
2300+ auto mirrorBodyOrientation = myAvatar->getWorldAlignedOrientation() * glm::quat(glm::vec3(0.0f, PI + _rotateMirror, 0.0f));
22732301
2274− glm::quat worldMirrorRotation = mirrorBodyOrientation * mirrorHmdRotation;
2302+ glm::quat hmdRotation = extractRotation(myAvatar->getHMDSensorMatrix());
2303+ // Mirror HMD yaw and roll
2304+ glm::vec3 mirrorHmdEulers = glm::eulerAngles(hmdRotation);
2305+ mirrorHmdEulers.y = -mirrorHmdEulers.y;
2306+ mirrorHmdEulers.z = -mirrorHmdEulers.z;
2307+ glm::quat mirrorHmdRotation = glm::quat(mirrorHmdEulers);
22752308
2276− _myCamera.setOrientation(worldMirrorRotation);
2309+ glm::quat worldMirrorRotation = mirrorBodyOrientation * mirrorHmdRotation;
22772310
2278− glm::vec3 hmdOffset = extractTranslation(myAvatar->getHMDSensorMatrix());
2279− // Mirror HMD lateral offsets
2280− hmdOffset.x = -hmdOffset.x;
2311+ _myCamera.setOrientation(worldMirrorRotation);
22812312
2282− _myCamera.setPosition(myAvatar->getDefaultEyePosition()
2283− + glm::vec3(0, _raiseMirror * myAvatar->getUniformScale(), 0)
2284− + mirrorBodyOrientation * glm::vec3(0.0f, 0.0f, 1.0f) * MIRROR_FULLSCREEN_DISTANCE * _scaleMirror
2285− + mirrorBodyOrientation * hmdOffset);
2286− } else {
2287− _myCamera.setOrientation(myAvatar->getWorldAlignedOrientation()
2288− * glm::quat(glm::vec3(0.0f, PI + _rotateMirror, 0.0f)));
2289− _myCamera.setPosition(myAvatar->getDefaultEyePosition()
2290− + glm::vec3(0, _raiseMirror * myAvatar->getUniformScale(), 0)
2291− + (myAvatar->getOrientation() * glm::quat(glm::vec3(0.0f, _rotateMirror, 0.0f))) *
2292− glm::vec3(0.0f, 0.0f, -1.0f) * MIRROR_FULLSCREEN_DISTANCE * _scaleMirror);
2293− }
2294− renderArgs._renderMode = RenderArgs::MIRROR_RENDER_MODE;
2295− } else if (_myCamera.getMode() == CAMERA_MODE_ENTITY) {
2296− EntityItemPointer cameraEntity = _myCamera.getCameraEntityPointer();
2297− if (cameraEntity != nullptr) {
2298− if (isHMDMode()) {
2299− glm::quat hmdRotation = extractRotation(myAvatar->getHMDSensorMatrix());
2300− _myCamera.setOrientation(cameraEntity->getRotation() * hmdRotation);
23012313 glm::vec3 hmdOffset = extractTranslation(myAvatar->getHMDSensorMatrix());
2302− _myCamera.setPosition(cameraEntity->getPosition() + (hmdRotation * hmdOffset));
2314+ // Mirror HMD lateral offsets
2315+ hmdOffset.x = -hmdOffset.x;
2316+
2317+ _myCamera.setPosition(myAvatar->getDefaultEyePosition()
2318+ + glm::vec3(0, _raiseMirror * myAvatar->getUniformScale(), 0)
2319+ + mirrorBodyOrientation * glm::vec3(0.0f, 0.0f, 1.0f) * MIRROR_FULLSCREEN_DISTANCE * _scaleMirror
2320+ + mirrorBodyOrientation * hmdOffset);
23032321 } else {
2304− _myCamera.setOrientation(cameraEntity->getRotation());
2305− _myCamera.setPosition(cameraEntity->getPosition());
2322+ _myCamera.setOrientation(myAvatar->getWorldAlignedOrientation()
2323+ * glm::quat(glm::vec3(0.0f, PI + _rotateMirror, 0.0f)));
2324+ _myCamera.setPosition(myAvatar->getDefaultEyePosition()
2325+ + glm::vec3(0, _raiseMirror * myAvatar->getUniformScale(), 0)
2326+ + (myAvatar->getOrientation() * glm::quat(glm::vec3(0.0f, _rotateMirror, 0.0f))) *
2327+ glm::vec3(0.0f, 0.0f, -1.0f) * MIRROR_FULLSCREEN_DISTANCE * _scaleMirror);
2328+ }
2329+ renderArgs._renderMode = RenderArgs::MIRROR_RENDER_MODE;
2330+ } else if (_myCamera.getMode() == CAMERA_MODE_ENTITY) {
2331+ EntityItemPointer cameraEntity = _myCamera.getCameraEntityPointer();
2332+ if (cameraEntity != nullptr) {
2333+ if (isHMDMode()) {
2334+ glm::quat hmdRotation = extractRotation(myAvatar->getHMDSensorMatrix());
2335+ _myCamera.setOrientation(cameraEntity->getRotation() * hmdRotation);
2336+ glm::vec3 hmdOffset = extractTranslation(myAvatar->getHMDSensorMatrix());
2337+ _myCamera.setPosition(cameraEntity->getPosition() + (hmdRotation * hmdOffset));
2338+ } else {
2339+ _myCamera.setOrientation(cameraEntity->getRotation());
2340+ _myCamera.setPosition(cameraEntity->getPosition());
2341+ }
23062342 }
23072343 }
2308− }
2309− // Update camera position
2310− if (!isHMDMode()) {
2311− _myCamera.update(1.0f / _frameCounter.rate());
2344+ // Update camera position
2345+ if (!isHMDMode()) {
2346+ _myCamera.update(1.0f / _frameCounter.rate());
2347+ }
23122348 }
23132349 }
23142350
2315− getApplicationCompositor().setFrameInfo(_frameCount, _myCamera.getTransform());
2351+ {
2352+ PROFILE_RANGE(render, "/updateCompositor");
2353+ getApplicationCompositor().setFrameInfo(_frameCount, _myCamera.getTransform());
2354+ }
23162355
2317− // Primary rendering pass
2318− auto framebufferCache = DependencyManager::get<FramebufferCache>();
2319− const QSize size = framebufferCache->getFrameBufferSize();
2320− // Final framebuffer that will be handled to the display-plugin
2321− auto finalFramebuffer = framebufferCache->getFramebuffer();
2356+ gpu::FramebufferPointer finalFramebuffer;
2357+ QSize finalFramebufferSize;
2358+ {
2359+ PROFILE_RANGE(render, "/getOutputFramebuffer");
2360+ // Primary rendering pass
2361+ auto framebufferCache = DependencyManager::get<FramebufferCache>();
2362+ finalFramebufferSize = framebufferCache->getFrameBufferSize();
2363+ // Final framebuffer that will be handled to the display-plugin
2364+ finalFramebuffer = framebufferCache->getFramebuffer();
2365+ }
23222366
23232367 {
23242368 PROFILE_RANGE(render, "/mainRender");
23252369 PerformanceTimer perfTimer("mainRender");
23262370 renderArgs._boomOffset = boomOffset;
2371+ // FIXME is this ever going to be different from the size previously set in the render args
2372+ // in the overlay render?
23272373 // Viewport is assigned to the size of the framebuffer
2328− renderArgs._viewport = ivec4(0, 0, size.width(), size.height());
2374+ renderArgs._viewport = ivec4(0, 0, finalFramebufferSize.width(), finalFramebufferSize.height());
23292375 if (displayPlugin->isStereo()) {
23302376 // Stereo modes will typically have a larger projection matrix overall,
23312377 // so we ask for the 'mono' projection matrix, which for stereo and HMD
@@ -3625,6 +3671,133 @@ bool Application::shouldPaint(float nsecsElapsed) { · 1293 unmodified lines
36253671 #include <TCHAR.h>
36263672 #include <pdh.h>
36273673 #pragma comment(lib, "pdh.lib")
3674+#pragma comment(lib, "ntdll.lib")
3675+
3676+extern "C" {
3677+ enum SYSTEM_INFORMATION_CLASS {
3678+ SystemBasicInformation = 0,
3679+ SystemProcessorPerformanceInformation = 8,
3680+ };
3681+
3682+ struct SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
3683+ LARGE_INTEGER IdleTime;
3684+ LARGE_INTEGER KernelTime;
3685+ LARGE_INTEGER UserTime;
3686+ LARGE_INTEGER DpcTime;
3687+ LARGE_INTEGER InterruptTime;
3688+ ULONG InterruptCount;
3689+ };
3690+
3691+ struct SYSTEM_BASIC_INFORMATION {
3692+ ULONG Reserved;
3693+ ULONG TimerResolution;
3694+ ULONG PageSize;
3695+ ULONG NumberOfPhysicalPages;
3696+ ULONG LowestPhysicalPageNumber;
3697+ ULONG HighestPhysicalPageNumber;
3698+ ULONG AllocationGranularity;
3699+ ULONG_PTR MinimumUserModeAddress;
3700+ ULONG_PTR MaximumUserModeAddress;
3701+ ULONG_PTR ActiveProcessorsAffinityMask;
3702+ CCHAR NumberOfProcessors;
3703+ };
3704+
3705+ NTSYSCALLAPI NTSTATUS NTAPI NtQuerySystemInformation(
3706+ _In_ SYSTEM_INFORMATION_CLASS SystemInformationClass,
3707+ _Out_writes_bytes_opt_(SystemInformationLength) PVOID SystemInformation,
3708+ _In_ ULONG SystemInformationLength,
3709+ _Out_opt_ PULONG ReturnLength
3710+ );
3711+
3712+}
3713+template <typename T>
3714+NTSTATUS NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, T& t) {
3715+ return NtQuerySystemInformation(SystemInformationClass, &t, (ULONG)sizeof(T), nullptr);
3716+}
3717+
3718+template <typename T>
3719+NTSTATUS NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, std::vector<T>& t) {
3720+ return NtQuerySystemInformation(SystemInformationClass, t.data(), (ULONG)(sizeof(T) * t.size()), nullptr);
3721+}
3722+
3723+
3724+template <typename T>
3725+void updateValueAndDelta(std::pair<T, T>& pair, T newValue) {
3726+ auto& value = pair.first;
3727+ auto& delta = pair.second;
3728+ delta = (value != 0) ? newValue - value : 0;
3729+ value = newValue;
3730+}
3731+
3732+struct MyCpuInfo {
3733+ using ValueAndDelta = std::pair<LONGLONG, LONGLONG>;
3734+ std::string name;
3735+ ValueAndDelta kernel { 0, 0 };
3736+ ValueAndDelta user { 0, 0 };
3737+ ValueAndDelta idle { 0, 0 };
3738+ float kernelUsage { 0.0f };
3739+ float userUsage { 0.0f };
3740+
3741+ void update(const SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION& cpuInfo) {
3742+ updateValueAndDelta(kernel, cpuInfo.KernelTime.QuadPart);
3743+ updateValueAndDelta(user, cpuInfo.UserTime.QuadPart);
3744+ updateValueAndDelta(idle, cpuInfo.IdleTime.QuadPart);
3745+ auto totalTime = kernel.second + user.second + idle.second;
3746+ if (totalTime != 0) {
3747+ kernelUsage = (FLOAT)kernel.second / totalTime;
3748+ userUsage = (FLOAT)user.second / totalTime;
3749+ } else {
3750+ kernelUsage = userUsage = 0.0f;
3751+ }
3752+ }
3753+};
3754+
3755+void updateCpuInformation() {
3756+ static std::once_flag once;
3757+ static SYSTEM_BASIC_INFORMATION systemInfo {};
3758+ static SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION cpuTotals;
3759+ static std::vector<SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION> cpuInfos;
3760+ static std::vector<MyCpuInfo> myCpuInfos;
3761+ static MyCpuInfo myCpuTotals;
3762+ std::call_once(once, [&] {
3763+ NtQuerySystemInformation( SystemBasicInformation, systemInfo);
3764+ cpuInfos.resize(systemInfo.NumberOfProcessors);
3765+ myCpuInfos.resize(systemInfo.NumberOfProcessors);
3766+ for (size_t i = 0; i < systemInfo.NumberOfProcessors; ++i) {
3767+ myCpuInfos[i].name = "cpu." + std::to_string(i);
3768+ }
3769+ myCpuTotals.name = "cpu.total";
3770+ });
3771+ NtQuerySystemInformation(SystemProcessorPerformanceInformation, cpuInfos);
3772+
3773+ // Zero the CPU totals.
3774+ memset(&cpuTotals, 0, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION));
3775+ for (size_t i = 0; i < systemInfo.NumberOfProcessors; ++i) {
3776+ auto& cpuInfo = cpuInfos[i];
3777+ // KernelTime includes IdleTime.
3778+ cpuInfo.KernelTime.QuadPart -= cpuInfo.IdleTime.QuadPart;
3779+
3780+ // Update totals
3781+ cpuTotals.IdleTime.QuadPart += cpuInfo.IdleTime.QuadPart;
3782+ cpuTotals.KernelTime.QuadPart += cpuInfo.KernelTime.QuadPart;
3783+ cpuTotals.UserTime.QuadPart += cpuInfo.UserTime.QuadPart;
3784+
3785+ // Update friendly structure
3786+ auto& myCpuInfo = myCpuInfos[i];
3787+ myCpuInfo.update(cpuInfo);
3788+ PROFILE_COUNTER(app, myCpuInfo.name.c_str(), {
3789+ { "kernel", myCpuInfo.kernelUsage },
3790+ { "user", myCpuInfo.userUsage }
3791+ });
3792+ }
3793+
3794+ myCpuTotals.update(cpuTotals);
3795+ PROFILE_COUNTER(app, myCpuTotals.name.c_str(), {
3796+ { "kernel", myCpuTotals.kernelUsage },
3797+ { "user", myCpuTotals.userUsage }
3798+ });
3799+}
3800+
36283801
36293802 static ULARGE_INTEGER lastCPU, lastSysCPU, lastUserCPU;
36303803 static int numProcessors;
@@ -3677,6 +3850,26 @@ void getCpuUsage(vec3& systemAndUser) { · 46 unmodified lines
36773850 systemAndUser.z = (float)counterVal.doubleValue;
36783851 }
36793852
3853+void setupCpuMonitorThread() {
3854+ initCpuUsage();
3855+ auto cpuMonitorThread = QThread::currentThread();
3856+
3857+ QTimer* timer = new QTimer();
3858+ timer->setInterval(50);
3859+ QObject::connect(timer, &QTimer::timeout, [] {
3860+ updateCpuInformation();
3861+ vec3 kernelUserAndSystem;
3862+ getCpuUsage(kernelUserAndSystem);
3863+ PROFILE_COUNTER(app, "cpuProcess", { { "system", kernelUserAndSystem.x }, { "user", kernelUserAndSystem.y } });
3864+ PROFILE_COUNTER(app, "cpuSystem", { { "system", kernelUserAndSystem.z } });
3865+ });
3866+ QObject::connect(cpuMonitorThread, &QThread::finished, [=] {
3867+ timer->deleteLater();
3868+ cpuMonitorThread->deleteLater();
3869+ });
3870+ timer->start();
3871+}
3872+
36803873 #endif
36813874
36823875
@@ -3697,15 +3890,17 @@ void Application::idle(float nsecsElapsed) { · 14 unmodified lines
36973890 }
36983891
36993892 #ifdef Q_OS_WIN
3893+ // If tracing is enabled then monitor the CPU in a separate thread
37003894 static std::once_flag once;
3701− std::call_once(once, [] {
3702− initCpuUsage();
3895+ std::call_once(once, [&] {
3896+ if (trace_app().isDebugEnabled()) {
3897+ QThread* cpuMonitorThread = new QThread(qApp);
3898+ cpuMonitorThread->setObjectName("cpuMonitorThread");
3899+ QObject::connect(cpuMonitorThread, &QThread::started, [this] { setupCpuMonitorThread(); });
3900+ QObject::connect(qApp, &QCoreApplication::aboutToQuit, cpuMonitorThread, &QThread::quit);
3901+ cpuMonitorThread->start();
3902+ }
37033903 });
3704−
3705− vec3 kernelUserAndSystem;
3706− getCpuUsage(kernelUserAndSystem);
3707− PROFILE_COUNTER(app, "cpuProcess", { { "system", kernelUserAndSystem.x }, { "user", kernelUserAndSystem.y } });
3708− PROFILE_COUNTER(app, "cpuSystem", { { "system", kernelUserAndSystem.z } });
37093904 #endif
37103905
37113906

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