Merge pull request #4482 from leoetlino/align

Common: Add alignment header
This commit is contained in:
Mat M 2016-12-08 06:06:05 -05:00 committed by GitHub
commit d7dc854b50
32 changed files with 146 additions and 119 deletions

View File

@ -0,0 +1,24 @@
// This file is under the public domain.
#pragma once
#include <cstddef>
#include <type_traits>
namespace Common
{
template <typename T>
constexpr T AlignUp(T value, size_t size)
{
static_assert(std::is_unsigned<T>(), "T must be an unsigned value.");
return static_cast<T>(value + (size - value % size) % size);
}
template <typename T>
constexpr T AlignDown(T value, size_t size)
{
static_assert(std::is_unsigned<T>(), "T must be an unsigned value.");
return static_cast<T>(value - value % size);
}
} // namespace Common

View File

@ -7,10 +7,10 @@
#include <cstring>
#include <vector>
#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<ARM64Reg> reg_pair;

View File

@ -35,6 +35,7 @@
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemGroup>
<ClInclude Include="Align.h" />
<ClInclude Include="Analytics.h" />
<ClInclude Include="Assert.h" />
<ClInclude Include="Atomic.h" />
@ -211,4 +212,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@ -18,6 +18,7 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Align.h" />
<ClInclude Include="Atomic.h" />
<ClInclude Include="Atomic_GCC.h" />
<ClInclude Include="Atomic_Win32.h" />
@ -293,4 +294,4 @@
<ItemGroup>
<Natvis Include="BitField.natvis" />
</ItemGroup>
</Project>
</Project>

View File

@ -180,9 +180,6 @@ struct Rectangle
float MathFloatVectorSum(const std::vector<float>&);
#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)
{

View File

@ -6,10 +6,10 @@
#include <zlib.h>
#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

View File

@ -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;
}

View File

@ -18,12 +18,12 @@
#include <string>
#include <vector>
#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<u8> file_data(file_size_rounded);
std::vector<u8> 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<u32>(Common::AlignUp(elem.size, BLOCK_SZ));
}
}
}

View File

@ -14,11 +14,11 @@
#include <utility>
#include <vector>
#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<u8>& 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());

View File

@ -10,12 +10,12 @@
#include <string>
#include <vector>
#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<u64>(entry.size, 1ull), 0x8000ull);
dataOffset = Common::AlignUp(dataOffset + std::max<u64>(entry.size, 1ull), 0x8000ull);
}
}

View File

@ -10,9 +10,9 @@
#include <utility>
#include <vector>
#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<IBlobReader> reader)
@ -35,9 +33,13 @@ CVolumeWAD::CVolumeWAD(std::unique_ptr<IBlobReader> 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()

View File

@ -9,6 +9,7 @@
#include <string>
#include <vector>
#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;
}

View File

@ -6,11 +6,11 @@
#include <memory>
#include <string>
#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;
}

View File

@ -6,6 +6,7 @@
#include <list>
#include <string>
#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<bool*> observers;
};

View File

@ -4,6 +4,7 @@
#include <string>
#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<unsigned int>(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);

View File

@ -4,6 +4,7 @@
#include <string>
#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<unsigned int>(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);

View File

@ -4,6 +4,7 @@
#include <string>
#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<unsigned int>(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);

View File

@ -4,6 +4,7 @@
#include <memory>
#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;

View File

@ -7,6 +7,8 @@
#include <memory>
#include <string>
#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<unsigned int>(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 = {

View File

@ -7,7 +7,6 @@
#include <memory>
#include <string>
#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);

View File

@ -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<u32>(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<u32>(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,

View File

@ -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<unsigned int>(out_tex_desc.Width) * 4,
Common::AlignUp(static_cast<unsigned int>(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 = {};

View File

@ -9,6 +9,7 @@
#include <strsafe.h>
#include <unordered_map>
#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 = {};

View File

@ -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

View File

@ -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<u32>(
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<u32>(
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++)

View File

@ -5,8 +5,8 @@
#include <memory>
#include <string>
#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<u32>(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

View File

@ -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<u8*>(
Common::AllocateAlignedMemory(ROUND_UP(m_size, ALIGN_PINNED_MEMORY), ALIGN_PINNED_MEMORY));
m_pointer = static_cast<u8*>(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);
}

View File

@ -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;

View File

@ -6,6 +6,7 @@
#include <cstring>
#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.

View File

@ -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)

View File

@ -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);

View File

@ -8,11 +8,13 @@
#include <string>
#include <utility>
#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;
}