From ed7dfdc8ad199c669d3ac059e52f32ae946b4a5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 23 Apr 2022 22:51:02 +0200 Subject: [PATCH 1/2] Fix mip level bug in Vulkan texture cache. --- GPU/Vulkan/TextureCacheVulkan.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/GPU/Vulkan/TextureCacheVulkan.cpp b/GPU/Vulkan/TextureCacheVulkan.cpp index d83f01a13b..58d20e970e 100644 --- a/GPU/Vulkan/TextureCacheVulkan.cpp +++ b/GPU/Vulkan/TextureCacheVulkan.cpp @@ -830,8 +830,8 @@ void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) { replaced.GetSize(i, mipWidth, mipHeight); } - int bpp = actualFmt == VULKAN_8888_FORMAT ? 4 : 2; - int stride = (mipWidth * bpp + 15) & ~15; + int bpp = actualFmt == VULKAN_8888_FORMAT ? 4 : 2; // output bpp + int stride = (mipWidth * bpp + 15) & ~15; // output stride int size = stride * mipHeight; uint32_t bufferOffset; VkBuffer texBuf; @@ -840,7 +840,7 @@ void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) { void *data; std::vector saveData; - auto loadLevel = [&](int sz, int lstride, int lfactor) { + auto loadLevel = [&](int sz, int level, int lstride, int lfactor) { if (willSaveTex) { saveData.resize(sz); data = &saveData[0]; @@ -865,14 +865,14 @@ void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) { VK_PROFILE_END(vulkan, cmdInit, VK_PIPELINE_STAGE_TRANSFER_BIT); } else { if (fakeMipmap) { - loadLevel(size, stride, scaleFactor); + loadLevel(size, i, stride, scaleFactor); entry->vkTex->UploadMip(cmdInit, 0, mipWidth, mipHeight, texBuf, bufferOffset, stride / bpp); } else { if (computeUpload) { int srcBpp = dstFmt == VULKAN_8888_FORMAT ? 4 : 2; int srcStride = mipUnscaledWidth * srcBpp; int srcSize = srcStride * mipUnscaledHeight; - loadLevel(srcSize, srcStride, 1); + loadLevel(srcSize, i, srcStride, 1); dataScaled = false; // This format can be used with storage images. @@ -888,7 +888,7 @@ void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) { VK_PROFILE_END(vulkan, cmdInit, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT); vulkan->Delete().QueueDeleteImageView(view); } else { - loadLevel(size, stride, scaleFactor); + loadLevel(size, i, stride, scaleFactor); VK_PROFILE_BEGIN(vulkan, cmdInit, VK_PIPELINE_STAGE_TRANSFER_BIT, "Copy Upload: %dx%d", mipWidth, mipHeight); entry->vkTex->UploadMip(cmdInit, i, mipWidth, mipHeight, texBuf, bufferOffset, stride / bpp); @@ -990,6 +990,7 @@ void TextureCacheVulkan::LoadTextureLevel(TexCacheEntry &entry, uint8_t *writePt u32 *pixelData = (u32 *)writePtr; int decPitch = rowPitch; + if (scaleFactor > 1) { tmpTexBufRearrange_.resize(std::max(bufw, w) * h); pixelData = tmpTexBufRearrange_.data(); From 73452b4743f603e96084e77221894bb21ccda9f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 23 Apr 2022 22:52:28 +0200 Subject: [PATCH 2/2] Minor cleanups --- Common/DbgNew.h | 2 ++ Common/GPU/Vulkan/VulkanImage.h | 2 +- Common/MemoryUtil.cpp | 2 +- Core/TextureReplacer.cpp | 3 +-- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Common/DbgNew.h b/Common/DbgNew.h index 831f7d42cd..2837cdf51b 100644 --- a/Common/DbgNew.h +++ b/Common/DbgNew.h @@ -3,6 +3,8 @@ // Utility file for using the MS CRT's memory tracking. // crtdbg.h overloads malloc with malloc_dbg etc, but does not catch new. So here we go. +// To add a full check of memory overruns, throw in a _CrtCheckMemory(). Useful to narrow things down. + #include #if defined(_DEBUG) diff --git a/Common/GPU/Vulkan/VulkanImage.h b/Common/GPU/Vulkan/VulkanImage.h index adbd167cf5..75f09a89ae 100644 --- a/Common/GPU/Vulkan/VulkanImage.h +++ b/Common/GPU/Vulkan/VulkanImage.h @@ -60,7 +60,7 @@ private: VulkanContext *vulkan_; VkImage image_ = VK_NULL_HANDLE; VkImageView view_ = VK_NULL_HANDLE; - VmaAllocation allocation_; + VmaAllocation allocation_ = VK_NULL_HANDLE; int32_t width_ = 0; int32_t height_ = 0; diff --git a/Common/MemoryUtil.cpp b/Common/MemoryUtil.cpp index 1e4a479e05..28464006a2 100644 --- a/Common/MemoryUtil.cpp +++ b/Common/MemoryUtil.cpp @@ -236,7 +236,7 @@ void *AllocateMemoryPages(size_t size, uint32_t memProtFlags) { void *AllocateAlignedMemory(size_t size, size_t alignment) { #ifdef _WIN32 - void* ptr = _aligned_malloc(size,alignment); + void* ptr = _aligned_malloc(size, alignment); #else void* ptr = NULL; #ifdef __ANDROID__ diff --git a/Core/TextureReplacer.cpp b/Core/TextureReplacer.cpp index 0cb06d0ed0..17ac72a155 100644 --- a/Core/TextureReplacer.cpp +++ b/Core/TextureReplacer.cpp @@ -549,8 +549,7 @@ public: } } - png_image png; - memset(&png, 0, sizeof(png)); + png_image png{}; png.version = PNG_IMAGE_VERSION; png.format = PNG_FORMAT_RGBA; png.width = w;