CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/GPU/Vulkan/TextureCacheVulkan.cpp
Views: 1401
// Copyright (c) 2012- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include <algorithm>18#include <cstring>1920#include "ext/xxhash.h"2122#include "Common/File/VFS/VFS.h"23#include "Common/Data/Text/I18n.h"24#include "Common/LogReporting.h"25#include "Common/Math/math_util.h"26#include "Common/Profiler/Profiler.h"27#include "Common/GPU/thin3d.h"28#include "Common/GPU/Vulkan/VulkanRenderManager.h"29#include "Common/System/OSD.h"30#include "Common/Data/Convert/ColorConv.h"31#include "Common/StringUtils.h"32#include "Common/TimeUtil.h"33#include "Common/GPU/Vulkan/VulkanContext.h"34#include "Common/GPU/Vulkan/VulkanImage.h"35#include "Common/GPU/Vulkan/VulkanMemory.h"3637#include "Core/Config.h"38#include "Core/MemMap.h"39#include "Core/System.h"4041#include "GPU/ge_constants.h"42#include "GPU/GPUState.h"43#include "GPU/Common/TextureShaderCommon.h"44#include "GPU/Common/PostShader.h"45#include "GPU/Common/TextureCacheCommon.h"46#include "GPU/Common/TextureDecoder.h"47#include "GPU/Vulkan/VulkanContext.h"48#include "GPU/Vulkan/TextureCacheVulkan.h"49#include "GPU/Vulkan/FramebufferManagerVulkan.h"50#include "GPU/Vulkan/ShaderManagerVulkan.h"51#include "GPU/Vulkan/DrawEngineVulkan.h"5253using namespace PPSSPP_VK;5455#define TEXCACHE_MIN_SLAB_SIZE (8 * 1024 * 1024)56#define TEXCACHE_MAX_SLAB_SIZE (32 * 1024 * 1024)57#define TEXCACHE_SLAB_PRESSURE 45859const char *uploadShader = R"(60#version 45061#extension GL_ARB_separate_shader_objects : enable6263// 8x8 is the most common compute shader workgroup size, and works great on all major64// hardware vendors.65layout (local_size_x = 8, local_size_y = 8, local_size_z = 1) in;6667uniform layout(set = 0, binding = 0, rgba8) writeonly image2D img;6869layout(std430, set = 0, binding = 1) buffer Buf {70uint data[];71} buf;7273layout(push_constant) uniform Params {74int width;75int height;76} params;7778uint readColoru(uvec2 p) {79return buf.data[p.y * params.width + p.x];80}8182vec4 readColorf(uvec2 p) {83// Unpack the color (we could look it up in a CLUT here if we wanted...)84// The imageStore repack is free.85return unpackUnorm4x8(readColoru(p));86}8788void writeColorf(ivec2 p, vec4 c) {89imageStore(img, p, c);90}9192%s9394// Note that main runs once per INPUT pixel, unlike the old model.95void main() {96uvec2 xy = gl_GlobalInvocationID.xy;97// Kill off any out-of-image threads to avoid stray writes.98// Should only happen on the tiniest mipmaps as PSP textures are power-of-2,99// and we use a 8x8 workgroup size. Probably not really necessary.100if (xy.x >= params.width || xy.y >= params.height)101return;102// applyScaling will write the upscaled pixels, using writeColorf above.103// It's expected to write a square of scale*scale pixels, at the location xy*scale.104applyScaling(xy);105}106107)";108109static int VkFormatBytesPerPixel(VkFormat format) {110switch (format) {111case VULKAN_8888_FORMAT: return 4;112case VULKAN_CLUT8_FORMAT: return 1;113default: break;114}115return 2;116}117118SamplerCache::~SamplerCache() {119DeviceLost();120}121122VkSampler SamplerCache::GetOrCreateSampler(const SamplerCacheKey &key) {123VkSampler sampler;124if (cache_.Get(key, &sampler)) {125return sampler;126}127128VkSamplerCreateInfo samp = { VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO };129samp.addressModeU = key.sClamp ? VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE : VK_SAMPLER_ADDRESS_MODE_REPEAT;130samp.addressModeV = key.tClamp ? VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE : VK_SAMPLER_ADDRESS_MODE_REPEAT;131// W addressing is irrelevant for 2d textures, but Mali recommends that all clamp modes are the same if possible so just copy from U.132samp.addressModeW = key.texture3d ? VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE : samp.addressModeU;133samp.compareOp = VK_COMPARE_OP_ALWAYS;134samp.flags = 0;135samp.magFilter = key.magFilt ? VK_FILTER_LINEAR : VK_FILTER_NEAREST;136samp.minFilter = key.minFilt ? VK_FILTER_LINEAR : VK_FILTER_NEAREST;137samp.mipmapMode = key.mipFilt ? VK_SAMPLER_MIPMAP_MODE_LINEAR : VK_SAMPLER_MIPMAP_MODE_NEAREST;138139if (key.aniso) {140// Docs say the min of this value and the supported max are used.141samp.maxAnisotropy = 1 << g_Config.iAnisotropyLevel;142samp.anisotropyEnable = true;143} else {144samp.maxAnisotropy = 1.0f;145samp.anisotropyEnable = false;146}147if (key.maxLevel == 9 * 256) {148// No max level needed. Better for performance on some archs like ARM Mali.149samp.maxLod = VK_LOD_CLAMP_NONE;150} else {151samp.maxLod = (float)(int32_t)key.maxLevel * (1.0f / 256.0f);152}153samp.minLod = (float)(int32_t)key.minLevel * (1.0f / 256.0f);154samp.mipLodBias = (float)(int32_t)key.lodBias * (1.0f / 256.0f);155156VkResult res = vkCreateSampler(vulkan_->GetDevice(), &samp, nullptr, &sampler);157_assert_(res == VK_SUCCESS);158cache_.Insert(key, sampler);159return sampler;160}161162std::string SamplerCache::DebugGetSamplerString(const std::string &id, DebugShaderStringType stringType) {163SamplerCacheKey key;164key.FromString(id);165return StringFromFormat("%s/%s mag:%s min:%s mip:%s maxLod:%f minLod:%f bias:%f",166key.sClamp ? "Clamp" : "Wrap",167key.tClamp ? "Clamp" : "Wrap",168key.magFilt ? "Linear" : "Nearest",169key.minFilt ? "Linear" : "Nearest",170key.mipFilt ? "Linear" : "Nearest",171key.maxLevel / 256.0f,172key.minLevel / 256.0f,173key.lodBias / 256.0f);174}175176void SamplerCache::DeviceLost() {177cache_.Iterate([&](const SamplerCacheKey &key, VkSampler sampler) {178vulkan_->Delete().QueueDeleteSampler(sampler);179});180cache_.Clear();181vulkan_ = nullptr;182}183184void SamplerCache::DeviceRestore(VulkanContext *vulkan) {185vulkan_ = vulkan;186}187188std::vector<std::string> SamplerCache::DebugGetSamplerIDs() const {189std::vector<std::string> ids;190cache_.Iterate([&](const SamplerCacheKey &id, VkSampler sampler) {191std::string idstr;192id.ToString(&idstr);193ids.push_back(idstr);194});195return ids;196}197198TextureCacheVulkan::TextureCacheVulkan(Draw::DrawContext *draw, Draw2D *draw2D, VulkanContext *vulkan)199: TextureCacheCommon(draw, draw2D),200computeShaderManager_(vulkan),201samplerCache_(vulkan) {202DeviceRestore(draw);203}204205TextureCacheVulkan::~TextureCacheVulkan() {206DeviceLost();207}208209void TextureCacheVulkan::SetFramebufferManager(FramebufferManagerVulkan *fbManager) {210framebufferManager_ = fbManager;211}212213void TextureCacheVulkan::DeviceLost() {214textureShaderCache_->DeviceLost();215216VulkanContext *vulkan = draw_ ? (VulkanContext *)draw_->GetNativeObject(Draw::NativeObject::CONTEXT) : nullptr;217218Clear(true);219220samplerCache_.DeviceLost();221if (samplerNearest_)222vulkan->Delete().QueueDeleteSampler(samplerNearest_);223224if (uploadCS_ != VK_NULL_HANDLE)225vulkan->Delete().QueueDeleteShaderModule(uploadCS_);226227computeShaderManager_.DeviceLost();228229nextTexture_ = nullptr;230draw_ = nullptr;231Unbind();232}233234void TextureCacheVulkan::DeviceRestore(Draw::DrawContext *draw) {235VulkanContext *vulkan = (VulkanContext *)draw->GetNativeObject(Draw::NativeObject::CONTEXT);236draw_ = draw;237238_assert_(!allocator_);239240samplerCache_.DeviceRestore(vulkan);241textureShaderCache_->DeviceRestore(draw);242243VkSamplerCreateInfo samp{ VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO };244samp.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;245samp.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;246samp.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;247samp.magFilter = VK_FILTER_NEAREST;248samp.minFilter = VK_FILTER_NEAREST;249samp.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;250VkResult res = vkCreateSampler(vulkan->GetDevice(), &samp, nullptr, &samplerNearest_);251_assert_(res == VK_SUCCESS);252253CompileScalingShader();254255computeShaderManager_.DeviceRestore(draw);256}257258void TextureCacheVulkan::NotifyConfigChanged() {259TextureCacheCommon::NotifyConfigChanged();260CompileScalingShader();261}262263static std::string ReadShaderSrc(const Path &filename) {264size_t sz = 0;265char *data = (char *)g_VFS.ReadFile(filename.c_str(), &sz);266if (!data)267return std::string();268269std::string src(data, sz);270delete[] data;271return src;272}273274void TextureCacheVulkan::CompileScalingShader() {275VulkanContext *vulkan = (VulkanContext *)draw_->GetNativeObject(Draw::NativeObject::CONTEXT);276277if (!g_Config.bTexHardwareScaling || g_Config.sTextureShaderName != textureShader_) {278if (uploadCS_ != VK_NULL_HANDLE)279vulkan->Delete().QueueDeleteShaderModule(uploadCS_);280textureShader_.clear();281shaderScaleFactor_ = 0; // no texture scaling shader282} else if (uploadCS_) {283// No need to recreate.284return;285}286287if (!g_Config.bTexHardwareScaling)288return;289290ReloadAllPostShaderInfo(draw_);291const TextureShaderInfo *shaderInfo = GetTextureShaderInfo(g_Config.sTextureShaderName);292if (!shaderInfo || shaderInfo->computeShaderFile.empty())293return;294295std::string shaderSource = ReadShaderSrc(shaderInfo->computeShaderFile);296std::string fullUploadShader = StringFromFormat(uploadShader, shaderSource.c_str());297298std::string error;299uploadCS_ = CompileShaderModule(vulkan, VK_SHADER_STAGE_COMPUTE_BIT, fullUploadShader.c_str(), &error);300_dbg_assert_msg_(uploadCS_ != VK_NULL_HANDLE, "failed to compile upload shader");301302textureShader_ = g_Config.sTextureShaderName;303shaderScaleFactor_ = shaderInfo->scaleFactor;304}305306void TextureCacheVulkan::ReleaseTexture(TexCacheEntry *entry, bool delete_them) {307delete entry->vkTex;308entry->vkTex = nullptr;309}310311VkFormat getClutDestFormatVulkan(GEPaletteFormat format) {312switch (format) {313case GE_CMODE_16BIT_ABGR4444:314return VULKAN_4444_FORMAT;315case GE_CMODE_16BIT_ABGR5551:316return VULKAN_1555_FORMAT;317case GE_CMODE_16BIT_BGR5650:318return VULKAN_565_FORMAT;319case GE_CMODE_32BIT_ABGR8888:320return VULKAN_8888_FORMAT;321}322return VK_FORMAT_UNDEFINED;323}324325static const VkFilter MagFiltVK[2] = {326VK_FILTER_NEAREST,327VK_FILTER_LINEAR328};329330void TextureCacheVulkan::StartFrame() {331TextureCacheCommon::StartFrame();332// TODO: For low memory detection, maybe use some indication from VMA.333// Maybe see https://gpuopen-librariesandsdks.github.io/VulkanMemoryAllocator/html/staying_within_budget.html#staying_within_budget_querying_for_budget .334computeShaderManager_.BeginFrame();335}336337void TextureCacheVulkan::UpdateCurrentClut(GEPaletteFormat clutFormat, u32 clutBase, bool clutIndexIsSimple) {338const u32 clutBaseBytes = clutFormat == GE_CMODE_32BIT_ABGR8888 ? (clutBase * sizeof(u32)) : (clutBase * sizeof(u16));339// Technically, these extra bytes weren't loaded, but hopefully it was loaded earlier.340// If not, we're going to hash random data, which hopefully doesn't cause a performance issue.341//342// TODO: Actually, this seems like a hack. The game can upload part of a CLUT and reference other data.343// clutTotalBytes_ is the last amount uploaded. We should hash clutMaxBytes_, but this will often hash344// unrelated old entries for small palettes.345// Adding clutBaseBytes may just be mitigating this for some usage patterns.346const u32 clutExtendedBytes = std::min(clutTotalBytes_ + clutBaseBytes, clutMaxBytes_);347348if (replacer_.Enabled())349clutHash_ = XXH32((const char *)clutBufRaw_, clutExtendedBytes, 0xC0108888);350else351clutHash_ = XXH3_64bits((const char *)clutBufRaw_, clutExtendedBytes) & 0xFFFFFFFF;352clutBuf_ = clutBufRaw_;353354// Special optimization: fonts typically draw clut4 with just alpha values in a single color.355clutAlphaLinear_ = false;356clutAlphaLinearColor_ = 0;357if (clutFormat == GE_CMODE_16BIT_ABGR4444 && clutIndexIsSimple) {358const u16_le *clut = GetCurrentClut<u16_le>();359clutAlphaLinear_ = true;360clutAlphaLinearColor_ = clut[15] & 0x0FFF;361for (int i = 0; i < 16; ++i) {362u16 step = clutAlphaLinearColor_ | (i << 12);363if (clut[i] != step) {364clutAlphaLinear_ = false;365break;366}367}368}369370clutLastFormat_ = gstate.clutformat;371}372373void TextureCacheVulkan::BindTexture(TexCacheEntry *entry) {374if (!entry || !entry->vkTex) {375Unbind();376return;377}378379int maxLevel = (entry->status & TexCacheEntry::STATUS_NO_MIPS) ? 0 : entry->maxLevel;380SamplerCacheKey samplerKey = GetSamplingParams(maxLevel, entry);381curSampler_ = samplerCache_.GetOrCreateSampler(samplerKey);382imageView_ = entry->vkTex->GetImageView();383drawEngine_->SetDepalTexture(VK_NULL_HANDLE, false);384gstate_c.SetUseShaderDepal(ShaderDepalMode::OFF);385}386387void TextureCacheVulkan::ApplySamplingParams(const SamplerCacheKey &key) {388curSampler_ = samplerCache_.GetOrCreateSampler(key);389}390391void TextureCacheVulkan::Unbind() {392imageView_ = VK_NULL_HANDLE;393curSampler_ = VK_NULL_HANDLE;394}395396void TextureCacheVulkan::BindAsClutTexture(Draw::Texture *tex, bool smooth) {397VkImageView clutTexture = (VkImageView)draw_->GetNativeObject(Draw::NativeObject::TEXTURE_VIEW, tex);398drawEngine_->SetDepalTexture(clutTexture, smooth);399}400401static Draw::DataFormat FromVulkanFormat(VkFormat fmt) {402switch (fmt) {403case VULKAN_8888_FORMAT: default: return Draw::DataFormat::R8G8B8A8_UNORM;404}405}406407static VkFormat ToVulkanFormat(Draw::DataFormat fmt) {408switch (fmt) {409case Draw::DataFormat::BC1_RGBA_UNORM_BLOCK: return VK_FORMAT_BC1_RGBA_UNORM_BLOCK;410case Draw::DataFormat::BC2_UNORM_BLOCK: return VK_FORMAT_BC2_UNORM_BLOCK;411case Draw::DataFormat::BC3_UNORM_BLOCK: return VK_FORMAT_BC3_UNORM_BLOCK;412case Draw::DataFormat::BC4_UNORM_BLOCK: return VK_FORMAT_BC4_UNORM_BLOCK;413case Draw::DataFormat::BC5_UNORM_BLOCK: return VK_FORMAT_BC5_UNORM_BLOCK;414case Draw::DataFormat::BC7_UNORM_BLOCK: return VK_FORMAT_BC7_UNORM_BLOCK;415case Draw::DataFormat::ASTC_4x4_UNORM_BLOCK: return VK_FORMAT_ASTC_4x4_UNORM_BLOCK;416case Draw::DataFormat::ETC2_R8G8B8_UNORM_BLOCK: return VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;417case Draw::DataFormat::ETC2_R8G8B8A1_UNORM_BLOCK: return VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK;418case Draw::DataFormat::ETC2_R8G8B8A8_UNORM_BLOCK: return VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;419420case Draw::DataFormat::R8G8B8A8_UNORM: return VULKAN_8888_FORMAT;421default: _assert_msg_(false, "Bad texture pixel format"); return VULKAN_8888_FORMAT;422}423}424425void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) {426VulkanContext *vulkan = (VulkanContext *)draw_->GetNativeObject(Draw::NativeObject::CONTEXT);427428BuildTexturePlan plan;429plan.hardwareScaling = g_Config.bTexHardwareScaling && uploadCS_ != VK_NULL_HANDLE;430plan.slowScaler = !plan.hardwareScaling || vulkan->DevicePerfClass() == PerfClass::SLOW;431if (!PrepareBuildTexture(plan, entry)) {432// We're screwed (invalid size or something, corrupt display list), let's just zap it.433if (entry->vkTex) {434delete entry->vkTex;435entry->vkTex = nullptr;436}437return;438}439440VkFormat dstFmt = GetDestFormat(GETextureFormat(entry->format), gstate.getClutPaletteFormat());441442if (plan.scaleFactor > 1) {443_dbg_assert_(!plan.doReplace);444// Whether hardware or software scaling, this is the dest format.445dstFmt = VULKAN_8888_FORMAT;446} else if (plan.decodeToClut8) {447dstFmt = VULKAN_CLUT8_FORMAT;448}449450_dbg_assert_(plan.levelsToLoad <= plan.maxPossibleLevels);451452// We don't generate mipmaps for 512x512 textures because they're almost exclusively used for menu backgrounds453// and similar, which don't really need it.454// Also, if using replacements, check that we really can generate mips for this format - that's not possible for compressed ones.455if (g_Config.iTexFiltering == TEX_FILTER_AUTO_MAX_QUALITY && plan.w <= 256 && plan.h <= 256 && (!plan.doReplace || plan.replaced->Format() == Draw::DataFormat::R8G8B8A8_UNORM)) {456// Boost the number of mipmaps.457if (plan.maxPossibleLevels > plan.levelsToCreate) { // TODO: Should check against levelsToLoad, no?458// We have to generate mips with a shader. This requires decoding to R8G8B8A8_UNORM format to avoid extra complications.459dstFmt = VULKAN_8888_FORMAT;460}461plan.levelsToCreate = plan.maxPossibleLevels;462}463464_dbg_assert_(plan.levelsToCreate >= plan.levelsToLoad);465466// Any texture scaling is gonna move away from the original 16-bit format, if any.467VkFormat actualFmt = plan.scaleFactor > 1 ? VULKAN_8888_FORMAT : dstFmt;468bool bcFormat = false;469int bcAlign = 0;470if (plan.doReplace) {471Draw::DataFormat fmt = plan.replaced->Format();472bcFormat = Draw::DataFormatIsBlockCompressed(fmt, &bcAlign);473actualFmt = ToVulkanFormat(fmt);474}475476bool computeUpload = false;477VkCommandBuffer cmdInit = (VkCommandBuffer)draw_->GetNativeObject(Draw::NativeObject::INIT_COMMANDBUFFER);478479delete entry->vkTex;480481VkImageLayout imageLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;482VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;483484if (actualFmt == VULKAN_8888_FORMAT && plan.scaleFactor > 1 && plan.hardwareScaling) {485if (uploadCS_ != VK_NULL_HANDLE) {486computeUpload = true;487} else {488WARN_LOG(Log::G3D, "Falling back to software scaling, hardware shader didn't compile");489}490}491492if (computeUpload) {493usage |= VK_IMAGE_USAGE_STORAGE_BIT;494imageLayout = VK_IMAGE_LAYOUT_GENERAL;495}496497if (plan.saveTexture) {498INFO_LOG(Log::G3D, "About to save texture");499actualFmt = VULKAN_8888_FORMAT;500}501502const VkComponentMapping *mapping;503switch (actualFmt) {504case VULKAN_4444_FORMAT: mapping = &VULKAN_4444_SWIZZLE; break;505case VULKAN_1555_FORMAT: mapping = &VULKAN_1555_SWIZZLE; break;506case VULKAN_565_FORMAT: mapping = &VULKAN_565_SWIZZLE; break;507default: mapping = &VULKAN_8888_SWIZZLE; break; // no channel swizzle508}509510char texName[64];511snprintf(texName, sizeof(texName), "tex_%08x_%s_%s", entry->addr, GeTextureFormatToString((GETextureFormat)entry->format, gstate.getClutPaletteFormat()), gstate.isTextureSwizzled() ? "swz" : "lin");512entry->vkTex = new VulkanTexture(vulkan, texName);513VulkanTexture *image = entry->vkTex;514515VulkanBarrierBatch barrier;516bool allocSuccess = image->CreateDirect(plan.createW, plan.createH, plan.depth, plan.levelsToCreate, actualFmt, imageLayout, usage, &barrier, mapping);517barrier.Flush(cmdInit);518if (!allocSuccess && !lowMemoryMode_) {519WARN_LOG_REPORT(Log::G3D, "Texture cache ran out of GPU memory; switching to low memory mode");520lowMemoryMode_ = true;521decimationCounter_ = 0;522Decimate(entry, true);523524// TODO: We should stall the GPU here and wipe things out of memory.525// As is, it will almost definitely fail the second time, but next frame it may recover.526527auto err = GetI18NCategory(I18NCat::ERRORS);528if (plan.scaleFactor > 1) {529g_OSD.Show(OSDType::MESSAGE_WARNING, err->T("Warning: Video memory FULL, reducing upscaling and switching to slow caching mode"), 2.0f);530} else {531g_OSD.Show(OSDType::MESSAGE_WARNING, err->T("Warning: Video memory FULL, switching to slow caching mode"), 2.0f);532}533534// Turn off texture replacement for this texture.535plan.replaced = nullptr;536537plan.createW /= plan.scaleFactor;538plan.createH /= plan.scaleFactor;539plan.scaleFactor = 1;540actualFmt = dstFmt;541542allocSuccess = image->CreateDirect(plan.createW, plan.createH, plan.depth, plan.levelsToCreate, actualFmt, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, &barrier, mapping);543barrier.Flush(cmdInit);544}545546if (!allocSuccess) {547ERROR_LOG(Log::G3D, "Failed to create texture (%dx%d)", plan.w, plan.h);548delete entry->vkTex;549entry->vkTex = nullptr;550}551552if (!entry->vkTex) {553return;554}555556VK_PROFILE_BEGIN(vulkan, cmdInit, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,557"Texture Upload (%08x) video=%d", entry->addr, plan.isVideo);558559// Upload the texture data. We simply reuse the same loop for 3D texture slices instead of mips, if we have those.560int levels;561if (plan.depth > 1) {562levels = plan.depth;563} else {564levels = plan.levelsToLoad;565}566567VulkanPushPool *pushBuffer = drawEngine_->GetPushBufferForTextureData();568569// Batch the copies.570TextureCopyBatch copyBatch;571copyBatch.reserve(levels);572573for (int i = 0; i < levels; i++) {574int mipUnscaledWidth = gstate.getTextureWidth(i);575int mipUnscaledHeight = gstate.getTextureHeight(i);576577int mipWidth;578int mipHeight;579plan.GetMipSize(i, &mipWidth, &mipHeight);580581int bpp = VkFormatBytesPerPixel(actualFmt);582int optimalStrideAlignment = std::max(4, (int)vulkan->GetPhysicalDeviceProperties().properties.limits.optimalBufferCopyRowPitchAlignment);583int byteStride = RoundUpToPowerOf2(mipWidth * bpp, optimalStrideAlignment); // output stride584int pixelStride = byteStride / bpp;585int uploadSize = byteStride * mipHeight;586587uint32_t bufferOffset;588VkBuffer texBuf;589// NVIDIA reports a min alignment of 1 but that can't be healthy... let's align by 16 as a minimum.590int pushAlignment = std::max(16, (int)vulkan->GetPhysicalDeviceProperties().properties.limits.optimalBufferCopyOffsetAlignment);591void *data;592std::vector<uint8_t> saveData;593594// Simple wrapper to avoid reading back from VRAM (very, very expensive).595auto loadLevel = [&](int sz, int srcLevel, int lstride, int lfactor) {596if (plan.saveTexture) {597saveData.resize(sz);598data = &saveData[0];599} else {600data = pushBuffer->Allocate(sz, pushAlignment, &texBuf, &bufferOffset);601}602LoadVulkanTextureLevel(*entry, (uint8_t *)data, lstride, srcLevel, lfactor, actualFmt);603if (plan.saveTexture)604bufferOffset = pushBuffer->Push(&saveData[0], sz, pushAlignment, &texBuf);605};606607bool dataScaled = true;608if (plan.doReplace) {609int rowLength = pixelStride;610if (bcFormat) {611// For block compressed formats, we just set the upload size to the data size..612uploadSize = plan.replaced->GetLevelDataSizeAfterCopy(plan.baseLevelSrc + i);613rowLength = (mipWidth + 3) & ~3;614}615// Directly load the replaced image.616data = pushBuffer->Allocate(uploadSize, pushAlignment, &texBuf, &bufferOffset);617double replaceStart = time_now_d();618if (!plan.replaced->CopyLevelTo(plan.baseLevelSrc + i, (uint8_t *)data, uploadSize, byteStride)) { // If plan.doReplace, this shouldn't fail.619WARN_LOG(Log::G3D, "Failed to copy replaced texture level");620// TODO: Fill with some pattern?621}622replacementTimeThisFrame_ += time_now_d() - replaceStart;623entry->vkTex->CopyBufferToMipLevel(cmdInit, ©Batch, i, mipWidth, mipHeight, 0, texBuf, bufferOffset, rowLength);624} else {625if (plan.depth != 1) {626// 3D texturing.627loadLevel(uploadSize, i, byteStride, plan.scaleFactor);628entry->vkTex->CopyBufferToMipLevel(cmdInit, ©Batch, 0, mipWidth, mipHeight, i, texBuf, bufferOffset, pixelStride);629} else if (computeUpload) {630int srcBpp = VkFormatBytesPerPixel(dstFmt);631int srcStride = mipUnscaledWidth * srcBpp;632int srcSize = srcStride * mipUnscaledHeight;633loadLevel(srcSize, i == 0 ? plan.baseLevelSrc : i, srcStride, 1);634dataScaled = false;635636// This format can be used with storage images.637VkImageView view = entry->vkTex->CreateViewForMip(i);638VkDescriptorSet descSet = computeShaderManager_.GetDescriptorSet(view, texBuf, bufferOffset, srcSize);639struct Params { int x; int y; } params{ mipUnscaledWidth, mipUnscaledHeight };640VK_PROFILE_BEGIN(vulkan, cmdInit, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,641"Compute Upload: %dx%d->%dx%d", mipUnscaledWidth, mipUnscaledHeight, mipWidth, mipHeight);642vkCmdBindPipeline(cmdInit, VK_PIPELINE_BIND_POINT_COMPUTE, computeShaderManager_.GetPipeline(uploadCS_));643vkCmdBindDescriptorSets(cmdInit, VK_PIPELINE_BIND_POINT_COMPUTE, computeShaderManager_.GetPipelineLayout(), 0, 1, &descSet, 0, nullptr);644vkCmdPushConstants(cmdInit, computeShaderManager_.GetPipelineLayout(), VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(params), ¶ms);645vkCmdDispatch(cmdInit, (mipUnscaledWidth + 7) / 8, (mipUnscaledHeight + 7) / 8, 1);646VK_PROFILE_END(vulkan, cmdInit, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT);647vulkan->Delete().QueueDeleteImageView(view);648} else {649loadLevel(uploadSize, i == 0 ? plan.baseLevelSrc : i, byteStride, plan.scaleFactor);650entry->vkTex->CopyBufferToMipLevel(cmdInit, ©Batch, i, mipWidth, mipHeight, 0, texBuf, bufferOffset, pixelStride);651}652// Format might be wrong in lowMemoryMode_, so don't save.653if (plan.saveTexture && !lowMemoryMode_) {654INFO_LOG(Log::G3D, "Calling NotifyTextureDecoded %08x", entry->addr);655656// When hardware texture scaling is enabled, this saves the original.657int w = dataScaled ? mipWidth : mipUnscaledWidth;658int h = dataScaled ? mipHeight : mipUnscaledHeight;659// At this point, data should be saveData, and not slow.660ReplacedTextureDecodeInfo replacedInfo;661replacedInfo.cachekey = entry->CacheKey();662replacedInfo.hash = entry->fullhash;663replacedInfo.addr = entry->addr;664replacedInfo.isVideo = IsVideo(entry->addr);665replacedInfo.isFinal = (entry->status & TexCacheEntry::STATUS_TO_SCALE) == 0;666replacedInfo.fmt = FromVulkanFormat(actualFmt);667replacer_.NotifyTextureDecoded(plan.replaced, replacedInfo, data, byteStride, plan.baseLevelSrc + i, mipUnscaledWidth, mipUnscaledHeight, w, h);668}669}670}671672if (!copyBatch.empty()) {673VK_PROFILE_BEGIN(vulkan, cmdInit, VK_PIPELINE_STAGE_TRANSFER_BIT, "Copy Upload");674// Submit the whole batch of mip uploads.675entry->vkTex->FinishCopyBatch(cmdInit, ©Batch);676VK_PROFILE_END(vulkan, cmdInit, VK_PIPELINE_STAGE_TRANSFER_BIT);677}678679VkImageLayout layout = computeUpload ? VK_IMAGE_LAYOUT_GENERAL : VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;680VkPipelineStageFlags prevStage = computeUpload ? VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT : VK_PIPELINE_STAGE_TRANSFER_BIT;681682// Generate any additional mipmap levels.683// This will transition the whole stack to GENERAL if it wasn't already.684if (plan.levelsToLoad < plan.levelsToCreate) {685VK_PROFILE_BEGIN(vulkan, cmdInit, VK_PIPELINE_STAGE_TRANSFER_BIT, "Mipgen up to level %d", plan.levelsToCreate);686entry->vkTex->GenerateMips(cmdInit, plan.levelsToLoad, computeUpload);687layout = VK_IMAGE_LAYOUT_GENERAL;688prevStage = VK_PIPELINE_STAGE_TRANSFER_BIT;689VK_PROFILE_END(vulkan, cmdInit, VK_PIPELINE_STAGE_TRANSFER_BIT);690}691692entry->vkTex->EndCreate(cmdInit, false, prevStage, layout);693VK_PROFILE_END(vulkan, cmdInit, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);694695// Signal that we support depth textures so use it as one.696if (plan.depth > 1) {697entry->status |= TexCacheEntry::STATUS_3D;698}699700if (plan.doReplace) {701entry->SetAlphaStatus(TexCacheEntry::TexStatus(plan.replaced->AlphaStatus()));702}703}704705VkFormat TextureCacheVulkan::GetDestFormat(GETextureFormat format, GEPaletteFormat clutFormat) {706if (!gstate_c.Use(GPU_USE_16BIT_FORMATS)) {707return VK_FORMAT_R8G8B8A8_UNORM;708}709switch (format) {710case GE_TFMT_CLUT4:711case GE_TFMT_CLUT8:712case GE_TFMT_CLUT16:713case GE_TFMT_CLUT32:714return getClutDestFormatVulkan(clutFormat);715case GE_TFMT_4444:716return VULKAN_4444_FORMAT;717case GE_TFMT_5551:718return VULKAN_1555_FORMAT;719case GE_TFMT_5650:720return VULKAN_565_FORMAT;721case GE_TFMT_8888:722case GE_TFMT_DXT1:723case GE_TFMT_DXT3:724case GE_TFMT_DXT5:725default:726return VULKAN_8888_FORMAT;727}728}729730void TextureCacheVulkan::LoadVulkanTextureLevel(TexCacheEntry &entry, uint8_t *writePtr, int rowPitch, int level, int scaleFactor, VkFormat dstFmt) {731int w = gstate.getTextureWidth(level);732int h = gstate.getTextureHeight(level);733734GETextureFormat tfmt = (GETextureFormat)entry.format;735GEPaletteFormat clutformat = gstate.getClutPaletteFormat();736u32 texaddr = gstate.getTextureAddress(level);737738_assert_msg_(texaddr != 0, "Can't load a texture from address null")739740int bufw = GetTextureBufw(level, texaddr, tfmt);741int bpp = VkFormatBytesPerPixel(dstFmt);742743u32 *pixelData;744int decPitch;745746TexDecodeFlags texDecFlags{};747if (!gstate_c.Use(GPU_USE_16BIT_FORMATS) || scaleFactor > 1 || dstFmt == VULKAN_8888_FORMAT) {748texDecFlags |= TexDecodeFlags::EXPAND32;749}750if (entry.status & TexCacheEntry::STATUS_CLUT_GPU) {751texDecFlags |= TexDecodeFlags::TO_CLUT8;752}753754if (scaleFactor > 1) {755tmpTexBufRearrange_.resize(std::max(bufw, w) * h);756pixelData = tmpTexBufRearrange_.data();757// We want to end up with a neatly packed texture for scaling.758decPitch = w * bpp;759} else {760pixelData = (u32 *)writePtr;761decPitch = rowPitch;762}763764CheckAlphaResult alphaResult = DecodeTextureLevel((u8 *)pixelData, decPitch, tfmt, clutformat, texaddr, level, bufw, texDecFlags);765entry.SetAlphaStatus(alphaResult, level);766767if (scaleFactor > 1) {768u32 fmt = dstFmt;769// CPU scaling reads from the destination buffer so we want cached RAM.770uint8_t *rearrange = (uint8_t *)AllocateAlignedMemory(w * scaleFactor * h * scaleFactor * 4, 16);771scaler_.ScaleAlways((u32 *)rearrange, pixelData, w, h, &w, &h, scaleFactor);772pixelData = (u32 *)writePtr;773774// We always end up at 8888. Other parts assume this.775_assert_(dstFmt == VULKAN_8888_FORMAT);776bpp = sizeof(u32);777decPitch = w * bpp;778779if (decPitch != rowPitch) {780for (int y = 0; y < h; ++y) {781memcpy(writePtr + rowPitch * y, rearrange + decPitch * y, w * bpp);782}783decPitch = rowPitch;784} else {785memcpy(writePtr, rearrange, w * h * 4);786}787FreeAlignedMemory(rearrange);788}789}790791void TextureCacheVulkan::BoundFramebufferTexture() {792imageView_ = (VkImageView)draw_->GetNativeObject(Draw::NativeObject::BOUND_TEXTURE0_IMAGEVIEW);793}794795bool TextureCacheVulkan::GetCurrentTextureDebug(GPUDebugBuffer &buffer, int level, bool *isFramebuffer) {796SetTexture();797if (!nextTexture_) {798return GetCurrentFramebufferTextureDebug(buffer, isFramebuffer);799}800801// Apply texture may need to rebuild the texture if we're about to render, or bind a framebuffer.802TexCacheEntry *entry = nextTexture_;803ApplyTexture();804805if (!entry->vkTex)806return false;807808VulkanTexture *texture = entry->vkTex;809VulkanRenderManager *renderManager = (VulkanRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);810811GPUDebugBufferFormat bufferFormat;812Draw::DataFormat drawFormat;813switch (texture->GetFormat()) {814case VULKAN_565_FORMAT:815bufferFormat = GPU_DBG_FORMAT_565;816drawFormat = Draw::DataFormat::B5G6R5_UNORM_PACK16;817break;818case VULKAN_1555_FORMAT:819bufferFormat = GPU_DBG_FORMAT_5551;820drawFormat = Draw::DataFormat::B5G5R5A1_UNORM_PACK16;821break;822case VULKAN_4444_FORMAT:823bufferFormat = GPU_DBG_FORMAT_4444;824drawFormat = Draw::DataFormat::B4G4R4A4_UNORM_PACK16;825break;826case VULKAN_8888_FORMAT:827default:828bufferFormat = GPU_DBG_FORMAT_8888;829drawFormat = Draw::DataFormat::R8G8B8A8_UNORM;830break;831}832833int w = texture->GetWidth();834int h = texture->GetHeight();835if (level > 0) {836// In the future, maybe this could do something for 3D textures...837if (level >= texture->GetNumMips())838return false;839w >>= level;840h >>= level;841}842buffer.Allocate(w, h, bufferFormat);843844renderManager->CopyImageToMemorySync(texture->GetImage(), level, 0, 0, w, h, drawFormat, (uint8_t *)buffer.GetData(), w, "GetCurrentTextureDebug");845846// Vulkan requires us to re-apply all dynamic state for each command buffer, and the above will cause us to start a new cmdbuf.847// So let's dirty the things that are involved in Vulkan dynamic state. Readbacks are not frequent so this won't hurt other backends.848gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE);849framebufferManager_->RebindFramebuffer("RebindFramebuffer - GetCurrentTextureDebug");850*isFramebuffer = false;851return true;852}853854void TextureCacheVulkan::GetStats(char *ptr, size_t size) {855snprintf(ptr, size, "N/A");856}857858std::vector<std::string> TextureCacheVulkan::DebugGetSamplerIDs() const {859return samplerCache_.DebugGetSamplerIDs();860}861862std::string TextureCacheVulkan::DebugGetSamplerString(const std::string &id, DebugShaderStringType stringType) {863return samplerCache_.DebugGetSamplerString(id, stringType);864}865866void *TextureCacheVulkan::GetNativeTextureView(const TexCacheEntry *entry) {867VkImageView view = entry->vkTex->GetImageArrayView();868return (void *)view;869}870871872