From 732aa96656ef605a7471972f6a66b663018c3b8a Mon Sep 17 00:00:00 2001 From: Stenzek Date: Thu, 29 Jun 2023 00:03:00 +1000 Subject: [PATCH] Common: Remove AlignedBuffer And replace its one use site (texture dumping). --- common/AlignedMalloc.h | 113 ------------------ .../GS/Renderers/HW/GSTextureReplacements.cpp | 11 +- pcsx2/GS/Renderers/OpenGL/GSTextureOGL.h | 1 - 3 files changed, 6 insertions(+), 119 deletions(-) diff --git a/common/AlignedMalloc.h b/common/AlignedMalloc.h index 894f863846..9c12a6849e 100644 --- a/common/AlignedMalloc.h +++ b/common/AlignedMalloc.h @@ -42,116 +42,3 @@ extern void _aligned_free(void* pmem); #define pcsx2_aligned_realloc(handle, new_size, align, old_size) \ _aligned_realloc(handle, new_size, align) #endif - -// -------------------------------------------------------------------------------------- -// AlignedBuffer -// -------------------------------------------------------------------------------------- -// A simple container class for an aligned allocation. By default, no bounds checking is -// performed, and there is no option for enabling bounds checking. If bounds checking and -// other features are needed, use the more robust SafeArray<> instead. -// -template -class AlignedBuffer -{ - static_assert(std::is_pod::value, "Must use a POD type"); - - struct Deleter - { - void operator()(T* ptr) - { - _aligned_free(ptr); - } - }; - - std::unique_ptr m_buffer; - std::size_t m_size; - -public: - AlignedBuffer(size_t size = 0) - { - Alloc(size); - } - - AlignedBuffer(const AlignedBuffer& copy) - { - Alloc(copy.m_size); - if (copy.m_size > 0) - std::memcpy(m_buffer.get(), copy.m_buffer.get(), copy.m_size); - } - - AlignedBuffer(AlignedBuffer&& move) - : m_buffer(std::move(move.m_buffer)) - , m_size(move.m_size) - { - move.m_size = 0; - } - - size_t GetSize() const { return m_size; } - size_t GetLength() const { return m_size; } - - void Alloc(size_t newsize) - { - m_size = newsize; - m_buffer.reset(); - if (!m_size) - return; - - m_buffer.reset(reinterpret_cast(_aligned_malloc(this->m_size * sizeof(T), align))); - if (!m_buffer) - throw std::bad_alloc(); - } - - void Resize(size_t newsize) - { - m_buffer.reset(reinterpret_cast(pcsx2_aligned_realloc(m_buffer.release(), newsize * sizeof(T), align, m_size * sizeof(T)))); - m_size = newsize; - - if (!m_buffer) - throw std::bad_alloc(); - } - - void Free() - { - Alloc(0); - } - - // Makes enough room for the requested size. Existing data in the array is retained. - void MakeRoomFor(uint size) - { - if (size <= m_size) - return; - Resize(size); - } - - AlignedBuffer& operator=(const AlignedBuffer& copy) - { - Alloc(copy.m_size); - if (copy.m_size > 0) - std::memcpy(m_buffer.get(), copy.m_buffer.get(), copy.m_size); - - return *this; - } - - AlignedBuffer& operator=(AlignedBuffer&& move) - { - m_buffer = std::move(move.m_buffer); - m_size = move.m_size; - move.m_size = 0; - return *this; - } - - T* GetPtr(uint idx = 0) const - { - return &m_buffer[idx]; - } - - T& operator[](uint idx) - { - return m_buffer[idx]; - } - - const T& operator[](uint idx) const - { - return m_buffer[idx]; - } -}; diff --git a/pcsx2/GS/Renderers/HW/GSTextureReplacements.cpp b/pcsx2/GS/Renderers/HW/GSTextureReplacements.cpp index 7088ffb929..781090a678 100644 --- a/pcsx2/GS/Renderers/HW/GSTextureReplacements.cpp +++ b/pcsx2/GS/Renderers/HW/GSTextureReplacements.cpp @@ -668,14 +668,15 @@ void GSTextureReplacements::DumpTexture(const GSTextureCache::HashCacheKey& hash // use per-texture buffer so we can compress the texture asynchronously and not block the GS thread // must be 32 byte aligned for ReadTexture(). - AlignedBuffer buffer(pitch * static_cast(read_height)); - psm.rtx(mem, mem.GetOffset(TEX0.TBP0, TEX0.TBW, TEX0.PSM), block_rect, buffer.GetPtr(), pitch, TEXA); + u8* buffer = static_cast(_aligned_malloc(pitch * static_cast(read_height), 32)); + psm.rtx(mem, mem.GetOffset(TEX0.TBP0, TEX0.TBW, TEX0.PSM), block_rect, buffer, pitch, TEXA); // okay, now we can actually dump it const u32 buffer_offset = ((rect.top - block_rect.top) * pitch) + ((rect.left - block_rect.left) * sizeof(u32)); - QueueWorkerThreadItem([filename = std::move(filename), tw, th, pitch, buffer = std::move(buffer), buffer_offset]() { - if (!SavePNGImage(filename.c_str(), tw, th, buffer.GetPtr() + buffer_offset, pitch)) - Console.Error("Failed to dump texture to '%s'.", filename.c_str()); + QueueWorkerThreadItem([filename = std::move(filename), tw, th, pitch, buffer, buffer_offset]() { + if (!SavePNGImage(filename.c_str(), tw, th, buffer + buffer_offset, pitch)) + Console.Error(fmt::format("Failed to dump texture to '{}'.", filename)); + _aligned_free(buffer); }); } diff --git a/pcsx2/GS/Renderers/OpenGL/GSTextureOGL.h b/pcsx2/GS/Renderers/OpenGL/GSTextureOGL.h index a1f2b903f2..4cb234d939 100644 --- a/pcsx2/GS/Renderers/OpenGL/GSTextureOGL.h +++ b/pcsx2/GS/Renderers/OpenGL/GSTextureOGL.h @@ -54,7 +54,6 @@ public: bool Save(const std::string& fn) final; void Swap(GSTexture* tex) final; - GSMap Read(const GSVector4i& r, AlignedBuffer& buffer); bool IsIntegerFormat() const { return (m_int_format == GL_RED_INTEGER || m_int_format == GL_RGBA_INTEGER);