From f2a5a5abe7620fcad0c524ffbf87ab8442cd4919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 16 Mar 2023 10:14:41 +0100 Subject: [PATCH] Avoid input/output arguments in the texture scaler too --- GPU/Common/TextureCacheCommon.cpp | 2 +- GPU/Common/TextureScalerCommon.cpp | 28 +++++++++++++++------------- GPU/Common/TextureScalerCommon.h | 6 +++--- GPU/Vulkan/TextureCacheVulkan.cpp | 2 +- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/GPU/Common/TextureCacheCommon.cpp b/GPU/Common/TextureCacheCommon.cpp index 70d8682d38..09fc10023e 100644 --- a/GPU/Common/TextureCacheCommon.cpp +++ b/GPU/Common/TextureCacheCommon.cpp @@ -2924,7 +2924,7 @@ void TextureCacheCommon::LoadTextureLevel(TexCacheEntry &entry, uint8_t *data, i if (plan.scaleFactor > 1) { // Note that this updates w and h! - scaler_.ScaleAlways((u32 *)data, pixelData, w, h, plan.scaleFactor); + scaler_.ScaleAlways((u32 *)data, pixelData, w, h, &w, &h, plan.scaleFactor); pixelData = (u32 *)data; decPitch = w * 4; diff --git a/GPU/Common/TextureScalerCommon.cpp b/GPU/Common/TextureScalerCommon.cpp index a2662a4c04..40e686bdde 100644 --- a/GPU/Common/TextureScalerCommon.cpp +++ b/GPU/Common/TextureScalerCommon.cpp @@ -605,29 +605,31 @@ bool TextureScalerCommon::IsEmptyOrFlat(const u32 *data, int pixels) const { return true; } -void TextureScalerCommon::ScaleAlways(u32 *out, u32 *src, int &width, int &height, int factor) { +void TextureScalerCommon::ScaleAlways(u32 *out, u32 *src, int width, int height, int *scaledWidth, int *scaledHeight, int factor) { if (IsEmptyOrFlat(src, width * height)) { // This means it was a flat texture. Vulkan wants the size up front, so we need to make it happen. u32 pixel = *src; - width *= factor; - height *= factor; + *scaledWidth = width * factor; + *scaledHeight = height * factor; + + size_t pixelCount = *scaledWidth * *scaledHeight; // ABCD. If A = D, and AB = CD, then they must all be equal (B = C, etc.) if ((pixel & 0x000000FF) == (pixel >> 24) && (pixel & 0x0000FFFF) == (pixel >> 16)) { - memset(out, pixel & 0xFF, width * height * sizeof(u32)); + memset(out, pixel & 0xFF, pixelCount * sizeof(u32)); } else { // Let's hope this is vectorized. - for (int i = 0; i < width * height; ++i) { + for (int i = 0; i < pixelCount; ++i) { out[i] = pixel; } } } else { - ScaleInto(out, src, width, height, factor); + ScaleInto(out, src, width, height, scaledWidth, scaledHeight, factor); } } -bool TextureScalerCommon::ScaleInto(u32 *outputBuf, u32 *src, int &width, int &height, int factor) { +bool TextureScalerCommon::ScaleInto(u32 *outputBuf, u32 *src, int width, int height, int *scaledWidth, int *scaledHeight, int factor) { #ifdef SCALING_MEASURE_TIME double t_start = time_now_d(); #endif @@ -660,21 +662,21 @@ bool TextureScalerCommon::ScaleInto(u32 *outputBuf, u32 *src, int &width, int &h } // update values accordingly - width *= factor; - height *= factor; + *scaledWidth = width * factor; + *scaledHeight = height * factor; #ifdef SCALING_MEASURE_TIME - if (width*height > 64 * 64 * factor*factor) { + if (*scaledWidth* *scaledHeight > 64 * 64 * factor*factor) { double t = time_now_d() - t_start; NOTICE_LOG(G3D, "TextureScaler: processed %9d pixels in %6.5lf seconds. (%9.2lf Mpixels/second)", - width*height, t, (width*height) / (t * 1000 * 1000)); + *scaledWidth * *scaledHeight, t, (*scaledWidth * *scaledHeight) / (t * 1000 * 1000)); } #endif return true; } -bool TextureScalerCommon::Scale(u32* &data, int &width, int &height, int factor) { +bool TextureScalerCommon::Scale(u32* &data, int width, int height, int *scaledWidth, int *scaledHeight, int factor) { // prevent processing empty or flat textures (this happens a lot in some games) // doesn't hurt the standard case, will be very quick for textures with actual texture if (IsEmptyOrFlat(data, width*height)) { @@ -685,7 +687,7 @@ bool TextureScalerCommon::Scale(u32* &data, int &width, int &height, int factor) bufOutput.resize(width * height * (factor * factor)); // used to store the upscaled image u32 *outputBuf = bufOutput.data(); - if (ScaleInto(outputBuf, data, width, height, factor)) { + if (ScaleInto(outputBuf, data, width, height, scaledWidth, scaledHeight, factor)) { data = outputBuf; return true; } diff --git a/GPU/Common/TextureScalerCommon.h b/GPU/Common/TextureScalerCommon.h index ee5b2f550c..a01457ae43 100644 --- a/GPU/Common/TextureScalerCommon.h +++ b/GPU/Common/TextureScalerCommon.h @@ -30,9 +30,9 @@ public: TextureScalerCommon(); ~TextureScalerCommon(); - void ScaleAlways(u32 *out, u32 *src, int &width, int &height, int factor); - bool Scale(u32 *&data, int &width, int &height, int factor); - bool ScaleInto(u32 *out, u32 *src, int &width, int &height, int factor); + void ScaleAlways(u32 *out, u32 *src, int width, int height, int *scaledWidth, int *scaledHeight, int factor); + bool Scale(u32 *&data, int width, int height, int *scaledWidth, int *scaledHeight, int factor); + bool ScaleInto(u32 *out, u32 *src, int width, int height, int *scaledWidth, int *scaledHeight, int factor); enum { XBRZ = 0, HYBRID = 1, BICUBIC = 2, HYBRID_BICUBIC = 3 }; diff --git a/GPU/Vulkan/TextureCacheVulkan.cpp b/GPU/Vulkan/TextureCacheVulkan.cpp index a08f231cd5..425905dd8f 100644 --- a/GPU/Vulkan/TextureCacheVulkan.cpp +++ b/GPU/Vulkan/TextureCacheVulkan.cpp @@ -754,7 +754,7 @@ void TextureCacheVulkan::LoadVulkanTextureLevel(TexCacheEntry &entry, uint8_t *w u32 fmt = dstFmt; // CPU scaling reads from the destination buffer so we want cached RAM. uint8_t *rearrange = (uint8_t *)AllocateAlignedMemory(w * scaleFactor * h * scaleFactor * 4, 16); - scaler_.ScaleAlways((u32 *)rearrange, pixelData, w, h, scaleFactor); + scaler_.ScaleAlways((u32 *)rearrange, pixelData, w, h, &w, &h, scaleFactor); pixelData = (u32 *)writePtr; // We always end up at 8888. Other parts assume this.