From 31ccfffd386cdf7bfafdccda240a179431ecc98c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Sun, 27 Nov 2016 11:56:22 +0100 Subject: [PATCH] Common: Add alignment header Gets rid of duplicated alignment code. --- Source/Core/Common/Align.h | 24 +++++++++++++++++++ Source/Core/Common/Arm64Emitter.cpp | 6 ++--- Source/Core/Common/Common.vcxproj | 3 ++- Source/Core/Common/Common.vcxproj.filters | 3 ++- Source/Core/Common/MathUtil.h | 3 --- Source/Core/Core/Boot/Boot.cpp | 4 ++-- Source/Core/Core/HW/DVDInterface.cpp | 4 ++-- Source/Core/Core/HW/WiiSaveCrypted.cpp | 8 +++---- Source/Core/DiscIO/NANDContentLoader.cpp | 4 ++-- Source/Core/DiscIO/VolumeDirectory.cpp | 10 ++++---- Source/Core/DiscIO/VolumeWad.cpp | 14 ++++++----- Source/Core/DiscIO/WbfsBlob.cpp | 8 ++----- Source/Core/DiscIO/WiiWad.cpp | 12 +++++----- Source/Core/VideoBackends/D3D/D3DUtil.cpp | 19 +++++++-------- .../VideoBackends/D3D/GeometryShaderCache.cpp | 4 +++- .../VideoBackends/D3D/PixelShaderCache.cpp | 4 +++- .../VideoBackends/D3D/VertexShaderCache.cpp | 4 +++- .../Core/VideoBackends/D3D12/D3DTexture.cpp | 3 ++- Source/Core/VideoBackends/D3D12/D3DUtil.cpp | 7 ++++-- Source/Core/VideoBackends/D3D12/D3DUtil.h | 6 ----- .../D3D12/FramebufferManager.cpp | 9 +++---- .../VideoBackends/D3D12/PSTextureEncoder.cpp | 5 ++-- Source/Core/VideoBackends/D3D12/Render.cpp | 5 ++-- .../Core/VideoBackends/D3D12/TextureCache.cpp | 3 ++- .../Core/VideoBackends/D3D12/XFBEncoder.cpp | 12 ++++++---- .../VideoBackends/OGL/ProgramShaderCache.cpp | 21 ++++++++-------- .../Core/VideoBackends/OGL/StreamBuffer.cpp | 9 +++---- .../VideoBackends/Software/TextureEncoder.cpp | 3 ++- .../VideoBackends/Vulkan/StateTracker.cpp | 13 +++++----- Source/Core/VideoBackends/Vulkan/Util.cpp | 12 ++-------- Source/Core/VideoBackends/Vulkan/Util.h | 1 - Source/Core/VideoCommon/TextureCacheBase.cpp | 22 +++++++++-------- 32 files changed, 146 insertions(+), 119 deletions(-) create mode 100644 Source/Core/Common/Align.h diff --git a/Source/Core/Common/Align.h b/Source/Core/Common/Align.h new file mode 100644 index 0000000000..40c45766d3 --- /dev/null +++ b/Source/Core/Common/Align.h @@ -0,0 +1,24 @@ +// This file is under the public domain. + +#pragma once + +#include +#include + +namespace Common +{ +template +constexpr T AlignUp(T value, size_t size) +{ + static_assert(std::is_unsigned(), "T must be an unsigned value."); + return static_cast(value + (size - value % size) % size); +} + +template +constexpr T AlignDown(T value, size_t size) +{ + static_assert(std::is_unsigned(), "T must be an unsigned value."); + return static_cast(value - value % size); +} + +} // namespace Common diff --git a/Source/Core/Common/Arm64Emitter.cpp b/Source/Core/Common/Arm64Emitter.cpp index 3b5a662d70..4fe3b6f0b3 100644 --- a/Source/Core/Common/Arm64Emitter.cpp +++ b/Source/Core/Common/Arm64Emitter.cpp @@ -7,10 +7,10 @@ #include #include +#include "Common/Align.h" #include "Common/Arm64Emitter.h" #include "Common/Assert.h" #include "Common/CommonTypes.h" -#include "Common/MathUtil.h" namespace Arm64Gen { @@ -2079,14 +2079,14 @@ bool ARM64XEmitter::MOVI2R2(ARM64Reg Rd, u64 imm1, u64 imm2) void ARM64XEmitter::ABI_PushRegisters(BitSet32 registers) { - int num_regs = registers.Count(); + unsigned int num_regs = registers.Count(); if (num_regs % 2) { bool first = true; // Stack is required to be quad-word aligned. - u32 stack_size = ROUND_UP(num_regs * 8, 16); + u32 stack_size = Common::AlignUp(num_regs * 8, 16); u32 current_offset = 0; std::vector reg_pair; diff --git a/Source/Core/Common/Common.vcxproj b/Source/Core/Common/Common.vcxproj index b6cb26cf94..1e88bd5e33 100644 --- a/Source/Core/Common/Common.vcxproj +++ b/Source/Core/Common/Common.vcxproj @@ -35,6 +35,7 @@ + @@ -211,4 +212,4 @@ - \ No newline at end of file + diff --git a/Source/Core/Common/Common.vcxproj.filters b/Source/Core/Common/Common.vcxproj.filters index 8b4876f638..3ad3825e23 100644 --- a/Source/Core/Common/Common.vcxproj.filters +++ b/Source/Core/Common/Common.vcxproj.filters @@ -18,6 +18,7 @@ + @@ -293,4 +294,4 @@ - \ No newline at end of file + diff --git a/Source/Core/Common/MathUtil.h b/Source/Core/Common/MathUtil.h index a38e49bf6d..71bc985456 100644 --- a/Source/Core/Common/MathUtil.h +++ b/Source/Core/Common/MathUtil.h @@ -180,9 +180,6 @@ struct Rectangle float MathFloatVectorSum(const std::vector&); -#define ROUND_UP(x, a) (((x) + (a)-1) & ~((a)-1)) -#define ROUND_DOWN(x, a) ((x) & ~((a)-1)) - // Rounds down. 0 -> undefined inline int IntLog2(u64 val) { diff --git a/Source/Core/Core/Boot/Boot.cpp b/Source/Core/Core/Boot/Boot.cpp index da104586c7..03f1dcfafe 100644 --- a/Source/Core/Core/Boot/Boot.cpp +++ b/Source/Core/Core/Boot/Boot.cpp @@ -6,10 +6,10 @@ #include +#include "Common/Align.h" #include "Common/CommonPaths.h" #include "Common/CommonTypes.h" #include "Common/FileUtil.h" -#include "Common/MathUtil.h" #include "Common/MsgHandler.h" #include "Common/StringUtil.h" @@ -72,7 +72,7 @@ void CBoot::Load_FST(bool _bIsWii) volume.ReadSwapped(0x0428, &fst_size, _bIsWii); volume.ReadSwapped(0x042c, &max_fst_size, _bIsWii); - u32 arena_high = ROUND_DOWN(0x817FFFFF - (max_fst_size << shift), 0x20); + u32 arena_high = Common::AlignDown(0x817FFFFF - (max_fst_size << shift), 0x20); Memory::Write_U32(arena_high, 0x00000034); // load FST diff --git a/Source/Core/Core/HW/DVDInterface.cpp b/Source/Core/Core/HW/DVDInterface.cpp index 34010422b6..67a7fa6089 100644 --- a/Source/Core/Core/HW/DVDInterface.cpp +++ b/Source/Core/Core/HW/DVDInterface.cpp @@ -9,9 +9,9 @@ #include "AudioCommon/AudioCommon.h" +#include "Common/Align.h" #include "Common/ChunkFile.h" #include "Common/CommonTypes.h" -#include "Common/MathUtil.h" #include "Core/ConfigManager.h" #include "Core/Core.h" @@ -1241,7 +1241,7 @@ u64 SimulateDiscReadTime(u64 offset, u32 length) } } - s_last_read_offset = ROUND_DOWN(offset + length - 2048, 2048); + s_last_read_offset = Common::AlignDown(offset + length - 2048, 2048); return ticks_until_completion; } diff --git a/Source/Core/Core/HW/WiiSaveCrypted.cpp b/Source/Core/Core/HW/WiiSaveCrypted.cpp index 28fbf786df..fb1298143b 100644 --- a/Source/Core/Core/HW/WiiSaveCrypted.cpp +++ b/Source/Core/Core/HW/WiiSaveCrypted.cpp @@ -18,12 +18,12 @@ #include #include +#include "Common/Align.h" #include "Common/CommonFuncs.h" #include "Common/CommonTypes.h" #include "Common/Crypto/ec.h" #include "Common/FileUtil.h" #include "Common/Logging/Log.h" -#include "Common/MathUtil.h" #include "Common/MsgHandler.h" #include "Common/NandPaths.h" #include "Common/StringUtil.h" @@ -345,7 +345,7 @@ void CWiiSaveCrypted::ImportWiiSaveFiles() if (file_hdr_tmp.type == 1) { file_size = Common::swap32(file_hdr_tmp.size); - u32 file_size_rounded = ROUND_UP(file_size, BLOCK_SZ); + u32 file_size_rounded = Common::AlignUp(file_size, BLOCK_SZ); std::vector file_data(file_size_rounded); std::vector file_data_enc(file_size_rounded); @@ -394,7 +394,7 @@ void CWiiSaveCrypted::ExportWiiSaveFiles() file_hdr_tmp.type = 1; } - u32 file_size_rounded = ROUND_UP(file_size, BLOCK_SZ); + u32 file_size_rounded = Common::AlignUp(file_size, BLOCK_SZ); file_hdr_tmp.magic = Common::swap32(FILE_HDR_MAGIC); file_hdr_tmp.size = Common::swap32(file_size); file_hdr_tmp.Permissions = 0x3c; @@ -630,7 +630,7 @@ void CWiiSaveCrypted::ScanForFiles(const std::string& save_directory, else { file_list.push_back(elem.physicalName); - size += ROUND_UP(elem.size, BLOCK_SZ); + size += static_cast(Common::AlignUp(elem.size, BLOCK_SZ)); } } } diff --git a/Source/Core/DiscIO/NANDContentLoader.cpp b/Source/Core/DiscIO/NANDContentLoader.cpp index 4a67f09405..5b3057efbf 100644 --- a/Source/Core/DiscIO/NANDContentLoader.cpp +++ b/Source/Core/DiscIO/NANDContentLoader.cpp @@ -14,11 +14,11 @@ #include #include +#include "Common/Align.h" #include "Common/CommonFuncs.h" #include "Common/CommonTypes.h" #include "Common/FileUtil.h" #include "Common/Logging/Log.h" -#include "Common/MathUtil.h" #include "Common/MsgHandler.h" #include "Common/NandPaths.h" #include "Common/StringUtil.h" @@ -264,7 +264,7 @@ void CNANDContentLoader::InitializeContentEntries(const std::vector& tmd, if (m_IsWAD) { - u32 rounded_size = ROUND_UP(content.m_Size, 0x40); + u32 rounded_size = Common::AlignUp(content.m_Size, 0x40); iv.fill(0); std::copy(&tmd[entry_offset + 0x01E8], &tmd[entry_offset + 0x01E8 + 2], iv.begin()); diff --git a/Source/Core/DiscIO/VolumeDirectory.cpp b/Source/Core/DiscIO/VolumeDirectory.cpp index 9828319c08..1d58f76052 100644 --- a/Source/Core/DiscIO/VolumeDirectory.cpp +++ b/Source/Core/DiscIO/VolumeDirectory.cpp @@ -10,12 +10,12 @@ #include #include +#include "Common/Align.h" #include "Common/Assert.h" #include "Common/CommonPaths.h" #include "Common/CommonTypes.h" #include "Common/FileUtil.h" #include "Common/Logging/Log.h" -#include "Common/MathUtil.h" #include "DiscIO/Blob.h" #include "DiscIO/Enums.h" #include "DiscIO/FileMonitor.h" @@ -308,7 +308,7 @@ bool CVolumeDirectory::SetApploader(const std::string& _rApploader) std::copy(data.begin(), data.end(), m_apploader.begin()); // 32byte aligned (plus 0x20 padding) - m_dol_address = ROUND_UP(APPLOADER_ADDRESS + m_apploader.size() + 0x20, 0x20ull); + m_dol_address = Common::AlignUp(APPLOADER_ADDRESS + m_apploader.size() + 0x20, 0x20ull); return true; } else @@ -332,7 +332,7 @@ void CVolumeDirectory::SetDOL(const std::string& rDOL) Write32((u32)(m_dol_address >> m_addressShift), 0x0420, &m_diskHeader); // 32byte aligned (plus 0x20 padding) - m_fst_address = ROUND_UP(m_dol_address + m_DOL.size() + 0x20, 0x20ull); + m_fst_address = Common::AlignUp(m_dol_address + m_DOL.size() + 0x20, 0x20ull); } } @@ -353,7 +353,7 @@ void CVolumeDirectory::BuildFST() m_fst_address = APPLOADER_ADDRESS + 0x2000; // 4 byte aligned start of data on disk - m_dataStartAddress = ROUND_UP(m_fst_address + m_FSTData.size(), 0x8000ull); + m_dataStartAddress = Common::AlignUp(m_fst_address + m_FSTData.size(), 0x8000ull); u64 curDataAddress = m_dataStartAddress; u32 fstOffset = 0; // Offset within FST data @@ -470,7 +470,7 @@ void CVolumeDirectory::WriteEntry(const File::FSTEntry& entry, u32& fstOffset, u m_virtualDisk.emplace(dataOffset, entry.physicalName); // 4 byte aligned - dataOffset = ROUND_UP(dataOffset + std::max(entry.size, 1ull), 0x8000ull); + dataOffset = Common::AlignUp(dataOffset + std::max(entry.size, 1ull), 0x8000ull); } } diff --git a/Source/Core/DiscIO/VolumeWad.cpp b/Source/Core/DiscIO/VolumeWad.cpp index 158be3dbca..3254190be9 100644 --- a/Source/Core/DiscIO/VolumeWad.cpp +++ b/Source/Core/DiscIO/VolumeWad.cpp @@ -10,9 +10,9 @@ #include #include +#include "Common/Align.h" #include "Common/CommonTypes.h" #include "Common/Logging/Log.h" -#include "Common/MathUtil.h" #include "Common/MsgHandler.h" #include "Common/StringUtil.h" #include "DiscIO/Blob.h" @@ -20,8 +20,6 @@ #include "DiscIO/Volume.h" #include "DiscIO/VolumeWad.h" -#define ALIGN_40(x) ROUND_UP(Common::swap32(x), 0x40) - namespace DiscIO { CVolumeWAD::CVolumeWAD(std::unique_ptr reader) @@ -35,9 +33,13 @@ CVolumeWAD::CVolumeWAD(std::unique_ptr reader) Read(0x14, 4, (u8*)&m_tmd_size); Read(0x18, 4, (u8*)&m_data_size); - m_offset = ALIGN_40(m_hdr_size) + ALIGN_40(m_cert_size); - m_tmd_offset = ALIGN_40(m_hdr_size) + ALIGN_40(m_cert_size) + ALIGN_40(m_tick_size); - m_opening_bnr_offset = m_tmd_offset + ALIGN_40(m_tmd_size) + ALIGN_40(m_data_size); + m_offset = Common::AlignUp(Common::swap32(m_hdr_size), 0x40) + + Common::AlignUp(Common::swap32(m_cert_size), 0x40); + m_tmd_offset = Common::AlignUp(Common::swap32(m_hdr_size), 0x40) + + Common::AlignUp(Common::swap32(m_cert_size), 0x40) + + Common::AlignUp(Common::swap32(m_tick_size), 0x40); + m_opening_bnr_offset = m_tmd_offset + Common::AlignUp(Common::swap32(m_tmd_size), 0x40) + + Common::AlignUp(Common::swap32(m_data_size), 0x40); } CVolumeWAD::~CVolumeWAD() diff --git a/Source/Core/DiscIO/WbfsBlob.cpp b/Source/Core/DiscIO/WbfsBlob.cpp index af244d9e68..6acbe5d0b3 100644 --- a/Source/Core/DiscIO/WbfsBlob.cpp +++ b/Source/Core/DiscIO/WbfsBlob.cpp @@ -9,6 +9,7 @@ #include #include +#include "Common/Align.h" #include "Common/CommonFuncs.h" #include "Common/CommonTypes.h" #include "Common/FileUtil.h" @@ -22,11 +23,6 @@ static const u64 WII_SECTOR_SIZE = 0x8000; static const u64 WII_SECTOR_COUNT = 143432 * 2; static const u64 WII_DISC_HEADER_SIZE = 256; -static inline u64 align(u64 value, u64 bounds) -{ - return (value + (bounds - 1)) & (~(bounds - 1)); -} - WbfsFileReader::WbfsFileReader(const std::string& filename) : m_total_files(0), m_size(0), m_good(true) { @@ -104,7 +100,7 @@ bool WbfsFileReader::ReadHeader() m_blocks_per_disc = (WII_SECTOR_COUNT * WII_SECTOR_SIZE + m_wbfs_sector_size - 1) / m_wbfs_sector_size; m_disc_info_size = - align(WII_DISC_HEADER_SIZE + m_blocks_per_disc * sizeof(u16), m_hd_sector_size); + Common::AlignUp(WII_DISC_HEADER_SIZE + m_blocks_per_disc * sizeof(u16), m_hd_sector_size); return m_header.disc_table[0] != 0; } diff --git a/Source/Core/DiscIO/WiiWad.cpp b/Source/Core/DiscIO/WiiWad.cpp index 0c93d601a9..9e85669204 100644 --- a/Source/Core/DiscIO/WiiWad.cpp +++ b/Source/Core/DiscIO/WiiWad.cpp @@ -6,11 +6,11 @@ #include #include +#include "Common/Align.h" #include "Common/Assert.h" #include "Common/CommonTypes.h" #include "Common/FileUtil.h" #include "Common/Logging/Log.h" -#include "Common/MathUtil.h" #include "DiscIO/Blob.h" #include "DiscIO/WiiWad.h" @@ -87,15 +87,15 @@ bool WiiWAD::ParseWAD(IBlobReader& reader) u32 offset = 0x40; m_certificate_chain = CreateWADEntry(reader, certificate_chain_size, offset); - offset += ROUND_UP(certificate_chain_size, 0x40); + offset += Common::AlignUp(certificate_chain_size, 0x40); m_ticket = CreateWADEntry(reader, ticket_size, offset); - offset += ROUND_UP(ticket_size, 0x40); + offset += Common::AlignUp(ticket_size, 0x40); m_tmd = CreateWADEntry(reader, tmd_size, offset); - offset += ROUND_UP(tmd_size, 0x40); + offset += Common::AlignUp(tmd_size, 0x40); m_data_app = CreateWADEntry(reader, data_app_size, offset); - offset += ROUND_UP(data_app_size, 0x40); + offset += Common::AlignUp(data_app_size, 0x40); m_footer = CreateWADEntry(reader, footer_size, offset); - offset += ROUND_UP(footer_size, 0x40); + offset += Common::AlignUp(footer_size, 0x40); return true; } diff --git a/Source/Core/VideoBackends/D3D/D3DUtil.cpp b/Source/Core/VideoBackends/D3D/D3DUtil.cpp index 639b1988c0..6e73bea9a8 100644 --- a/Source/Core/VideoBackends/D3D/D3DUtil.cpp +++ b/Source/Core/VideoBackends/D3D/D3DUtil.cpp @@ -6,6 +6,7 @@ #include #include +#include "Common/Align.h" #include "VideoBackends/D3D/D3DBase.h" #include "VideoBackends/D3D/D3DShader.h" #include "VideoBackends/D3D/D3DState.h" @@ -22,7 +23,7 @@ namespace D3D class UtilVertexBuffer { public: - UtilVertexBuffer(int size) : buf(nullptr), offset(0), max_size(size) + UtilVertexBuffer(unsigned int size) : max_size(size) { D3D11_BUFFER_DESC desc = CD3D11_BUFFER_DESC(max_size, D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); @@ -31,7 +32,7 @@ public: ~UtilVertexBuffer() { buf->Release(); } int GetSize() const { return max_size; } // returns vertex offset to the new data - int AppendData(void* data, int size, int vertex_size) + int AppendData(void* data, unsigned int size, unsigned int vertex_size) { D3D11_MAPPED_SUBRESOURCE map; if (offset + size >= max_size) @@ -47,8 +48,7 @@ public: { context->Map(buf, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &map); } - offset = ((offset + vertex_size - 1) / vertex_size) * - vertex_size; // align offset to vertex_size bytes + offset = Common::AlignUp(offset, vertex_size); memcpy((u8*)map.pData + offset, data, size); context->Unmap(buf, 0); @@ -56,13 +56,12 @@ public: return (offset - size) / vertex_size; } - int BeginAppendData(void** write_ptr, int size, int vertex_size) + int BeginAppendData(void** write_ptr, unsigned int size, unsigned int vertex_size) { _dbg_assert_(VIDEO, size < max_size); D3D11_MAPPED_SUBRESOURCE map; - int aligned_offset = ((offset + vertex_size - 1) / vertex_size) * - vertex_size; // align offset to vertex_size bytes + unsigned int aligned_offset = Common::AlignUp(offset, vertex_size); if (aligned_offset + size > max_size) { // wrap buffer around and notify observers @@ -87,9 +86,9 @@ public: void AddWrapObserver(bool* observer) { observers.push_back(observer); } inline ID3D11Buffer*& GetBuffer() { return buf; } private: - ID3D11Buffer* buf; - int offset; - int max_size; + ID3D11Buffer* buf = nullptr; + unsigned int offset = 0; + unsigned int max_size; std::list observers; }; diff --git a/Source/Core/VideoBackends/D3D/GeometryShaderCache.cpp b/Source/Core/VideoBackends/D3D/GeometryShaderCache.cpp index 85b84eaf0d..1d8cef5fd9 100644 --- a/Source/Core/VideoBackends/D3D/GeometryShaderCache.cpp +++ b/Source/Core/VideoBackends/D3D/GeometryShaderCache.cpp @@ -4,6 +4,7 @@ #include +#include "Common/Align.h" #include "Common/FileUtil.h" #include "Common/LinearDiskCache.h" #include "Common/StringUtil.h" @@ -135,7 +136,8 @@ const char copy_shader_code[] = { void GeometryShaderCache::Init() { - unsigned int gbsize = ROUND_UP(sizeof(GeometryShaderConstants), 16); // must be a multiple of 16 + unsigned int gbsize = Common::AlignUp(static_cast(sizeof(GeometryShaderConstants)), + 16); // must be a multiple of 16 D3D11_BUFFER_DESC gbdesc = CD3D11_BUFFER_DESC(gbsize, D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); HRESULT hr = D3D::device->CreateBuffer(&gbdesc, nullptr, &gscbuf); diff --git a/Source/Core/VideoBackends/D3D/PixelShaderCache.cpp b/Source/Core/VideoBackends/D3D/PixelShaderCache.cpp index df65d562e0..5364c58cdd 100644 --- a/Source/Core/VideoBackends/D3D/PixelShaderCache.cpp +++ b/Source/Core/VideoBackends/D3D/PixelShaderCache.cpp @@ -4,6 +4,7 @@ #include +#include "Common/Align.h" #include "Common/CommonTypes.h" #include "Common/FileUtil.h" #include "Common/LinearDiskCache.h" @@ -457,7 +458,8 @@ public: void PixelShaderCache::Init() { - unsigned int cbsize = ROUND_UP(sizeof(PixelShaderConstants), 16); // must be a multiple of 16 + unsigned int cbsize = Common::AlignUp(static_cast(sizeof(PixelShaderConstants)), + 16); // must be a multiple of 16 D3D11_BUFFER_DESC cbdesc = CD3D11_BUFFER_DESC(cbsize, D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); D3D::device->CreateBuffer(&cbdesc, nullptr, &pscbuf); diff --git a/Source/Core/VideoBackends/D3D/VertexShaderCache.cpp b/Source/Core/VideoBackends/D3D/VertexShaderCache.cpp index 02b027f2ba..a698ed6ebd 100644 --- a/Source/Core/VideoBackends/D3D/VertexShaderCache.cpp +++ b/Source/Core/VideoBackends/D3D/VertexShaderCache.cpp @@ -4,6 +4,7 @@ #include +#include "Common/Align.h" #include "Common/CommonTypes.h" #include "Common/FileUtil.h" #include "Common/LinearDiskCache.h" @@ -122,7 +123,8 @@ void VertexShaderCache::Init() {"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}, }; - unsigned int cbsize = ROUND_UP(sizeof(VertexShaderConstants), 16); // must be a multiple of 16 + unsigned int cbsize = Common::AlignUp(static_cast(sizeof(VertexShaderConstants)), + 16); // must be a multiple of 16 D3D11_BUFFER_DESC cbdesc = CD3D11_BUFFER_DESC(cbsize, D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); HRESULT hr = D3D::device->CreateBuffer(&cbdesc, nullptr, &vscbuf); diff --git a/Source/Core/VideoBackends/D3D12/D3DTexture.cpp b/Source/Core/VideoBackends/D3D12/D3DTexture.cpp index 1d8146f1e6..20b0eb5b39 100644 --- a/Source/Core/VideoBackends/D3D12/D3DTexture.cpp +++ b/Source/Core/VideoBackends/D3D12/D3DTexture.cpp @@ -4,6 +4,7 @@ #include +#include "Common/Align.h" #include "Common/CommonTypes.h" #include "Common/MsgHandler.h" #include "VideoBackends/D3D12/D3DBase.h" @@ -34,7 +35,7 @@ void ReplaceRGBATexture2D(ID3D12Resource* texture12, const u8* buffer, unsigned D3D12_RESOURCE_STATES current_resource_state) { const unsigned int upload_size = - AlignValue(src_pitch, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) * height; + Common::AlignUp(src_pitch, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) * height; ID3D12Resource* upload_buffer = nullptr; size_t upload_buffer_offset = 0; diff --git a/Source/Core/VideoBackends/D3D12/D3DUtil.cpp b/Source/Core/VideoBackends/D3D12/D3DUtil.cpp index 61e39d376a..4b7e4767c3 100644 --- a/Source/Core/VideoBackends/D3D12/D3DUtil.cpp +++ b/Source/Core/VideoBackends/D3D12/D3DUtil.cpp @@ -7,6 +7,8 @@ #include #include +#include "Common/Align.h" + #include "VideoBackends/D3D12/D3DBase.h" #include "VideoBackends/D3D12/D3DCommandListManager.h" #include "VideoBackends/D3D12/D3DDescriptorHeapManager.h" @@ -254,8 +256,9 @@ int CD3DFont::Init() ID3D12Resource* temporaryFontTextureUploadBuffer; CheckHR(D3D::device12->CreateCommittedResource( &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD), D3D12_HEAP_FLAG_NONE, - &CD3DX12_RESOURCE_DESC::Buffer( - AlignValue(m_tex_width * 4, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) * m_tex_height), + &CD3DX12_RESOURCE_DESC::Buffer(Common::AlignUp(static_cast(m_tex_width) * 4, + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) * + m_tex_height), D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&temporaryFontTextureUploadBuffer))); D3D12_SUBRESOURCE_DATA subresource_data_dest = { diff --git a/Source/Core/VideoBackends/D3D12/D3DUtil.h b/Source/Core/VideoBackends/D3D12/D3DUtil.h index 942f9bcdec..9b3794cc05 100644 --- a/Source/Core/VideoBackends/D3D12/D3DUtil.h +++ b/Source/Core/VideoBackends/D3D12/D3DUtil.h @@ -7,7 +7,6 @@ #include #include -#include "Common/MathUtil.h" #include "VideoBackends/D3D12/D3DState.h" #include "VideoBackends/D3D12/D3DStreamBuffer.h" @@ -19,11 +18,6 @@ extern StateCache gx_state_cache; namespace D3D { -constexpr unsigned int AlignValue(unsigned int value, unsigned int alignment) -{ - return (value + (alignment - 1)) & ~(alignment - 1); -} - void ResourceBarrier(ID3D12GraphicsCommandList* command_list, ID3D12Resource* resource, D3D12_RESOURCE_STATES state_before, D3D12_RESOURCE_STATES state_after, UINT subresource); diff --git a/Source/Core/VideoBackends/D3D12/FramebufferManager.cpp b/Source/Core/VideoBackends/D3D12/FramebufferManager.cpp index f567852568..77b2a411ae 100644 --- a/Source/Core/VideoBackends/D3D12/FramebufferManager.cpp +++ b/Source/Core/VideoBackends/D3D12/FramebufferManager.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include "VideoBackends/D3D12/FramebufferManager.h" +#include "Common/Align.h" #include "Core/HW/Memmap.h" #include "VideoBackends/D3D12/D3DBase.h" #include "VideoBackends/D3D12/D3DCommandListManager.h" @@ -324,8 +325,8 @@ void FramebufferManager::InitializeEFBAccessCopies() buf12->Release(); // EFB access - color staging/readback buffer - m_efb.color_access_readback_pitch = - D3D::AlignValue(EFB_WIDTH * sizeof(u32), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); + m_efb.color_access_readback_pitch = Common::AlignUp(static_cast(EFB_WIDTH * sizeof(u32)), + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); texdesc12 = CD3DX12_RESOURCE_DESC::Buffer(m_efb.color_access_readback_pitch * EFB_HEIGHT); hr = D3D::device12->CreateCommittedResource( &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK), D3D12_HEAP_FLAG_NONE, &texdesc12, @@ -347,8 +348,8 @@ void FramebufferManager::InitializeEFBAccessCopies() buf12->Release(); // EFB access - depth staging/readback buffer - m_efb.depth_access_readback_pitch = - D3D::AlignValue(EFB_WIDTH * sizeof(float), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); + m_efb.depth_access_readback_pitch = Common::AlignUp(static_cast(EFB_WIDTH * sizeof(float)), + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); texdesc12 = CD3DX12_RESOURCE_DESC::Buffer(m_efb.depth_access_readback_pitch * EFB_HEIGHT); hr = D3D::device12->CreateCommittedResource( &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK), D3D12_HEAP_FLAG_NONE, &texdesc12, diff --git a/Source/Core/VideoBackends/D3D12/PSTextureEncoder.cpp b/Source/Core/VideoBackends/D3D12/PSTextureEncoder.cpp index 25c5dd92c5..f95c1528eb 100644 --- a/Source/Core/VideoBackends/D3D12/PSTextureEncoder.cpp +++ b/Source/Core/VideoBackends/D3D12/PSTextureEncoder.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include "VideoBackends/D3D12/PSTextureEncoder.h" +#include "Common/Align.h" #include "Core/HW/Memmap.h" #include "VideoBackends/D3D12/D3DBase.h" #include "VideoBackends/D3D12/D3DCommandListManager.h" @@ -62,7 +63,7 @@ void PSTextureEncoder::Init() CheckHR(D3D::device12->CreateCommittedResource( &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK), D3D12_HEAP_FLAG_NONE, &CD3DX12_RESOURCE_DESC::Buffer( - D3D::AlignValue(static_cast(out_tex_desc.Width) * 4, + Common::AlignUp(static_cast(out_tex_desc.Width) * 4, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) * out_tex_desc.Height), D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&m_out_readback_buffer))); @@ -179,7 +180,7 @@ void PSTextureEncoder::Encode(u8* dst, u32 format, u32 native_width, u32 bytes_p dst_location.PlacedFootprint.Footprint.Width = EFB_WIDTH * 4; dst_location.PlacedFootprint.Footprint.Height = EFB_HEIGHT / 4; dst_location.PlacedFootprint.Footprint.Depth = 1; - dst_location.PlacedFootprint.Footprint.RowPitch = D3D::AlignValue( + dst_location.PlacedFootprint.Footprint.RowPitch = Common::AlignUp( dst_location.PlacedFootprint.Footprint.Width * 4, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); D3D12_TEXTURE_COPY_LOCATION src_location = {}; diff --git a/Source/Core/VideoBackends/D3D12/Render.cpp b/Source/Core/VideoBackends/D3D12/Render.cpp index 2bfe8d3a07..be65d04f4b 100644 --- a/Source/Core/VideoBackends/D3D12/Render.cpp +++ b/Source/Core/VideoBackends/D3D12/Render.cpp @@ -9,6 +9,7 @@ #include #include +#include "Common/Align.h" #include "Common/CommonTypes.h" #include "Common/FileUtil.h" #include "Common/MathUtil.h" @@ -180,7 +181,7 @@ void CreateScreenshotTexture() // This texture is released to be recreated when the window is resized in Renderer::SwapImpl. const unsigned int screenshot_buffer_size = - D3D::AlignValue(D3D::GetBackBufferWidth() * 4, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) * + Common::AlignUp(D3D::GetBackBufferWidth() * 4, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) * D3D::GetBackBufferHeight(); CheckHR(D3D::device12->CreateCommittedResource( @@ -757,7 +758,7 @@ void Renderer::SwapImpl(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height dst_location.PlacedFootprint.Footprint.Width = GetTargetRectangle().GetWidth(); dst_location.PlacedFootprint.Footprint.Height = GetTargetRectangle().GetHeight(); dst_location.PlacedFootprint.Footprint.Depth = 1; - dst_location.PlacedFootprint.Footprint.RowPitch = D3D::AlignValue( + dst_location.PlacedFootprint.Footprint.RowPitch = Common::AlignUp( dst_location.PlacedFootprint.Footprint.Width * 4, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); D3D12_TEXTURE_COPY_LOCATION src_location = {}; diff --git a/Source/Core/VideoBackends/D3D12/TextureCache.cpp b/Source/Core/VideoBackends/D3D12/TextureCache.cpp index ffedeeeadc..322cec9b1d 100644 --- a/Source/Core/VideoBackends/D3D12/TextureCache.cpp +++ b/Source/Core/VideoBackends/D3D12/TextureCache.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include "VideoBackends/D3D12/TextureCache.h" +#include "Common/Align.h" #include "VideoBackends/D3D12/D3DBase.h" #include "VideoBackends/D3D12/D3DCommandListManager.h" #include "VideoBackends/D3D12/D3DDescriptorHeapManager.h" @@ -44,7 +45,7 @@ bool TextureCache::TCacheEntry::Save(const std::string& filename, unsigned int l u32 level_width = std::max(config.width >> level, 1u); u32 level_height = std::max(config.height >> level, 1u); size_t level_pitch = - D3D::AlignValue(level_width * sizeof(u32), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); + Common::AlignUp(level_width * sizeof(u32), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); size_t required_readback_buffer_size = level_pitch * level_height; // Check if the current readback buffer is large enough diff --git a/Source/Core/VideoBackends/D3D12/XFBEncoder.cpp b/Source/Core/VideoBackends/D3D12/XFBEncoder.cpp index a2b2bc1998..9e0d15327e 100644 --- a/Source/Core/VideoBackends/D3D12/XFBEncoder.cpp +++ b/Source/Core/VideoBackends/D3D12/XFBEncoder.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include "VideoBackends/D3D12/XFBEncoder.h" +#include "Common/Align.h" #include "Common/CommonTypes.h" #include "Common/Logging/Log.h" #include "Common/MsgHandler.h" @@ -23,7 +24,7 @@ constexpr size_t XFB_TEXTURE_HEIGHT = MAX_XFB_HEIGHT; // Buffer enough space for 2 XFB buffers (our frame latency) constexpr size_t XFB_UPLOAD_BUFFER_SIZE = - D3D::AlignValue(XFB_TEXTURE_WIDTH * sizeof(u32), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) * + Common::AlignUp(XFB_TEXTURE_WIDTH * sizeof(u32), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) * XFB_TEXTURE_HEIGHT * 2; constexpr size_t XFB_ENCODER_PARAMS_BUFFER_SIZE = 64 * 1024; @@ -48,7 +49,7 @@ XFBEncoder::XFBEncoder() CheckHR(D3D::device12->CreateCommittedResource( &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK), D3D12_HEAP_FLAG_NONE, &CD3DX12_RESOURCE_DESC::Buffer( - D3D::AlignValue(XFB_TEXTURE_WIDTH * sizeof(u32), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) * + Common::AlignUp(XFB_TEXTURE_WIDTH * sizeof(u32), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) * MAX_XFB_HEIGHT), D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&m_readback_buffer))); @@ -117,7 +118,8 @@ void XFBEncoder::EncodeTextureToRam(u8* dst, u32 dst_pitch, u32 dst_height, // Copy from YUYV intermediate texture to readback buffer. It's likely the pitch here is going to // be different to dst_pitch. - u32 readback_pitch = D3D::AlignValue(dst_width * sizeof(u16), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); + u32 readback_pitch = static_cast( + Common::AlignUp(dst_width * sizeof(u16), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT)); D3D12_PLACED_SUBRESOURCE_FOOTPRINT dst_footprint = { 0, {DXGI_FORMAT_R8G8B8A8_UNORM, dst_texture_width, dst_height, 1, readback_pitch}}; CD3DX12_TEXTURE_COPY_LOCATION dst_location(m_readback_buffer, dst_footprint); @@ -154,8 +156,8 @@ void XFBEncoder::DecodeToTexture(D3DTexture2D* dst_texture, const u8* src, u32 s "XFB source does not exceed maximum size"); // Copy to XFB upload buffer. Each row has to be done separately due to pitch differences. - u32 buffer_pitch = - D3D::AlignValue(src_width / 2 * sizeof(u32), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); + u32 buffer_pitch = static_cast( + Common::AlignUp(src_width / 2 * sizeof(u32), D3D12_TEXTURE_DATA_PITCH_ALIGNMENT)); m_upload_buffer->AllocateSpaceInBuffer(buffer_pitch * src_height, D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT); for (u32 row = 0; row < src_height; row++) diff --git a/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp b/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp index 66952ed976..44bf063a8a 100644 --- a/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp +++ b/Source/Core/VideoBackends/OGL/ProgramShaderCache.cpp @@ -5,8 +5,8 @@ #include #include +#include "Common/Align.h" #include "Common/Common.h" -#include "Common/MathUtil.h" #include "Common/StringUtil.h" #include "Core/ConfigManager.h" @@ -148,22 +148,22 @@ void ProgramShaderCache::UploadConstants() memcpy(buffer.first, &PixelShaderManager::constants, sizeof(PixelShaderConstants)); - memcpy(buffer.first + ROUND_UP(sizeof(PixelShaderConstants), s_ubo_align), + memcpy(buffer.first + Common::AlignUp(sizeof(PixelShaderConstants), s_ubo_align), &VertexShaderManager::constants, sizeof(VertexShaderConstants)); - memcpy(buffer.first + ROUND_UP(sizeof(PixelShaderConstants), s_ubo_align) + - ROUND_UP(sizeof(VertexShaderConstants), s_ubo_align), + memcpy(buffer.first + Common::AlignUp(sizeof(PixelShaderConstants), s_ubo_align) + + Common::AlignUp(sizeof(VertexShaderConstants), s_ubo_align), &GeometryShaderManager::constants, sizeof(GeometryShaderConstants)); s_buffer->Unmap(s_ubo_buffer_size); glBindBufferRange(GL_UNIFORM_BUFFER, 1, s_buffer->m_buffer, buffer.second, sizeof(PixelShaderConstants)); glBindBufferRange(GL_UNIFORM_BUFFER, 2, s_buffer->m_buffer, - buffer.second + ROUND_UP(sizeof(PixelShaderConstants), s_ubo_align), + buffer.second + Common::AlignUp(sizeof(PixelShaderConstants), s_ubo_align), sizeof(VertexShaderConstants)); glBindBufferRange(GL_UNIFORM_BUFFER, 3, s_buffer->m_buffer, - buffer.second + ROUND_UP(sizeof(PixelShaderConstants), s_ubo_align) + - ROUND_UP(sizeof(VertexShaderConstants), s_ubo_align), + buffer.second + Common::AlignUp(sizeof(PixelShaderConstants), s_ubo_align) + + Common::AlignUp(sizeof(VertexShaderConstants), s_ubo_align), sizeof(GeometryShaderConstants)); PixelShaderManager::dirty = false; @@ -407,9 +407,10 @@ void ProgramShaderCache::Init() // then the UBO will fail. glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &s_ubo_align); - s_ubo_buffer_size = ROUND_UP(sizeof(PixelShaderConstants), s_ubo_align) + - ROUND_UP(sizeof(VertexShaderConstants), s_ubo_align) + - ROUND_UP(sizeof(GeometryShaderConstants), s_ubo_align); + s_ubo_buffer_size = + static_cast(Common::AlignUp(sizeof(PixelShaderConstants), s_ubo_align) + + Common::AlignUp(sizeof(VertexShaderConstants), s_ubo_align) + + Common::AlignUp(sizeof(GeometryShaderConstants), s_ubo_align)); // We multiply by *4*4 because we need to get down to basic machine units. // So multiply by four to get how many floats we have from vec4s diff --git a/Source/Core/VideoBackends/OGL/StreamBuffer.cpp b/Source/Core/VideoBackends/OGL/StreamBuffer.cpp index a81d96b0ff..97a25a5bc7 100644 --- a/Source/Core/VideoBackends/OGL/StreamBuffer.cpp +++ b/Source/Core/VideoBackends/OGL/StreamBuffer.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. +#include "Common/Align.h" #include "Common/GL/GLUtil.h" #include "Common/MemoryUtil.h" @@ -261,11 +262,11 @@ public: PinnedMemory(u32 type, u32 size) : StreamBuffer(type, size) { CreateFences(); - m_pointer = static_cast( - Common::AllocateAlignedMemory(ROUND_UP(m_size, ALIGN_PINNED_MEMORY), ALIGN_PINNED_MEMORY)); + m_pointer = static_cast(Common::AllocateAlignedMemory( + Common::AlignUp(m_size, ALIGN_PINNED_MEMORY), ALIGN_PINNED_MEMORY)); glBindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, m_buffer); - glBufferData(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, ROUND_UP(m_size, ALIGN_PINNED_MEMORY), - m_pointer, GL_STREAM_COPY); + glBufferData(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, + Common::AlignUp(m_size, ALIGN_PINNED_MEMORY), m_pointer, GL_STREAM_COPY); glBindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, 0); glBindBuffer(m_buffertype, m_buffer); } diff --git a/Source/Core/VideoBackends/Software/TextureEncoder.cpp b/Source/Core/VideoBackends/Software/TextureEncoder.cpp index 15e159b30f..dcb29de2a5 100644 --- a/Source/Core/VideoBackends/Software/TextureEncoder.cpp +++ b/Source/Core/VideoBackends/Software/TextureEncoder.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. +#include "Common/Align.h" #include "Common/CommonFuncs.h" #include "Common/CommonTypes.h" #include "Common/MsgHandler.h" @@ -215,7 +216,7 @@ static void SetSpans(int sBlkSize, int tBlkSize, s32* tSpan, s32* sBlkSpan, s32* { // width is 1 less than the number of pixels of width u32 width = bpmem.copyTexSrcWH.x >> bpmem.triggerEFBCopy.half_scale; - u32 alignedWidth = (width + sBlkSize) & (~(sBlkSize - 1)); + u32 alignedWidth = Common::AlignUp(width, sBlkSize); u32 readStride = 3 << bpmem.triggerEFBCopy.half_scale; diff --git a/Source/Core/VideoBackends/Vulkan/StateTracker.cpp b/Source/Core/VideoBackends/Vulkan/StateTracker.cpp index 34ea98f41b..e9bbb980b0 100644 --- a/Source/Core/VideoBackends/Vulkan/StateTracker.cpp +++ b/Source/Core/VideoBackends/Vulkan/StateTracker.cpp @@ -6,6 +6,7 @@ #include +#include "Common/Align.h" #include "Common/Assert.h" #include "VideoBackends/Vulkan/CommandBufferManager.h" @@ -101,11 +102,11 @@ bool StateTracker::Initialize() // To work around this we reserve the maximum buffer size at all times, but only commit // as many bytes as we use. m_uniform_buffer_reserve_size = sizeof(PixelShaderConstants); - m_uniform_buffer_reserve_size = Util::AlignValue(m_uniform_buffer_reserve_size, - g_vulkan_context->GetUniformBufferAlignment()) + + m_uniform_buffer_reserve_size = Common::AlignUp(m_uniform_buffer_reserve_size, + g_vulkan_context->GetUniformBufferAlignment()) + sizeof(VertexShaderConstants); - m_uniform_buffer_reserve_size = Util::AlignValue(m_uniform_buffer_reserve_size, - g_vulkan_context->GetUniformBufferAlignment()) + + m_uniform_buffer_reserve_size = Common::AlignUp(m_uniform_buffer_reserve_size, + g_vulkan_context->GetUniformBufferAlignment()) + sizeof(GeometryShaderConstants); // Default dirty flags include all descriptors @@ -461,9 +462,9 @@ void StateTracker::UploadAllConstants() size_t ub_alignment = g_vulkan_context->GetUniformBufferAlignment(); size_t pixel_constants_offset = 0; size_t vertex_constants_offset = - Util::AlignValue(pixel_constants_offset + sizeof(PixelShaderConstants), ub_alignment); + Common::AlignUp(pixel_constants_offset + sizeof(PixelShaderConstants), ub_alignment); size_t geometry_constants_offset = - Util::AlignValue(vertex_constants_offset + sizeof(VertexShaderConstants), ub_alignment); + Common::AlignUp(vertex_constants_offset + sizeof(VertexShaderConstants), ub_alignment); size_t allocation_size = geometry_constants_offset + sizeof(GeometryShaderConstants); // Allocate everything at once. diff --git a/Source/Core/VideoBackends/Vulkan/Util.cpp b/Source/Core/VideoBackends/Vulkan/Util.cpp index f85321bac5..f49ca90580 100644 --- a/Source/Core/VideoBackends/Vulkan/Util.cpp +++ b/Source/Core/VideoBackends/Vulkan/Util.cpp @@ -4,6 +4,7 @@ #include "VideoBackends/Vulkan/Util.h" +#include "Common/Align.h" #include "Common/Assert.h" #include "Common/CommonFuncs.h" #include "Common/MathUtil.h" @@ -20,22 +21,13 @@ namespace Vulkan { namespace Util { -size_t AlignValue(size_t value, size_t alignment) -{ - // Have to use mod rather than masking bits in case alignment is not a power of two. - size_t offset = value % alignment; - if (offset != 0) - value += (alignment - offset); - return value; -} - size_t AlignBufferOffset(size_t offset, size_t alignment) { // Assume an offset of zero is already aligned to a value larger than alignment. if (offset == 0) return 0; - return AlignValue(offset, alignment); + return Common::AlignUp(offset, alignment); } u32 MakeRGBA8Color(float r, float g, float b, float a) diff --git a/Source/Core/VideoBackends/Vulkan/Util.h b/Source/Core/VideoBackends/Vulkan/Util.h index bb701716c2..7ee8e82356 100644 --- a/Source/Core/VideoBackends/Vulkan/Util.h +++ b/Source/Core/VideoBackends/Vulkan/Util.h @@ -19,7 +19,6 @@ class StateTracker; namespace Util { -size_t AlignValue(size_t value, size_t alignment); size_t AlignBufferOffset(size_t offset, size_t alignment); u32 MakeRGBA8Color(float r, float g, float b, float a); diff --git a/Source/Core/VideoCommon/TextureCacheBase.cpp b/Source/Core/VideoCommon/TextureCacheBase.cpp index 05546f940e..9b5df69627 100644 --- a/Source/Core/VideoCommon/TextureCacheBase.cpp +++ b/Source/Core/VideoCommon/TextureCacheBase.cpp @@ -8,11 +8,13 @@ #include #include +#include "Common/Align.h" #include "Common/Assert.h" #include "Common/CommonTypes.h" #include "Common/FileUtil.h" #include "Common/Hash.h" #include "Common/Logging/Log.h" +#include "Common/MathUtil.h" #include "Common/MemoryUtil.h" #include "Common/StringUtil.h" @@ -511,8 +513,8 @@ TextureCacheBase::TCacheEntryBase* TextureCacheBase::Load(const u32 stage) const unsigned int bsw = TexDecoder_GetBlockWidthInTexels(texformat); const unsigned int bsh = TexDecoder_GetBlockHeightInTexels(texformat); - unsigned int expandedWidth = ROUND_UP(width, bsw); - unsigned int expandedHeight = ROUND_UP(height, bsh); + unsigned int expandedWidth = Common::AlignUp(width, bsw); + unsigned int expandedHeight = Common::AlignUp(height, bsh); const unsigned int nativeW = width; const unsigned int nativeH = height; @@ -546,8 +548,8 @@ TextureCacheBase::TCacheEntryBase* TextureCacheBase::Load(const u32 stage) for (u32 level = 1; level != tex_levels; ++level) { // We still need to calculate the original size of the mips - const u32 expanded_mip_width = ROUND_UP(CalculateLevelSize(width, level), bsw); - const u32 expanded_mip_height = ROUND_UP(CalculateLevelSize(height, level), bsh); + const u32 expanded_mip_width = Common::AlignUp(CalculateLevelSize(width, level), bsw); + const u32 expanded_mip_height = Common::AlignUp(CalculateLevelSize(height, level), bsh); additional_mips_size += TexDecoder_GetTextureSizeInBytes(expanded_mip_width, expanded_mip_height, texformat); @@ -846,8 +848,8 @@ TextureCacheBase::TCacheEntryBase* TextureCacheBase::Load(const u32 stage) { const u32 mip_width = CalculateLevelSize(width, level); const u32 mip_height = CalculateLevelSize(height, level); - const u32 expanded_mip_width = ROUND_UP(mip_width, bsw); - const u32 expanded_mip_height = ROUND_UP(mip_height, bsh); + const u32 expanded_mip_width = Common::AlignUp(mip_width, bsw); + const u32 expanded_mip_height = Common::AlignUp(mip_height, bsh); const u8*& mip_src_data = from_tmem ? ((level % 2) ? ptr_odd : ptr_even) : src_data; const u8* tlut = &texMem[tlutaddr]; @@ -1235,8 +1237,8 @@ void TextureCacheBase::CopyRenderTargetToTexture(u32 dstAddr, unsigned int dstFo const u32 blockW = TexDecoder_GetBlockWidthInTexels(baseFormat); // Round up source height to multiple of block size - u32 actualHeight = ROUND_UP(tex_h, blockH); - const u32 actualWidth = ROUND_UP(tex_w, blockW); + u32 actualHeight = Common::AlignUp(tex_h, blockH); + const u32 actualWidth = Common::AlignUp(tex_w, blockW); u32 num_blocks_y = actualHeight / blockH; const u32 num_blocks_x = actualWidth / blockW; @@ -1429,7 +1431,7 @@ u32 TextureCacheBase::TCacheEntryBase::BytesPerRow() const const u32 blockW = TexDecoder_GetBlockWidthInTexels(format); // Round up source height to multiple of block size - const u32 actualWidth = ROUND_UP(native_width, blockW); + const u32 actualWidth = Common::AlignUp(native_width, blockW); const u32 numBlocksX = actualWidth / blockW; @@ -1443,7 +1445,7 @@ u32 TextureCacheBase::TCacheEntryBase::NumBlocksY() const { u32 blockH = TexDecoder_GetBlockHeightInTexels(format); // Round up source height to multiple of block size - u32 actualHeight = ROUND_UP(native_height, blockH); + u32 actualHeight = Common::AlignUp(native_height, blockH); return actualHeight / blockH; }