Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed mistakes in details cache optimization #1721

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/Layers/xrRender/DetailManager_CACHE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ void CDetailManager::cache_Update(int v_x, int v_z, Fvector& view)
float bestDistances[dm_max_decompress];
std::fill_n(bestDistances, dm_max_decompress, flt_max);

auto bestDistancesBegin = std::begin(bestDistances);
auto bestDistancesEnd = std::end(bestDistances);
ptrdiff_t maxIdx = 0;

for (u32 i = 0, size = cache_task.size(); i < size; ++i)
{
// Gain access to data
Expand All @@ -197,17 +201,20 @@ void CDetailManager::cache_Update(int v_x, int v_z, Fvector& view)
float D = view.distance_to_sqr(C);

// Select
for (int j = 0; j < dm_max_decompress; ++j)
if (D < bestDistances[maxIdx])
{
if (D < bestDistances[j])
{
bestDistances[j] = D;
bestIndexes[j] = i;
}
bestDistances[maxIdx] = D;
bestIndexes[maxIdx] = i;

const auto maxIt = std::max_element(bestDistancesBegin, bestDistancesEnd);
maxIdx = std::distance(bestDistancesBegin, maxIt);
}
}

for (int i = 0; i < dm_max_decompress; ++i)
// Because indexes become invalid after an erase, sort them and process in the reverse order to erase everything correctly
std::sort(bestIndexes, bestIndexes + dm_max_decompress);

for (int i = dm_max_decompress - 1; i >= 0; --i)
{
const u32 bestId = bestIndexes[i];

Expand Down
Loading