diff --git a/Common/Data/Collections/TinySet.h b/Common/Data/Collections/TinySet.h index 8a7cf89fa6..219a26d3cd 100644 --- a/Common/Data/Collections/TinySet.h +++ b/Common/Data/Collections/TinySet.h @@ -3,10 +3,12 @@ #include // Insert-only small-set implementation. Performs no allocation unless MaxFastSize is exceeded. +// Can also be used as a small vector, then use push_back (or push_in_place) instead of insert. +// Duplicates are thus allowed if you use that, but not if you exclusively use insert. template struct TinySet { ~TinySet() { delete slowLookup_; } - inline void insert(T t) { + inline void insert(const T &t) { // Fast linear scan. for (int i = 0; i < fastCount; i++) { if (fastLookup_[i] == t) @@ -20,6 +22,27 @@ struct TinySet { // Fall back to slow path. insertSlow(t); } + inline void push_back(const T &t) { + if (fastCount < MaxFastSize) { + fastLookup_[fastCount++] = t; + return; + } + if (!slowLookup_) { + slowLookup_ = new std::vector(); + } + slowLookup_->push_back(t); + } + inline T *add_back() { + if (fastCount < MaxFastSize) { + return &fastLookup_[fastCount++]; + } + if (!slowLookup_) { + slowLookup_ = new std::vector(); + } + T t; + slowLookup_->push_back(t); + return slowLookup_->back(); + } bool contains(T t) const { for (int i = 0; i < fastCount; i++) { if (fastLookup_[i] == t) @@ -52,6 +75,23 @@ struct TinySet { slowLookup_ = nullptr; fastCount = 0; } + bool empty() const { + return fastCount == 0; + } + size_t size() const { + if (fastCount <= MaxFastSize) { + return fastCount; + } else { + return slowLookup_->size() + MaxFastSize; + } + } + const T &operator[] (size_t index) const { + if (index < MaxFastSize) { + return fastLookup_[index]; + } else { + return (*slowLookup_)[index - MaxFastSize]; + } + } private: void insertSlow(T t) { diff --git a/GPU/Common/FramebufferManagerCommon.cpp b/GPU/Common/FramebufferManagerCommon.cpp index 5f111793ca..95535d5317 100644 --- a/GPU/Common/FramebufferManagerCommon.cpp +++ b/GPU/Common/FramebufferManagerCommon.cpp @@ -21,6 +21,7 @@ #include "Common/GPU/thin3d.h" #include "Common/GPU/OpenGL/GLFeatures.h" +#include "Common/Data/Collections/TinySet.h" #include "Common/Data/Convert/ColorConv.h" #include "Common/Data/Text/I18n.h" #include "Common/Math/lin/matrix4x4.h" @@ -1677,7 +1678,7 @@ bool FramebufferManagerCommon::FindTransferFramebuffer(u32 basePtr, int stride_p int x_bytes = x_pixels * bpp; int w_bytes = w_pixels * bpp; - std::vector candidates; + TinySet candidates; // We work entirely in bytes when we do the matching, because games don't consistently use bpps that match // that of their buffers. Then after matching we try to map the copy to the simplest operation that does @@ -1752,20 +1753,27 @@ bool FramebufferManagerCommon::FindTransferFramebuffer(u32 basePtr, int stride_p candidates.push_back(candidate); } + const BlockTransferRect *best = nullptr; // Sort candidates by just recency for now, we might add other. - std::sort(candidates.begin(), candidates.end()); + for (size_t i = 0; i < candidates.size(); i++) { + const BlockTransferRect *candidate = &candidates[i]; + if (!best || candidate->vfb->colorBindSeq > best->vfb->colorBindSeq) { + best = candidate; + } + } if (candidates.size() > 1) { - std::string log; - for (auto &candidate : candidates) { - log += " - " + candidate.ToString() + "\n"; + if (Reporting::ShouldLogNTimes("mulblock", 5)) { + std::string log; + for (size_t i = 0; i < candidates.size(); i++) { + log += " - " + candidates[i].ToString() + "\n"; + } + WARN_LOG(G3D, "Multiple framebuffer candidates for %08x/%d/%d %d,%d %dx%d (dest = %d):\n%s", basePtr, stride_pixels, bpp, x_pixels, y, w_pixels, h, (int)destination, log.c_str()); } - WARN_LOG_N_TIMES(mulblock, 5, G3D, "Multiple framebuffer candidates for %08x/%d/%d %d,%d %dx%d (dest = %d):\n%s", basePtr, stride_pixels, bpp, x_pixels, y, w_pixels, h, (int)destination, log.c_str()); } if (!candidates.empty()) { - // Pick the last candidate. - *rect = candidates.back(); + *rect = *best; return true; } else { if (Memory::IsVRAMAddress(basePtr) && destination && h >= 128) { diff --git a/GPU/Common/FramebufferManagerCommon.h b/GPU/Common/FramebufferManagerCommon.h index dad0a887f1..f78e52d13e 100644 --- a/GPU/Common/FramebufferManagerCommon.h +++ b/GPU/Common/FramebufferManagerCommon.h @@ -245,10 +245,6 @@ struct BlockTransferRect { int x_pixels() const { return x_bytes / BufferFormatBytesPerPixel(vfb->fb_format); } - - bool operator < (const BlockTransferRect &other) const { - return vfb->colorBindSeq < other.vfb->colorBindSeq; - } }; namespace Draw {