blender/intern/cycles/session/cache_eviction.cpp
Brecht Van Lommel fc9f1ed6da Cycles: Texture cache eviction for viewport rendering
Evict unused texture cache tiles in viewport rendering. The heuristic is to
do this least 2 seconds after the last viewport navigation, and after
rendering has completed.

When using many render samples this is not ideal, as renders might rarely
complete. On the other hand, doing it too early would evict tiles that are
used by later render samples. So there is room improve this later, but
it's better than not evicting at all..

Pull Request: https://projects.blender.org/blender/blender/pulls/157244
2026-04-20 17:51:59 +02:00

90 lines
2.2 KiB
C++

/* SPDX-FileCopyrightText: 2026 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "session/cache_eviction.h"
#include <chrono>
#include "util/math_base.h"
#include "util/time.h"
CCL_NAMESPACE_BEGIN
CacheEvictionManager::CacheEvictionManager(bool background) : background_(background) {}
void CacheEvictionManager::reset()
{
render_tile_count_ = 0;
viewport_last_activity_ = 0.0;
viewport_was_navigating_ = false;
render_tile_count_ = 0;
}
void CacheEvictionManager::set_navigating(bool navigating)
{
navigating_ = navigating;
}
bool CacheEvictionManager::is_navigating() const
{
return (background_) ? false : navigating_;
}
bool CacheEvictionManager::need_eviction(bool idle, bool switched_to_new_tile)
{
/* Final render. */
if (background_) {
/* Evict before rendering the next tile, except the first one where we
* can't determine what was shared with other tiles. */
if (idle || !switched_to_new_tile) {
return false;
}
render_tile_count_++;
return render_tile_count_ >= 2;
}
/* Viewport render. */
const bool navigating = navigating_;
if (navigating) {
/* No eviction while navigating. */
viewport_last_activity_ = 0.0;
}
else if (viewport_was_navigating_) {
/* Start eviction timer when navigating stops. */
viewport_last_activity_ = time_dt();
}
viewport_was_navigating_ = navigating;
if (!idle) {
/* Restart eviction timer while not idle. */
viewport_last_activity_ = time_dt();
return false;
}
if (wait_time(idle) != std::chrono::milliseconds::zero()) {
/* Not ready to evict yet. */
return false;
}
/* Eviction needed now, clear existing timer. */
viewport_last_activity_ = 0.0;
return true;
}
std::chrono::milliseconds CacheEvictionManager::wait_time(bool idle) const
{
if (!idle || background_ || viewport_last_activity_ == 0.0 || viewport_was_navigating_) {
/* No eviction pending. */
return std::chrono::milliseconds::max();
}
/* Wait for VIEWPORT_EVICTION_DELAY after last activity. */
const double elapsed = time_dt() - viewport_last_activity_;
const double remaining = VIEWPORT_EVICTION_DELAY - elapsed;
return std::chrono::milliseconds(int64_t(max(0.0, remaining) * 1000.0));
}
CCL_NAMESPACE_END