From 28978c8adbbba97b9b1064c889f19fc382144e47 Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Wed, 30 Mar 2016 17:49:30 -0700 Subject: [PATCH] optimize checkEnterLeaveEntities() to do distance check and stationary checks less frequently --- interface/src/Application.cpp | 4 +++- libraries/entities-renderer/src/EntityTreeRenderer.cpp | 10 ++++++++-- libraries/entities-renderer/src/EntityTreeRenderer.h | 4 ++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 781816a62a..12f343eba3 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3347,9 +3347,10 @@ void Application::update(float deltaTime) { } { PROFILE_RANGE_EX("HarvestChanges", 0xffffff00, (uint64_t)getActiveDisplayPlugin()->presentCount()); - PerformanceTimer perfTimer("havestChanges"); + PerformanceTimer perfTimer("harvestChanges"); if (_physicsEngine->hasOutgoingChanges()) { getEntities()->getTree()->withWriteLock([&] { + PerformanceTimer perfTimer("handleOutgoingChanges"); const VectorOfMotionStates& outgoingChanges = _physicsEngine->getOutgoingChanges(); _entitySimulation.handleOutgoingChanges(outgoingChanges, Physics::getSessionUUID()); avatarManager->handleOutgoingChanges(outgoingChanges); @@ -3365,6 +3366,7 @@ void Application::update(float deltaTime) { // Collision events (and their scripts) must not be handled when we're locked, above. (That would risk // deadlock.) _entitySimulation.handleCollisionEvents(collisionEvents); + // NOTE: the getEntities()->update() call below will wait for lock // and will simulate entity motion (the EntityTree has been given an EntitySimulation). getEntities()->update(); // update the models... diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp index 65ac5197c8..4078e24076 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp +++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp @@ -130,6 +130,7 @@ void EntityTreeRenderer::setTree(OctreePointer newTree) { } void EntityTreeRenderer::update() { + PerformanceTimer perfTimer("ETRupdate"); if (_tree && !_shuttingDown) { EntityTreePointer tree = std::static_pointer_cast(_tree); tree->update(); @@ -158,6 +159,7 @@ void EntityTreeRenderer::update() { } bool EntityTreeRenderer::findBestZoneAndMaybeContainingEntities(const glm::vec3& avatarPosition, QVector* entitiesContainingAvatar) { + PerformanceTimer perfTimer("findBestZone"); bool didUpdate = false; float radius = 1.0f; // for now, assume 1 meter radius QVector foundEntities; @@ -218,12 +220,15 @@ bool EntityTreeRenderer::findBestZoneAndMaybeContainingEntities(const glm::vec3& return didUpdate; } bool EntityTreeRenderer::checkEnterLeaveEntities() { + PerformanceTimer perfTimer("checkEnterLeaveEntities"); + auto now = usecTimestampNow(); bool didUpdate = false; if (_tree && !_shuttingDown) { glm::vec3 avatarPosition = _viewState->getAvatarPosition(); - if (avatarPosition != _lastAvatarPosition) { + // If we've moved "enough" check to see our enter/leave state + if (glm::distance(avatarPosition, _lastAvatarPosition) > ZONE_CHECK_DISTANCE) { QVector entitiesContainingAvatar; didUpdate = findBestZoneAndMaybeContainingEntities(avatarPosition, &entitiesContainingAvatar); @@ -248,7 +253,8 @@ bool EntityTreeRenderer::checkEnterLeaveEntities() { } _currentEntitiesInside = entitiesContainingAvatar; _lastAvatarPosition = avatarPosition; - } else { + } else if ((now - _lastZoneCheck) > ZONE_CHECK_INTERVAL) { // if it's been a while since checking zone state + _lastZoneCheck = now; didUpdate = findBestZoneAndMaybeContainingEntities(avatarPosition, nullptr); } } diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.h b/libraries/entities-renderer/src/EntityTreeRenderer.h index 095723dc9a..0fd5adc3f0 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.h +++ b/libraries/entities-renderer/src/EntityTreeRenderer.h @@ -178,6 +178,10 @@ private: std::shared_ptr _bestZone; float _bestZoneVolume; + quint64 _lastZoneCheck { 0 }; + const quint64 ZONE_CHECK_INTERVAL = USECS_PER_MSEC * 100; // ~10hz + const float ZONE_CHECK_DISTANCE = 0.001f; + glm::vec3 _previousKeyLightColor; float _previousKeyLightIntensity; float _previousKeyLightAmbientIntensity;