Replaces round to closest power of two by std ones

This commit is contained in:
Vladislav Belov
2026-09-21 00:47:13 +02:00
parent 0b90eea56b
commit 0650721b19
14 changed files with 28 additions and 60 deletions
+2 -2
View File
@@ -22,7 +22,6 @@
#include "lib/alignment.h"
#include "lib/allocators/dynarray.h"
#include "lib/allocators/shared_ptr.h"
#include "lib/bits.h"
#include "lib/debug.h"
#include "lib/file/vfs/vfs.h"
#include "lib/file/vfs/vfs_path.h"
@@ -32,6 +31,7 @@
#include "maths/MathUtil.h"
#include "ps/Filesystem.h"
#include <bit>
#include <cmath>
#include <cstring>
#include <memory>
@@ -96,7 +96,7 @@ void CHeightMipmap::Initialize(size_t mapSize, const u16* ptr)
ReleaseData();
m_MapSize = mapSize;
size_t mipmapSize = round_down_to_pow2(mapSize);
size_t mipmapSize = std::bit_floor(mapSize);
while (mipmapSize > 1)
{
+3 -2
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -47,6 +47,7 @@
#include "simulation2/system/Entity.h"
#include <array>
#include <bit>
#include <cstring>
#include <iterator>
@@ -255,7 +256,7 @@ void CLOSTexture::ConstructTexture(Renderer::Backend::IDeviceCommandContext* dev
m_MapSize = cmpRangeManager->GetVerticesPerSide();
const size_t textureSize = round_up_to_pow2(round_up((size_t)m_MapSize + g_BlurSize - 1, g_SubTextureAlignment));
const size_t textureSize = std::bit_ceil(round_up((size_t)m_MapSize + g_BlurSize - 1, g_SubTextureAlignment));
Renderer::Backend::IDevice* backendDevice = deviceCommandContext->GetDevice();
+2 -2
View File
@@ -28,7 +28,6 @@
#include "graphics/TerrainTextureEntry.h"
#include "graphics/TerritoryTexture.h"
#include "graphics/TextureManager.h"
#include "lib/bits.h"
#include "lib/code_generation.h"
#include "lib/debug.h"
#include "lib/hash.h"
@@ -69,6 +68,7 @@
#include <algorithm>
#include <array>
#include <bit>
#include <cmath>
#include <cstdint>
#include <iterator>
@@ -392,7 +392,7 @@ void CMiniMapTexture::CreateTextures(
DestroyTextures();
m_MapSize = terrain.GetVerticesPerSide();
const size_t textureSize = round_up_to_pow2(static_cast<size_t>(m_MapSize));
const size_t textureSize = std::bit_ceil(static_cast<size_t>(m_MapSize));
const Renderer::Backend::Sampler::Desc defaultSamplerDesc =
Renderer::Backend::Sampler::MakeDefaultSampler(
+1 -1
View File
@@ -231,7 +231,7 @@ CTerrainTextureManager::LoadAlphaMap(const VfsPath& alphaMapType)
// copy each alpha map (tile) into one buffer, arrayed horizontally.
//
const size_t tileWidth = 2 + base + 2; // 2 pixel border (avoids bilinear filtering artifacts)
const size_t totalWidth = round_up_to_pow2(tileWidth * NUM_ALPHA_MAPS);
const size_t totalWidth = std::bit_ceil(tileWidth * NUM_ALPHA_MAPS);
const size_t totalHeight = base; ENSURE(std::has_single_bit(totalHeight));
std::shared_ptr<u8> data;
AllocateAligned(data, totalWidth * totalHeight, maxSectorSize);
+3 -3
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -21,7 +21,6 @@
#include "graphics/Color.h"
#include "graphics/Terrain.h"
#include "lib/bits.h"
#include "lib/debug.h"
#include "ps/Profile.h"
#include "renderer/backend/Format.h"
@@ -39,6 +38,7 @@
#include "simulation2/system/Entity.h"
#include <algorithm>
#include <bit>
#include <cstdint>
#include <cstring>
#include <vector>
@@ -93,7 +93,7 @@ void CTerritoryTexture::ConstructTexture(Renderer::Backend::IDeviceCommandContex
// Convert size from terrain tiles to territory tiles
m_MapSize = cmpTerrain->GetMapSize() * Pathfinding::NAVCELL_SIZE_INT / ICmpTerritoryManager::NAVCELLS_PER_TERRITORY_TILE;
const uint32_t textureSize = round_up_to_pow2(static_cast<uint32_t>(m_MapSize));
const uint32_t textureSize = std::bit_ceil(static_cast<uint32_t>(m_MapSize));
m_Texture = deviceCommandContext->GetDevice()->CreateTexture2D("TerritoryTexture",
Renderer::Backend::ITexture::Usage::TRANSFER_DST |
-18
View File
@@ -195,24 +195,6 @@ struct CeilLog2<0>
**/
extern int floor_log2(const float x);
/**
* round up to next larger power of two.
**/
template<typename T>
inline T round_up_to_pow2(T x)
{
return T(1) << ceil_log2(x);
}
/**
* round down to next larger power of two.
**/
template<typename T>
inline T round_down_to_pow2(T x)
{
return T(1) << floor_log2(x);
}
/**
* round number up/down to the next given multiple.
*
+3 -2
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@@ -32,6 +32,7 @@
#include "lib/sysdep/rtl.h"
#include <algorithm>
#include <bit>
#include <cstring>
static const size_t BLOCK_SIZE = 512*KiB;
@@ -47,7 +48,7 @@ void WriteBuffer::EnsureSufficientCapacity(size_t size)
{
if(m_size + size > m_capacity)
{
m_capacity = round_up_to_pow2(m_size + size);
m_capacity = std::bit_ceil(m_size + size);
std::shared_ptr<u8> newData;
AllocateAligned(newData, m_capacity, maxSectorSize);
memcpy(newData.get(), m_data.get(), m_size);
-17
View File
@@ -101,23 +101,6 @@ public:
EQUALS(floor_log2(256.f), 8);
}
void test_round_up_to_pow2()
{
EQUALS(round_up_to_pow2(0u), 1u);
EQUALS(round_up_to_pow2(1u), 1u);
EQUALS(round_up_to_pow2(127u), 128u);
EQUALS(round_up_to_pow2(128u), 128u);
EQUALS(round_up_to_pow2(129u), 256u);
}
void test_round_down_to_pow2()
{
EQUALS(round_down_to_pow2(1u), 1u);
EQUALS(round_down_to_pow2(127u), 64u);
EQUALS(round_down_to_pow2(128u), 128u);
EQUALS(round_down_to_pow2(129u), 128u);
}
void test_round_up()
{
EQUALS(round_up( 0u, 16u), 0u);
+3 -2
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -21,6 +21,7 @@
#include "lib/bits.h"
#include "ps/containers/StaticVector.h"
#include <bit>
#include <cstddef>
#include <fmt/format.h>
#include <memory>
@@ -64,7 +65,7 @@ public:
{
m_BuffersToFree.emplace_back(std::move(m_Buffer));
m_Size = 0;
m_Capacity = std::min(std::max(round_up_to_pow2(n), m_Capacity * 2), m_MaxCapacity);
m_Capacity = std::min(std::max(std::bit_ceil(n), m_Capacity * 2), m_MaxCapacity);
if (n > m_Capacity)
{
throw CapacityExceededException{fmt::format(
+2 -2
View File
@@ -21,7 +21,6 @@
#include "graphics/Camera.h"
#include "graphics/Color.h"
#include "lib/bits.h"
#include "lib/config2.h"
#include "lib/debug.h"
#include "maths/BoundingBoxAligned.h"
@@ -50,6 +49,7 @@
#include <algorithm>
#include <array>
#include <bit>
#include <cmath>
#include <cstdint>
#include <memory>
@@ -513,7 +513,7 @@ void ShadowMapInternals::CreateTexture(const uint32_t width, const uint32_t heig
break;
// Ultra
case 2:
shadowMapSize = std::max(round_up_to_pow2(std::max(width, height)), 4096u);
shadowMapSize = std::max(std::bit_ceil(static_cast<uint32_t>(std::max(width, height))), 4096u);
break;
// Medium as is
default:
+3 -3
View File
@@ -26,7 +26,6 @@
#include "graphics/ShaderManager.h"
#include "graphics/ShaderTechnique.h"
#include "graphics/Terrain.h"
#include "lib/bits.h"
#include "maths/MathUtil.h"
#include "maths/Matrix3D.h"
#include "maths/Vector2D.h"
@@ -50,6 +49,7 @@
#include <algorithm>
#include <array>
#include <bit>
#include <cstdint>
#include <cstdlib>
#include <iterator>
@@ -360,8 +360,8 @@ void TerrainTextureOverlay::RenderAfterWater(
const ssize_t w = static_cast<ssize_t>(terrain.GetTilesPerSide() * m_TexelsPerTile);
const ssize_t h = static_cast<ssize_t>(terrain.GetTilesPerSide() * m_TexelsPerTile);
const uint32_t requiredWidth = round_up_to_pow2(w);
const uint32_t requiredHeight = round_up_to_pow2(h);
const uint32_t requiredWidth = std::bit_ceil(static_cast<uint32_t>(w));
const uint32_t requiredHeight = std::bit_ceil(static_cast<uint32_t>(h));
// Recreate the texture with new size if necessary
if (!m_Texture || m_Texture->GetWidth() != requiredWidth || m_Texture->GetHeight() != requiredHeight)
+2 -2
View File
@@ -25,7 +25,6 @@
#include "graphics/ShaderTechniquePtr.h"
#include "graphics/Terrain.h"
#include "graphics/TextureManager.h"
#include "lib/bits.h"
#include "lib/code_annotation.h"
#include "lib/debug.h"
#include "lib/path.h"
@@ -56,6 +55,7 @@
#include <algorithm>
#include <array>
#include <bit>
#include <climits>
#include <cmath>
#include <cstdint>
@@ -264,7 +264,7 @@ void WaterManager::DestroyViewSizeDependentObjects()
void WaterManager::RecreateOrLoadTexturesIfNeeded()
{
// Use screen-sized textures for minimum artifacts.
const size_t newRefTextureSize = round_up_to_pow2(g_Renderer.GetHeight());
const size_t newRefTextureSize = std::bit_ceil(static_cast<uint32_t>(g_Renderer.GetHeight()));
if (m_RefTextureSize != newRefTextureSize)
{
@@ -20,7 +20,6 @@
#include "DeviceCommandContext.h"
#include "graphics/Color.h"
#include "lib/bits.h"
#include "lib/debug.h"
#include "ps/CLogger.h"
#include "ps/ConfigDB.h"
@@ -246,7 +245,7 @@ void CDeviceCommandContext::CUploadRing::ResizeIfNeeded(
// We need to pad the data size for uniforms because we use dynamic offsets
// with a fixed range.
const uint32_t paddedDataSize{
m_Type == IBuffer::Type::UNIFORM ? round_up_to_pow2(dataSize) : dataSize};
m_Type == IBuffer::Type::UNIFORM ? std::bit_ceil(dataSize) : dataSize};
const bool resizeNeeded = !m_Buffer || m_BlockOffset + paddedDataSize > m_Capacity;
if (!resizeNeeded)
return;
@@ -257,7 +256,7 @@ void CDeviceCommandContext::CUploadRing::ResizeIfNeeded(
ExecuteUploads(commandBuffer);
}
m_Capacity = std::max(m_Capacity * 2, round_up_to_pow2(dataSize));
m_Capacity = std::max(m_Capacity * 2, std::bit_ceil(dataSize));
m_Buffer = m_Device->CreateCBuffer(
"UploadRingBuffer", m_Type, m_Capacity, IBuffer::Usage::DYNAMIC | IBuffer::Usage::TRANSFER_DST);
@@ -32,6 +32,7 @@
#include "renderer/backend/vulkan/Utilities.h"
#include <algorithm>
#include <bit>
#include <cstddef>
#include <cstring>
#include <limits>
@@ -374,7 +375,7 @@ uint32_t CRingCommandContext::AcquireFreeSpace(
!m_StagingBuffer || m_StagingBuffer->GetSize() < m_MaxStagingBufferCapacity;
if (needsResize && canResize)
{
const uint32_t minimumRequiredCapacity = round_up_to_pow2(requiredSize);
const uint32_t minimumRequiredCapacity = std::bit_ceil(requiredSize);
const uint32_t newCapacity = std::min(
std::max(m_StagingBuffer ? m_StagingBuffer->GetSize() * 2 : INITIAL_STAGING_BUFFER_CAPACITY, minimumRequiredCapacity),
m_MaxStagingBufferCapacity);