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
25#include <QtCore/QCommandLineParser>25#include <QtCore/QCommandLineParser>
26#include <QtCore/QMimeData>26#include <QtCore/QMimeData>
27#include <QtCore/QThreadPool>27#include <QtCore/QThreadPool>
28#include <QtConcurrent/QtConcurrentRun>
28 29
29#include <QtGui/QScreen>30#include <QtGui/QScreen>
30#include <QtGui/QWindow>31#include <QtGui/QWindow>
@@ -481,6 +482,12 @@ bool setupEssentials(int& argc, char** argv, bool runningMarkerExisted) { · 450 unmodified lines
481 static const auto SUPPRESS_SETTINGS_RESET = "--suppress-settings-reset";482 static const auto SUPPRESS_SETTINGS_RESET = "--suppress-settings-reset";
482 bool suppressPrompt = cmdOptionExists(argc, const_cast<const char**>(argv), SUPPRESS_SETTINGS_RESET);483 bool suppressPrompt = cmdOptionExists(argc, const_cast<const char**>(argv), SUPPRESS_SETTINGS_RESET);
483 bool previousSessionCrashed = CrashHandler::checkForResetSettings(runningMarkerExisted, suppressPrompt);484 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 }
484 491
485 Setting::init();492 Setting::init();
486 493
@@ -1219,8 +1226,8 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer, bo · 732 unmodified lines
1219 settingsTimer->stop();1226 settingsTimer->stop();
1220 // Delete it (this will trigger the thread destruction1227 // Delete it (this will trigger the thread destruction
1221 settingsTimer->deleteLater();1228 settingsTimer->deleteLater();
1222 // Mark the settings thread as finished, so we know we can safely save in the main application 1229 // Mark the settings thread as finished, so we know we can safely save in the main application
1223 // shutdown code 1230 // shutdown code
1224 _settingsGuard.trigger();1231 _settingsGuard.trigger();
1225 });1232 });
1226 1233
@@ -2166,48 +2173,74 @@ void Application::paintGL() { · 939 unmodified lines
2166 return;2173 return;
2167 }2174 }
2168 2175
2169 auto displayPlugin = getActiveDisplayPlugin();2176 DisplayPluginPointer displayPlugin;
2170 // FIXME not needed anymore?2177 {
2171 _offscreenContext->makeCurrent();2178 PROFILE_RANGE(render, "/getActiveDisplayPlugin");
2179 displayPlugin = getActiveDisplayPlugin();
2180 }
2172 2181
2173 // If a display plugin loses it's underlying support, it2182 {
2174 // needs to be able to signal us to not use it2183 PROFILE_RANGE(render, "/offscreenMakeCurrent");
2175 if (!displayPlugin->beginFrameRender(_frameCount)) {2184 // FIXME not needed anymore?
2176 _inPaint = false;2185 _offscreenContext->makeCurrent();
2177 updateDisplayMode();2186 }
2178 return;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 }
2179 }2197 }
2180 2198
2181 // update the avatar with a fresh HMD pose2199 // update the avatar with a fresh HMD pose
2182 getMyAvatar()->updateFromHMDSensorMatrix(getHMDSensorPose());2200 {
2201 PROFILE_RANGE(render, "/updateAvatar");
2202 getMyAvatar()->updateFromHMDSensorMatrix(getHMDSensorPose());
2203 }
2183 2204
2184 auto lodManager = DependencyManager::get<LODManager>();2205 auto lodManager = DependencyManager::get<LODManager>();
2185 2206
2207 RenderArgs renderArgs;
2186 {2208 {
2187 QMutexLocker viewLocker(&_viewMutex);2209 PROFILE_RANGE(render, "/buildFrustrumAndArgs");
2188 _viewFrustum.calculate();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 }
2189 }2221 }
2190 RenderArgs renderArgs(_gpuContext, getEntities(), lodManager->getOctreeSizeScale(),2222
2191 lodManager->getBoundaryLevelAdjust(), RenderArgs::DEFAULT_RENDER_MODE,
2192 RenderArgs::MONO, RenderArgs::RENDER_DEBUG_NONE);
2193 {2223 {
2194 QMutexLocker viewLocker(&_viewMutex);2224 PROFILE_RANGE(render, "/resizeGL");
2195 renderArgs.setViewFrustum(_viewFrustum);2225 PerformanceWarning::setSuppressShortTimings(Menu::getInstance()->isOptionChecked(MenuOption::SuppressShortTimings));
2226 bool showWarnings = Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings);
2227 PerformanceWarning warn(showWarnings, "Application::paintGL()");
2228 resizeGL();
2196 }2229 }
2197 2230
2198 PerformanceWarning::setSuppressShortTimings(Menu::getInstance()->isOptionChecked(MenuOption::SuppressShortTimings));2231 {
2199 bool showWarnings = Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings);2232 PROFILE_RANGE(render, "/gpuContextReset");
2200 PerformanceWarning warn(showWarnings, "Application::paintGL()");2233 _gpuContext->beginFrame(getHMDSensorPose());
2201 resizeGL();2234 // Reset the gpu::Context Stages
2235 // Back to the default framebuffer;
2236 gpu::doInBatch(_gpuContext, [&](gpu::Batch& batch) {
2237 batch.resetStages();
2238 });
2239 }
2202 2240
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 });
2209 2241
2210 {2242 {
2243 PROFILE_RANGE(render, "/renderOverlay");
2211 PerformanceTimer perfTimer("renderOverlay");2244 PerformanceTimer perfTimer("renderOverlay");
2212 // NOTE: There is no batch associated with this renderArgs2245 // NOTE: There is no batch associated with this renderArgs
2213 // the ApplicationOverlay class assumes it's viewport is setup to be the device size2246 // the ApplicationOverlay class assumes it's viewport is setup to be the device size
@@ -2218,114 +2251,127 @@ void Application::paintGL() { · 4 unmodified lines
2218 2251
2219 glm::vec3 boomOffset;2252 glm::vec3 boomOffset;
2220 {2253 {
2221 PerformanceTimer perfTimer("CameraUpdates");2254 PROFILE_RANGE(render, "/updateCamera");
2255 {
2256 PerformanceTimer perfTimer("CameraUpdates");
2222 2257
2223 auto myAvatar = getMyAvatar();2258 auto myAvatar = getMyAvatar();
2224 boomOffset = myAvatar->getScale() * myAvatar->getBoomLength() * -IDENTITY_FORWARD;2259 boomOffset = myAvatar->getScale() * myAvatar->getBoomLength() * -IDENTITY_FORWARD;
2225 2260
2226 if (_myCamera.getMode() == CAMERA_MODE_FIRST_PERSON || _myCamera.getMode() == CAMERA_MODE_THIRD_PERSON) {2261 if (_myCamera.getMode() == CAMERA_MODE_FIRST_PERSON || _myCamera.getMode() == CAMERA_MODE_THIRD_PERSON) {
2227 Menu::getInstance()->setIsOptionChecked(MenuOption::FirstPerson, myAvatar->getBoomLength() <= MyAvatar::ZOOM_MIN);2262 Menu::getInstance()->setIsOptionChecked(MenuOption::FirstPerson, myAvatar->getBoomLength() <= MyAvatar::ZOOM_MIN);
2228 Menu::getInstance()->setIsOptionChecked(MenuOption::ThirdPerson, !(myAvatar->getBoomLength() <= MyAvatar::ZOOM_MIN));2263 Menu::getInstance()->setIsOptionChecked(MenuOption::ThirdPerson, !(myAvatar->getBoomLength() <= MyAvatar::ZOOM_MIN));
2229 cameraMenuChanged();2264 cameraMenuChanged();
2230 }2265 }
2231 2266
2232 // The render mode is default or mirror if the camera is in mirror mode, assigned further below2267 // The render mode is default or mirror if the camera is in mirror mode, assigned further below
2233 renderArgs._renderMode = RenderArgs::DEFAULT_RENDER_MODE;2268 renderArgs._renderMode = RenderArgs::DEFAULT_RENDER_MODE;
2234 2269
2235 // Always use the default eye position, not the actual head eye position.2270 // 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,2271 // Using the latter will cause the camera to wobble with idle animations,
2237 // or with changes from the face tracker2272 // or with changes from the face tracker
2238 if (_myCamera.getMode() == CAMERA_MODE_FIRST_PERSON) {2273 if (_myCamera.getMode() == CAMERA_MODE_FIRST_PERSON) {
2239 if (isHMDMode()) {2274 if (isHMDMode()) {
2240 mat4 camMat = myAvatar->getSensorToWorldMatrix() * myAvatar->getHMDSensorMatrix();2275 mat4 camMat = myAvatar->getSensorToWorldMatrix() * myAvatar->getHMDSensorMatrix();
2241 _myCamera.setPosition(extractTranslation(camMat));2276 _myCamera.setPosition(extractTranslation(camMat));
2242 _myCamera.setOrientation(glm::quat_cast(camMat));2277 _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);
2258 } else {2278 } else {
2259 _myCamera.setPosition(myAvatar->getDefaultEyePosition()2279 _myCamera.setPosition(myAvatar->getDefaultEyePosition());
2260 + myAvatar->getOrientation() * boomOffset);2280 _myCamera.setOrientation(myAvatar->getMyHead()->getHeadOrientation());
2261 }2281 }
2262 }2282 } else if (_myCamera.getMode() == CAMERA_MODE_THIRD_PERSON) {
2263 } else if (_myCamera.getMode() == CAMERA_MODE_MIRROR) {2283 if (isHMDMode()) {
2264 if (isHMDMode()) {2284 auto hmdWorldMat = myAvatar->getSensorToWorldMatrix() * myAvatar->getHMDSensorMatrix();
2265 auto mirrorBodyOrientation = myAvatar->getWorldAlignedOrientation() * glm::quat(glm::vec3(0.0f, PI + _rotateMirror, 0.0f));2285 _myCamera.setOrientation(glm::normalize(glm::quat_cast(hmdWorldMat)));
2266 2286 _myCamera.setPosition(extractTranslation(hmdWorldMat) +
2267 glm::quat hmdRotation = extractRotation(myAvatar->getHMDSensorMatrix());2287 myAvatar->getOrientation() * boomOffset);
2268 // Mirror HMD yaw and roll2288 } else {
2269 glm::vec3 mirrorHmdEulers = glm::eulerAngles(hmdRotation);2289 _myCamera.setOrientation(myAvatar->getHead()->getOrientation());
2270 mirrorHmdEulers.y = -mirrorHmdEulers.y;2290 if (Menu::getInstance()->isOptionChecked(MenuOption::CenterPlayerInView)) {
2271 mirrorHmdEulers.z = -mirrorHmdEulers.z;2291 _myCamera.setPosition(myAvatar->getDefaultEyePosition()
2272 glm::quat mirrorHmdRotation = glm::quat(mirrorHmdEulers);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));
2273 2301
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);
2275 2308
2276 _myCamera.setOrientation(worldMirrorRotation);2309 glm::quat worldMirrorRotation = mirrorBodyOrientation * mirrorHmdRotation;
2277 2310
2278 glm::vec3 hmdOffset = extractTranslation(myAvatar->getHMDSensorMatrix());2311 _myCamera.setOrientation(worldMirrorRotation);
2279 // Mirror HMD lateral offsets
2280 hmdOffset.x = -hmdOffset.x;
2281 2312
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);
2301 glm::vec3 hmdOffset = extractTranslation(myAvatar->getHMDSensorMatrix());2313 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);
2303 } else {2321 } else {
2304 _myCamera.setOrientation(cameraEntity->getRotation());2322 _myCamera.setOrientation(myAvatar->getWorldAlignedOrientation()
2305 _myCamera.setPosition(cameraEntity->getPosition());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 }
2306 }2342 }
2307 }2343 }
2308 }2344 // Update camera position
2309 // Update camera position2345 if (!isHMDMode()) {
2310 if (!isHMDMode()) {2346 _myCamera.update(1.0f / _frameCounter.rate());
2311 _myCamera.update(1.0f / _frameCounter.rate());2347 }
2312 }2348 }
2313 }2349 }
2314 2350
2315 getApplicationCompositor().setFrameInfo(_frameCount, _myCamera.getTransform());2351 {
2352 PROFILE_RANGE(render, "/updateCompositor");
2353 getApplicationCompositor().setFrameInfo(_frameCount, _myCamera.getTransform());
2354 }
2316 2355
2317 // Primary rendering pass2356 gpu::FramebufferPointer finalFramebuffer;
2318 auto framebufferCache = DependencyManager::get<FramebufferCache>();2357 QSize finalFramebufferSize;
2319 const QSize size = framebufferCache->getFrameBufferSize();2358 {
2320 // Final framebuffer that will be handled to the display-plugin2359 PROFILE_RANGE(render, "/getOutputFramebuffer");
2321 auto finalFramebuffer = framebufferCache->getFramebuffer();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 }
2322 2366
2323 {2367 {
2324 PROFILE_RANGE(render, "/mainRender");2368 PROFILE_RANGE(render, "/mainRender");
2325 PerformanceTimer perfTimer("mainRender");2369 PerformanceTimer perfTimer("mainRender");
2326 renderArgs._boomOffset = boomOffset;2370 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?
2327 // Viewport is assigned to the size of the framebuffer2373 // 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());
2329 if (displayPlugin->isStereo()) {2375 if (displayPlugin->isStereo()) {
2330 // Stereo modes will typically have a larger projection matrix overall,2376 // Stereo modes will typically have a larger projection matrix overall,
2331 // so we ask for the 'mono' projection matrix, which for stereo and HMD2377 // 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
3625#include <TCHAR.h>3671#include <TCHAR.h>
3626#include <pdh.h>3672#include <pdh.h>
3627#pragma comment(lib, "pdh.lib")3673#pragma comment(lib, "pdh.lib")
3674#pragma comment(lib, "ntdll.lib")
3675
3676extern "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}
3713template <typename T>
3714NTSTATUS NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, T& t) {
3715 return NtQuerySystemInformation(SystemInformationClass, &t, (ULONG)sizeof(T), nullptr);
3716}
3717
3718template <typename T>
3719NTSTATUS NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, std::vector<T>& t) {
3720 return NtQuerySystemInformation(SystemInformationClass, t.data(), (ULONG)(sizeof(T) * t.size()), nullptr);
3721}
3722
3723
3724template <typename T>
3725void 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
3732struct 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
3755void 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
3628 3801
3629static ULARGE_INTEGER lastCPU, lastSysCPU, lastUserCPU;3802static ULARGE_INTEGER lastCPU, lastSysCPU, lastUserCPU;
3630static int numProcessors;3803static int numProcessors;
@@ -3677,6 +3850,26 @@ void getCpuUsage(vec3& systemAndUser) { · 46 unmodified lines
3677 systemAndUser.z = (float)counterVal.doubleValue;3850 systemAndUser.z = (float)counterVal.doubleValue;
3678}3851}
3679 3852
3853void 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
3680#endif3873#endif
3681 3874
3682 3875
@@ -3697,15 +3890,17 @@ void Application::idle(float nsecsElapsed) { · 14 unmodified lines
3697 }3890 }
3698 3891
3699#ifdef Q_OS_WIN3892#ifdef Q_OS_WIN
3893 // If tracing is enabled then monitor the CPU in a separate thread
3700 static std::once_flag once;3894 static std::once_flag once;
3701 std::call_once(once, [] {3895 std::call_once(once, [&] {
3702 initCpuUsage();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 }
3703 });3903 });
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 } });
3709#endif3904#endif
3710 3905
3711 3906

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