2016-01-03 23:09:37 +01:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include "ext/xxhash.h"
|
|
|
|
#include "i18n/i18n.h"
|
|
|
|
#include "math/math_util.h"
|
|
|
|
#include "profiler/profiler.h"
|
2017-02-04 18:46:12 +01:00
|
|
|
#include "thin3d/thin3d.h"
|
2016-01-03 23:09:37 +01:00
|
|
|
#include "Common/ColorConv.h"
|
|
|
|
#include "Core/Config.h"
|
|
|
|
#include "Core/Host.h"
|
|
|
|
#include "Core/MemMap.h"
|
|
|
|
#include "Core/Reporting.h"
|
|
|
|
#include "Core/System.h"
|
2016-03-17 11:56:43 +01:00
|
|
|
|
|
|
|
#include "Common/Vulkan/VulkanContext.h"
|
|
|
|
#include "Common/Vulkan/VulkanImage.h"
|
2016-03-20 09:35:10 +01:00
|
|
|
#include "Common/Vulkan/VulkanMemory.h"
|
2016-03-17 11:56:43 +01:00
|
|
|
|
2016-01-03 23:09:37 +01:00
|
|
|
#include "GPU/ge_constants.h"
|
|
|
|
#include "GPU/GPUState.h"
|
2016-01-03 00:46:41 +01:00
|
|
|
#include "GPU/Vulkan/TextureCacheVulkan.h"
|
2016-01-03 23:09:37 +01:00
|
|
|
#include "GPU/Vulkan/FramebufferVulkan.h"
|
|
|
|
#include "GPU/Vulkan/FragmentShaderGeneratorVulkan.h"
|
|
|
|
#include "GPU/Vulkan/DepalettizeShaderVulkan.h"
|
|
|
|
#include "GPU/Vulkan/ShaderManagerVulkan.h"
|
|
|
|
#include "GPU/Vulkan/DrawEngineVulkan.h"
|
|
|
|
#include "GPU/Common/TextureDecoder.h"
|
|
|
|
|
|
|
|
#ifdef _M_SSE
|
2017-03-12 17:16:38 +01:00
|
|
|
#include <emmintrin.h>
|
2016-01-03 23:09:37 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define TEXCACHE_NAME_CACHE_SIZE 16
|
|
|
|
|
|
|
|
#define TEXCACHE_MAX_TEXELS_SCALED (256*256) // Per frame
|
|
|
|
|
2016-03-26 18:22:21 -07:00
|
|
|
#define TEXCACHE_MIN_SLAB_SIZE (4 * 1024 * 1024)
|
|
|
|
#define TEXCACHE_MAX_SLAB_SIZE (32 * 1024 * 1024)
|
2016-01-03 23:09:37 +01:00
|
|
|
|
2016-03-26 13:17:17 -07:00
|
|
|
// Note: some drivers prefer B4G4R4A4_UNORM_PACK16 over R4G4B4A4_UNORM_PACK16.
|
|
|
|
#define VULKAN_4444_FORMAT VK_FORMAT_B4G4R4A4_UNORM_PACK16
|
|
|
|
#define VULKAN_1555_FORMAT VK_FORMAT_A1R5G5B5_UNORM_PACK16
|
|
|
|
#define VULKAN_565_FORMAT VK_FORMAT_B5G6R5_UNORM_PACK16
|
2016-01-09 21:19:18 +01:00
|
|
|
#define VULKAN_8888_FORMAT VK_FORMAT_R8G8B8A8_UNORM
|
|
|
|
|
2016-03-26 13:17:17 -07:00
|
|
|
static const VkComponentMapping VULKAN_4444_SWIZZLE = { VK_COMPONENT_SWIZZLE_A, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B };
|
|
|
|
static const VkComponentMapping VULKAN_1555_SWIZZLE = { VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_A };
|
|
|
|
static const VkComponentMapping VULKAN_565_SWIZZLE = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A };
|
|
|
|
static const VkComponentMapping VULKAN_8888_SWIZZLE = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A };
|
|
|
|
|
2017-02-08 18:07:34 +01:00
|
|
|
|
|
|
|
CachedTextureVulkan::~CachedTextureVulkan() {
|
|
|
|
delete texture_;
|
|
|
|
}
|
|
|
|
|
2016-01-09 01:23:32 +01:00
|
|
|
SamplerCache::~SamplerCache() {
|
2017-08-20 19:03:16 +02:00
|
|
|
DeviceLost();
|
2016-01-09 01:23:32 +01:00
|
|
|
}
|
2016-01-03 23:09:37 +01:00
|
|
|
|
2016-01-09 01:23:32 +01:00
|
|
|
VkSampler SamplerCache::GetOrCreateSampler(const SamplerCacheKey &key) {
|
2017-08-20 19:03:16 +02:00
|
|
|
VkSampler sampler = cache_.Get(key);
|
|
|
|
if (sampler != VK_NULL_HANDLE)
|
|
|
|
return sampler;
|
2016-01-09 01:23:32 +01:00
|
|
|
|
2016-03-15 00:37:14 +01:00
|
|
|
VkSamplerCreateInfo samp = { VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO };
|
2016-01-09 01:23:32 +01:00
|
|
|
samp.addressModeU = key.sClamp ? VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE : VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
|
|
|
samp.addressModeV = key.tClamp ? VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE : VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
2017-03-03 14:15:27 +01:00
|
|
|
samp.addressModeW = samp.addressModeU; // irrelevant, but Mali recommends that all clamp modes are the same if possible.
|
2016-01-09 01:23:32 +01:00
|
|
|
samp.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
|
2016-01-09 21:19:18 +01:00
|
|
|
samp.compareOp = VK_COMPARE_OP_ALWAYS;
|
2016-01-09 01:23:32 +01:00
|
|
|
samp.flags = 0;
|
2016-01-10 13:08:54 +01:00
|
|
|
samp.magFilter = key.magFilt ? VK_FILTER_LINEAR : VK_FILTER_NEAREST;
|
2016-03-15 00:37:14 +01:00
|
|
|
samp.minFilter = key.minFilt ? VK_FILTER_LINEAR : VK_FILTER_NEAREST;
|
|
|
|
samp.mipmapMode = key.mipFilt ? VK_SAMPLER_MIPMAP_MODE_LINEAR : VK_SAMPLER_MIPMAP_MODE_NEAREST;
|
|
|
|
|
2016-03-17 21:59:16 -07:00
|
|
|
if (gstate_c.Supports(GPU_SUPPORTS_ANISOTROPY) && g_Config.iAnisotropyLevel > 0) {
|
2016-03-17 21:56:04 -07:00
|
|
|
// Docs say the min of this value and the supported max are used.
|
|
|
|
samp.maxAnisotropy = 1 << g_Config.iAnisotropyLevel;
|
2016-03-15 00:37:14 +01:00
|
|
|
samp.anisotropyEnable = true;
|
2016-03-17 21:59:16 -07:00
|
|
|
} else {
|
|
|
|
samp.maxAnisotropy = 1.0f;
|
|
|
|
samp.anisotropyEnable = false;
|
2016-03-15 00:37:14 +01:00
|
|
|
}
|
2016-03-17 21:59:16 -07:00
|
|
|
|
2016-03-15 00:37:14 +01:00
|
|
|
samp.maxLod = key.maxLevel;
|
2016-01-09 01:23:32 +01:00
|
|
|
samp.minLod = 0.0f;
|
|
|
|
samp.mipLodBias = 0.0f;
|
|
|
|
|
|
|
|
VkResult res = vkCreateSampler(vulkan_->GetDevice(), &samp, nullptr, &sampler);
|
2016-01-09 21:19:18 +01:00
|
|
|
assert(res == VK_SUCCESS);
|
2017-08-20 19:03:16 +02:00
|
|
|
cache_.Insert(key, sampler);
|
2016-01-09 01:23:32 +01:00
|
|
|
return sampler;
|
|
|
|
}
|
|
|
|
|
2016-10-09 11:26:44 -07:00
|
|
|
void SamplerCache::DeviceLost() {
|
2017-08-20 19:03:16 +02:00
|
|
|
cache_.Iterate([&](const SamplerCacheKey &key, VkSampler sampler) {
|
|
|
|
vulkan_->Delete().QueueDeleteSampler(sampler);
|
|
|
|
});
|
|
|
|
cache_.Clear();
|
2016-10-09 11:26:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void SamplerCache::DeviceRestore(VulkanContext *vulkan) {
|
|
|
|
vulkan_ = vulkan;
|
|
|
|
}
|
|
|
|
|
2017-02-05 19:51:50 +01:00
|
|
|
TextureCacheVulkan::TextureCacheVulkan(Draw::DrawContext *draw, VulkanContext *vulkan)
|
|
|
|
: TextureCacheCommon(draw),
|
|
|
|
vulkan_(vulkan),
|
|
|
|
samplerCache_(vulkan),
|
|
|
|
texelsScaledThisFrame_(0) {
|
2016-01-03 23:09:37 +01:00
|
|
|
timesInvalidatedAllThisFrame_ = 0;
|
|
|
|
lastBoundTexture = nullptr;
|
2016-03-26 18:22:21 -07:00
|
|
|
allocator_ = new VulkanDeviceAllocator(vulkan_, TEXCACHE_MIN_SLAB_SIZE, TEXCACHE_MAX_SLAB_SIZE);
|
2016-01-03 23:09:37 +01:00
|
|
|
|
|
|
|
SetupTextureDecoder();
|
|
|
|
}
|
|
|
|
|
|
|
|
TextureCacheVulkan::~TextureCacheVulkan() {
|
|
|
|
Clear(true);
|
2016-05-01 17:35:26 -07:00
|
|
|
|
2017-10-09 12:17:54 +02:00
|
|
|
if (allocator_) {
|
|
|
|
allocator_->Destroy();
|
|
|
|
|
|
|
|
// We have to delete on queue, so this can free its queued deletions.
|
|
|
|
vulkan_->Delete().QueueCallback([](void *ptr) {
|
|
|
|
auto allocator = static_cast<VulkanDeviceAllocator *>(ptr);
|
|
|
|
delete allocator;
|
|
|
|
}, allocator_);
|
|
|
|
}
|
2016-01-03 23:09:37 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 15:58:46 +01:00
|
|
|
void TextureCacheVulkan::SetFramebufferManager(FramebufferManagerVulkan *fbManager) {
|
|
|
|
framebufferManagerVulkan_ = fbManager;
|
|
|
|
framebufferManager_ = fbManager;
|
|
|
|
}
|
|
|
|
|
2016-10-09 11:26:44 -07:00
|
|
|
void TextureCacheVulkan::DeviceLost() {
|
|
|
|
Clear(true);
|
|
|
|
|
|
|
|
if (allocator_) {
|
|
|
|
allocator_->Destroy();
|
|
|
|
|
|
|
|
// We have to delete on queue, so this can free its queued deletions.
|
|
|
|
vulkan_->Delete().QueueCallback([](void *ptr) {
|
|
|
|
auto allocator = static_cast<VulkanDeviceAllocator *>(ptr);
|
|
|
|
delete allocator;
|
|
|
|
}, allocator_);
|
|
|
|
allocator_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
samplerCache_.DeviceLost();
|
|
|
|
|
|
|
|
nextTexture_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCacheVulkan::DeviceRestore(VulkanContext *vulkan) {
|
|
|
|
vulkan_ = vulkan;
|
|
|
|
|
2017-10-09 12:17:54 +02:00
|
|
|
assert(!allocator_);
|
|
|
|
|
2016-10-09 11:26:44 -07:00
|
|
|
allocator_ = new VulkanDeviceAllocator(vulkan_, TEXCACHE_MIN_SLAB_SIZE, TEXCACHE_MAX_SLAB_SIZE);
|
|
|
|
samplerCache_.DeviceRestore(vulkan);
|
|
|
|
}
|
|
|
|
|
2017-02-23 17:31:24 +01:00
|
|
|
void TextureCacheVulkan::ReleaseTexture(TexCacheEntry *entry, bool delete_them) {
|
2017-02-19 22:31:07 +01:00
|
|
|
DEBUG_LOG(G3D, "Deleting texture %p", entry->vkTex);
|
|
|
|
delete entry->vkTex;
|
2017-02-23 09:25:33 +01:00
|
|
|
entry->vkTex = nullptr;
|
2016-01-03 23:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
VkFormat getClutDestFormatVulkan(GEPaletteFormat format) {
|
|
|
|
switch (format) {
|
|
|
|
case GE_CMODE_16BIT_ABGR4444:
|
2016-01-09 21:19:18 +01:00
|
|
|
return VULKAN_4444_FORMAT;
|
2016-01-03 23:09:37 +01:00
|
|
|
case GE_CMODE_16BIT_ABGR5551:
|
2016-01-09 21:19:18 +01:00
|
|
|
return VULKAN_1555_FORMAT;
|
2016-01-03 23:09:37 +01:00
|
|
|
case GE_CMODE_16BIT_BGR5650:
|
2016-01-09 21:19:18 +01:00
|
|
|
return VULKAN_565_FORMAT;
|
2016-01-03 23:09:37 +01:00
|
|
|
case GE_CMODE_32BIT_ABGR8888:
|
2016-01-09 21:19:18 +01:00
|
|
|
return VULKAN_8888_FORMAT;
|
2016-01-03 23:09:37 +01:00
|
|
|
}
|
|
|
|
return VK_FORMAT_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VkFilter MagFiltVK[2] = {
|
|
|
|
VK_FILTER_NEAREST,
|
|
|
|
VK_FILTER_LINEAR
|
|
|
|
};
|
|
|
|
|
2016-01-09 21:19:18 +01:00
|
|
|
void TextureCacheVulkan::UpdateSamplingParams(TexCacheEntry &entry, SamplerCacheKey &key) {
|
2016-01-03 23:09:37 +01:00
|
|
|
// TODO: Make GetSamplingParams write SamplerCacheKey directly
|
|
|
|
int minFilt;
|
|
|
|
int magFilt;
|
|
|
|
bool sClamp;
|
|
|
|
bool tClamp;
|
|
|
|
float lodBias;
|
2017-05-12 20:01:08 -07:00
|
|
|
bool autoMip;
|
2017-05-31 21:23:20 -07:00
|
|
|
u8 maxLevel = (entry.status & TexCacheEntry::STATUS_BAD_MIPS) ? 0 : entry.maxLevel;
|
|
|
|
GetSamplingParams(minFilt, magFilt, sClamp, tClamp, lodBias, maxLevel, entry.addr, autoMip);
|
2016-01-03 23:09:37 +01:00
|
|
|
key.minFilt = minFilt & 1;
|
2016-01-09 21:19:18 +01:00
|
|
|
key.mipEnable = (minFilt >> 2) & 1;
|
|
|
|
key.mipFilt = (minFilt >> 1) & 1;
|
2016-01-03 23:09:37 +01:00
|
|
|
key.magFilt = magFilt & 1;
|
|
|
|
key.sClamp = sClamp;
|
|
|
|
key.tClamp = tClamp;
|
2016-03-15 00:37:14 +01:00
|
|
|
key.maxLevel = entry.vkTex->texture_->GetNumMips() - 1;
|
2016-01-03 23:09:37 +01:00
|
|
|
/*
|
|
|
|
if (entry.maxLevel != 0) {
|
|
|
|
if (force || entry.lodBias != lodBias) {
|
|
|
|
if (gstate_c.Supports(GPU_SUPPORTS_TEXTURE_LOD_CONTROL)) {
|
2017-05-12 20:01:08 -07:00
|
|
|
if (autoMip) {
|
2016-01-03 23:09:37 +01:00
|
|
|
// TODO
|
2017-05-12 20:01:08 -07:00
|
|
|
} else {
|
2016-01-03 23:09:37 +01:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entry.lodBias = lodBias;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (entry.framebuffer) {
|
|
|
|
WARN_LOG_REPORT_ONCE(wrongFramebufAttach, G3D, "Framebuffer still attached in UpdateSamplingParams()?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCacheVulkan::SetFramebufferSamplingParams(u16 bufferWidth, u16 bufferHeight, SamplerCacheKey &key) {
|
|
|
|
int minFilt;
|
|
|
|
int magFilt;
|
|
|
|
bool sClamp;
|
|
|
|
bool tClamp;
|
|
|
|
float lodBias;
|
2017-05-12 20:01:08 -07:00
|
|
|
bool autoMip;
|
|
|
|
GetSamplingParams(minFilt, magFilt, sClamp, tClamp, lodBias, 0, 0, autoMip);
|
2016-01-03 23:09:37 +01:00
|
|
|
|
|
|
|
key.minFilt = minFilt & 1;
|
|
|
|
key.mipFilt = 0;
|
|
|
|
key.magFilt = magFilt & 1;
|
|
|
|
key.sClamp = sClamp;
|
|
|
|
key.tClamp = tClamp;
|
|
|
|
|
|
|
|
// Often the framebuffer will not match the texture size. We'll wrap/clamp in the shader in that case.
|
|
|
|
// This happens whether we have OES_texture_npot or not.
|
|
|
|
int w = gstate.getTextureWidth(0);
|
|
|
|
int h = gstate.getTextureHeight(0);
|
|
|
|
if (w != bufferWidth || h != bufferHeight) {
|
|
|
|
key.sClamp = true;
|
|
|
|
key.tClamp = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCacheVulkan::StartFrame() {
|
2017-03-25 11:34:21 -07:00
|
|
|
InvalidateLastTexture();
|
2016-01-03 23:09:37 +01:00
|
|
|
timesInvalidatedAllThisFrame_ = 0;
|
|
|
|
texelsScaledThisFrame_ = 0;
|
2016-03-27 08:33:22 -07:00
|
|
|
|
2016-01-03 23:09:37 +01:00
|
|
|
if (clearCacheNextFrame_) {
|
|
|
|
Clear(true);
|
|
|
|
clearCacheNextFrame_ = false;
|
|
|
|
} else {
|
|
|
|
Decimate();
|
|
|
|
}
|
2016-03-26 18:22:21 -07:00
|
|
|
|
|
|
|
allocator_->Begin();
|
2016-01-03 23:09:37 +01:00
|
|
|
}
|
|
|
|
|
2016-03-27 08:33:22 -07:00
|
|
|
void TextureCacheVulkan::EndFrame() {
|
|
|
|
allocator_->End();
|
|
|
|
|
|
|
|
if (texelsScaledThisFrame_) {
|
|
|
|
// INFO_LOG(G3D, "Scaled %i texels", texelsScaledThisFrame_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-03 23:09:37 +01:00
|
|
|
void TextureCacheVulkan::UpdateCurrentClut(GEPaletteFormat clutFormat, u32 clutBase, bool clutIndexIsSimple) {
|
|
|
|
const u32 clutBaseBytes = clutFormat == GE_CMODE_32BIT_ABGR8888 ? (clutBase * sizeof(u32)) : (clutBase * sizeof(u16));
|
|
|
|
// Technically, these extra bytes weren't loaded, but hopefully it was loaded earlier.
|
|
|
|
// If not, we're going to hash random data, which hopefully doesn't cause a performance issue.
|
|
|
|
//
|
|
|
|
// TODO: Actually, this seems like a hack. The game can upload part of a CLUT and reference other data.
|
|
|
|
// clutTotalBytes_ is the last amount uploaded. We should hash clutMaxBytes_, but this will often hash
|
|
|
|
// unrelated old entries for small palettes.
|
|
|
|
// Adding clutBaseBytes may just be mitigating this for some usage patterns.
|
|
|
|
const u32 clutExtendedBytes = std::min(clutTotalBytes_ + clutBaseBytes, clutMaxBytes_);
|
|
|
|
|
|
|
|
clutHash_ = DoReliableHash32((const char *)clutBufRaw_, clutExtendedBytes, 0xC0108888);
|
2016-03-26 13:17:17 -07:00
|
|
|
clutBuf_ = clutBufRaw_;
|
2016-01-03 23:09:37 +01:00
|
|
|
|
|
|
|
// Special optimization: fonts typically draw clut4 with just alpha values in a single color.
|
|
|
|
clutAlphaLinear_ = false;
|
|
|
|
clutAlphaLinearColor_ = 0;
|
|
|
|
if (clutFormat == GE_CMODE_16BIT_ABGR4444 && clutIndexIsSimple) {
|
|
|
|
const u16_le *clut = GetCurrentClut<u16_le>();
|
|
|
|
clutAlphaLinear_ = true;
|
2016-03-26 16:13:51 -07:00
|
|
|
clutAlphaLinearColor_ = clut[15] & 0x0FFF;
|
2016-01-03 23:09:37 +01:00
|
|
|
for (int i = 0; i < 16; ++i) {
|
2016-03-26 16:13:51 -07:00
|
|
|
u16 step = clutAlphaLinearColor_ | (i << 12);
|
2016-01-03 23:09:37 +01:00
|
|
|
if (clut[i] != step) {
|
|
|
|
clutAlphaLinear_ = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clutLastFormat_ = gstate.clutformat;
|
|
|
|
}
|
|
|
|
|
2017-02-19 23:07:00 +01:00
|
|
|
void TextureCacheVulkan::BindTexture(TexCacheEntry *entry) {
|
2017-02-19 23:19:55 +01:00
|
|
|
if (!entry || !entry->vkTex) {
|
|
|
|
imageView_ = VK_NULL_HANDLE;
|
|
|
|
sampler_ = VK_NULL_HANDLE;
|
|
|
|
return;
|
|
|
|
}
|
2017-02-19 23:07:00 +01:00
|
|
|
|
2017-02-19 23:19:55 +01:00
|
|
|
imageView_ = entry->vkTex->texture_->GetImageView();
|
|
|
|
SamplerCacheKey key;
|
|
|
|
UpdateSamplingParams(*entry, key);
|
|
|
|
sampler_ = samplerCache_.GetOrCreateSampler(key);
|
2017-02-19 23:07:00 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 15:43:53 +01:00
|
|
|
void TextureCacheVulkan::Unbind() {
|
2017-02-19 23:19:55 +01:00
|
|
|
imageView_ = VK_NULL_HANDLE;
|
|
|
|
sampler_ = VK_NULL_HANDLE;
|
2017-03-25 11:34:21 -07:00
|
|
|
InvalidateLastTexture();
|
2016-01-03 23:09:37 +01:00
|
|
|
}
|
|
|
|
|
2017-02-19 23:19:55 +01:00
|
|
|
void TextureCacheVulkan::ApplyTextureFramebuffer(TexCacheEntry *entry, VirtualFramebuffer *framebuffer) {
|
2016-01-05 21:18:43 +01:00
|
|
|
DepalShaderVulkan *depal = nullptr;
|
2016-01-03 23:09:37 +01:00
|
|
|
const GEPaletteFormat clutFormat = gstate.getClutPaletteFormat();
|
|
|
|
if ((entry->status & TexCacheEntry::STATUS_DEPALETTIZE) && !g_Config.bDisableSlowFramebufEffects) {
|
|
|
|
// depal = depalShaderCache_->GetDepalettizeShader(clutFormat, framebuffer->drawnFormat);
|
|
|
|
}
|
|
|
|
if (depal) {
|
|
|
|
// VulkanTexture *clutTexture = depalShaderCache_->GetClutTexture(clutFormat, clutHash_, clutBuf_);
|
2016-03-22 00:12:41 +01:00
|
|
|
// VulkanFBO *depalFBO = framebufferManager_->GetTempFBO(framebuffer->renderWidth, framebuffer->renderHeight, VK_FBO_8888);
|
2016-01-03 23:09:37 +01:00
|
|
|
|
2016-03-17 11:56:43 +01:00
|
|
|
//depalFBO->BeginPass(cmd);
|
2016-01-03 23:09:37 +01:00
|
|
|
|
|
|
|
struct Pos {
|
|
|
|
Pos(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {
|
|
|
|
}
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
float z;
|
|
|
|
};
|
|
|
|
struct UV {
|
|
|
|
UV(float u_, float v_) : u(u_), v(v_) {
|
|
|
|
}
|
|
|
|
float u;
|
|
|
|
float v;
|
|
|
|
};
|
|
|
|
|
|
|
|
Pos pos[4] = {
|
|
|
|
{ -1, -1, -1 },
|
|
|
|
{ 1, -1, -1 },
|
|
|
|
{ 1, 1, -1 },
|
|
|
|
{ -1, 1, -1 },
|
|
|
|
};
|
|
|
|
UV uv[4] = {
|
|
|
|
{ 0, 0 },
|
|
|
|
{ 1, 0 },
|
|
|
|
{ 1, 1 },
|
|
|
|
{ 0, 1 },
|
|
|
|
};
|
|
|
|
static const int indices[4] = { 0, 1, 3, 2 };
|
|
|
|
|
|
|
|
// If min is not < max, then we don't have values (wasn't set during decode.)
|
|
|
|
if (gstate_c.vertBounds.minV < gstate_c.vertBounds.maxV) {
|
|
|
|
const float invWidth = 1.0f / (float)framebuffer->bufferWidth;
|
|
|
|
const float invHeight = 1.0f / (float)framebuffer->bufferHeight;
|
|
|
|
// Inverse of half = double.
|
|
|
|
const float invHalfWidth = invWidth * 2.0f;
|
|
|
|
const float invHalfHeight = invHeight * 2.0f;
|
|
|
|
|
|
|
|
const int u1 = gstate_c.vertBounds.minU + gstate_c.curTextureXOffset;
|
|
|
|
const int v1 = gstate_c.vertBounds.minV + gstate_c.curTextureYOffset;
|
|
|
|
const int u2 = gstate_c.vertBounds.maxU + gstate_c.curTextureXOffset;
|
|
|
|
const int v2 = gstate_c.vertBounds.maxV + gstate_c.curTextureYOffset;
|
|
|
|
|
|
|
|
const float left = u1 * invHalfWidth - 1.0f;
|
|
|
|
const float right = u2 * invHalfWidth - 1.0f;
|
|
|
|
const float top = v1 * invHalfHeight - 1.0f;
|
|
|
|
const float bottom = v2 * invHalfHeight - 1.0f;
|
|
|
|
// Points are: BL, BR, TR, TL.
|
|
|
|
pos[0] = Pos(left, bottom, -1.0f);
|
|
|
|
pos[1] = Pos(right, bottom, -1.0f);
|
|
|
|
pos[2] = Pos(right, top, -1.0f);
|
|
|
|
pos[3] = Pos(left, top, -1.0f);
|
|
|
|
|
|
|
|
// And also the UVs, same order.
|
|
|
|
const float uvleft = u1 * invWidth;
|
|
|
|
const float uvright = u2 * invWidth;
|
|
|
|
const float uvtop = v1 * invHeight;
|
|
|
|
const float uvbottom = v2 * invHeight;
|
|
|
|
uv[0] = UV(uvleft, uvbottom);
|
|
|
|
uv[1] = UV(uvright, uvbottom);
|
|
|
|
uv[2] = UV(uvright, uvtop);
|
|
|
|
uv[3] = UV(uvleft, uvtop);
|
|
|
|
}
|
|
|
|
|
2017-02-19 23:07:00 +01:00
|
|
|
shaderManagerVulkan_->DirtyLastShader();
|
2016-01-03 23:09:37 +01:00
|
|
|
|
2016-03-17 11:56:43 +01:00
|
|
|
//depalFBO->EndPass(cmd);
|
|
|
|
//depalFBO->TransitionToTexture(cmd);
|
|
|
|
//imageView = depalFBO->GetColorImageView();
|
2016-05-21 15:40:58 -07:00
|
|
|
|
|
|
|
const u32 bytesPerColor = clutFormat == GE_CMODE_32BIT_ABGR8888 ? sizeof(u32) : sizeof(u16);
|
|
|
|
const u32 clutTotalColors = clutMaxBytes_ / bytesPerColor;
|
|
|
|
|
|
|
|
TexCacheEntry::Status alphaStatus = CheckAlpha(clutBuf_, getClutDestFormatVulkan(clutFormat), clutTotalColors, clutTotalColors, 1);
|
2017-04-03 17:04:58 +02:00
|
|
|
gstate_c.SetTextureFullAlpha(alphaStatus == TexCacheEntry::STATUS_ALPHA_FULL);
|
|
|
|
gstate_c.SetTextureSimpleAlpha(alphaStatus == TexCacheEntry::STATUS_ALPHA_SIMPLE);
|
2017-05-22 10:42:40 +02:00
|
|
|
|
|
|
|
// imageView_ = depalFbo->getImageView().
|
2016-05-21 15:40:58 -07:00
|
|
|
} else {
|
|
|
|
entry->status &= ~TexCacheEntry::STATUS_DEPALETTIZE;
|
|
|
|
|
2017-05-22 10:42:40 +02:00
|
|
|
imageView_ = framebufferManagerVulkan_->BindFramebufferAsColorTexture(0, framebuffer, BINDFBCOLOR_MAY_COPY_WITH_UV | BINDFBCOLOR_APPLY_TEX_OFFSET);
|
|
|
|
|
2017-04-03 17:04:58 +02:00
|
|
|
gstate_c.SetTextureFullAlpha(gstate.getTextureFormat() == GE_TFMT_5650);
|
|
|
|
gstate_c.SetTextureSimpleAlpha(gstate_c.textureFullAlpha);
|
2016-01-03 23:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SamplerCacheKey samplerKey;
|
|
|
|
SetFramebufferSamplingParams(framebuffer->bufferWidth, framebuffer->bufferHeight, samplerKey);
|
2017-05-22 10:42:40 +02:00
|
|
|
sampler_ = samplerCache_.GetOrCreateSampler(samplerKey);
|
2017-05-24 00:45:15 +02:00
|
|
|
InvalidateLastTexture(entry);
|
2016-01-03 23:09:37 +01:00
|
|
|
}
|
|
|
|
|
2016-04-30 13:44:31 -07:00
|
|
|
ReplacedTextureFormat FromVulkanFormat(VkFormat fmt) {
|
|
|
|
switch (fmt) {
|
2017-02-08 15:48:36 +01:00
|
|
|
case VULKAN_565_FORMAT: return ReplacedTextureFormat::F_5650;
|
|
|
|
case VULKAN_1555_FORMAT: return ReplacedTextureFormat::F_5551;
|
|
|
|
case VULKAN_4444_FORMAT: return ReplacedTextureFormat::F_4444;
|
|
|
|
case VULKAN_8888_FORMAT: default: return ReplacedTextureFormat::F_8888;
|
2016-04-30 13:44:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VkFormat ToVulkanFormat(ReplacedTextureFormat fmt) {
|
|
|
|
switch (fmt) {
|
2017-02-08 15:48:36 +01:00
|
|
|
case ReplacedTextureFormat::F_5650: return VULKAN_565_FORMAT;
|
|
|
|
case ReplacedTextureFormat::F_5551: return VULKAN_1555_FORMAT;
|
|
|
|
case ReplacedTextureFormat::F_4444: return VULKAN_4444_FORMAT;
|
|
|
|
case ReplacedTextureFormat::F_8888: default: return VULKAN_8888_FORMAT;
|
2016-04-30 13:44:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-19 23:25:09 +01:00
|
|
|
void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry, bool replaceImages) {
|
2016-05-01 17:27:14 -07:00
|
|
|
entry->status &= ~TexCacheEntry::STATUS_ALPHA_MASK;
|
|
|
|
|
2017-08-19 17:32:10 +02:00
|
|
|
VkCommandBuffer cmdInit = (VkCommandBuffer)draw_->GetNativeObject(Draw::NativeObject::INIT_COMMANDBUFFER);
|
2016-01-03 23:09:37 +01:00
|
|
|
// For the estimate, we assume cluts always point to 8888 for simplicity.
|
|
|
|
cacheSizeEstimate_ += EstimateTexMemoryUsage(entry);
|
|
|
|
|
|
|
|
if (entry->framebuffer) {
|
2016-05-21 17:53:42 -07:00
|
|
|
// Nothing else to do here.
|
2016-05-01 17:27:14 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((entry->bufw == 0 || (gstate.texbufwidth[0] & 0xf800) != 0) && entry->addr >= PSP_GetKernelMemoryEnd()) {
|
|
|
|
ERROR_LOG_REPORT(G3D, "Texture with unexpected bufw (full=%d)", gstate.texbufwidth[0] & 0xffff);
|
|
|
|
// Proceeding here can cause a crash.
|
2016-01-03 23:09:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust maxLevel to actually present levels..
|
|
|
|
bool badMipSizes = false;
|
2016-05-01 17:27:14 -07:00
|
|
|
int maxLevel = entry->maxLevel;
|
|
|
|
for (int i = 0; i <= maxLevel; i++) {
|
2016-01-03 23:09:37 +01:00
|
|
|
// If encountering levels pointing to nothing, adjust max level.
|
|
|
|
u32 levelTexaddr = gstate.getTextureAddress(i);
|
|
|
|
if (!Memory::IsValidAddress(levelTexaddr)) {
|
|
|
|
maxLevel = i - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-04-01 21:17:58 +02:00
|
|
|
// If size reaches 1, stop, and override maxlevel.
|
|
|
|
int tw = gstate.getTextureWidth(i);
|
|
|
|
int th = gstate.getTextureHeight(i);
|
|
|
|
if (tw == 1 || th == 1) {
|
|
|
|
maxLevel = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-03 23:09:37 +01:00
|
|
|
if (i > 0 && gstate_c.Supports(GPU_SUPPORTS_TEXTURE_LOD_CONTROL)) {
|
|
|
|
if (tw != 1 && tw != (gstate.getTextureWidth(i - 1) >> 1))
|
|
|
|
badMipSizes = true;
|
|
|
|
else if (th != 1 && th != (gstate.getTextureHeight(i - 1) >> 1))
|
|
|
|
badMipSizes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In addition, simply don't load more than level 0 if g_Config.bMipMap is false.
|
2017-05-26 10:16:43 +02:00
|
|
|
if (badMipSizes) {
|
2016-01-03 23:09:37 +01:00
|
|
|
maxLevel = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If GLES3 is available, we can preallocate the storage, which makes texture loading more efficient.
|
2016-05-01 17:27:14 -07:00
|
|
|
VkFormat dstFmt = GetDestFormat(GETextureFormat(entry->format), gstate.getClutPaletteFormat());
|
2016-01-03 23:09:37 +01:00
|
|
|
|
2016-03-26 11:44:22 -07:00
|
|
|
int scaleFactor = standardScaleFactor_;
|
2016-01-03 23:09:37 +01:00
|
|
|
|
|
|
|
// Rachet down scale factor in low-memory mode.
|
|
|
|
if (lowMemoryMode_) {
|
|
|
|
// Keep it even, though, just in case of npot troubles.
|
|
|
|
scaleFactor = scaleFactor > 4 ? 4 : (scaleFactor > 2 ? 2 : 1);
|
|
|
|
}
|
|
|
|
|
2017-02-20 00:19:58 +01:00
|
|
|
u64 cachekey = replacer_.Enabled() ? entry->CacheKey() : 0;
|
2016-05-01 17:27:14 -07:00
|
|
|
int w = gstate.getTextureWidth(0);
|
|
|
|
int h = gstate.getTextureHeight(0);
|
2017-02-20 00:19:58 +01:00
|
|
|
ReplacedTexture &replaced = replacer_.FindReplacement(cachekey, entry->fullhash, w, h);
|
2016-04-30 13:44:31 -07:00
|
|
|
if (replaced.GetSize(0, w, h)) {
|
|
|
|
// We're replacing, so we won't scale.
|
|
|
|
scaleFactor = 1;
|
2016-06-06 19:35:58 -07:00
|
|
|
entry->status |= TexCacheEntry::STATUS_IS_SCALED;
|
2017-05-26 10:16:43 +02:00
|
|
|
maxLevel = replaced.MaxLevel();
|
|
|
|
badMipSizes = false;
|
2016-04-30 13:44:31 -07:00
|
|
|
}
|
|
|
|
|
2016-01-03 23:09:37 +01:00
|
|
|
// Don't scale the PPGe texture.
|
2016-12-16 22:53:55 +02:00
|
|
|
if (entry->addr > 0x05000000 && entry->addr < PSP_GetKernelMemoryEnd())
|
2016-01-03 23:09:37 +01:00
|
|
|
scaleFactor = 1;
|
2016-05-06 19:58:01 -07:00
|
|
|
if ((entry->status & TexCacheEntry::STATUS_CHANGE_FREQUENT) != 0 && scaleFactor != 1) {
|
2016-01-03 23:09:37 +01:00
|
|
|
// Remember for later that we /wanted/ to scale this texture.
|
|
|
|
entry->status |= TexCacheEntry::STATUS_TO_SCALE;
|
|
|
|
scaleFactor = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scaleFactor != 1) {
|
|
|
|
if (texelsScaledThisFrame_ >= TEXCACHE_MAX_TEXELS_SCALED) {
|
|
|
|
entry->status |= TexCacheEntry::STATUS_TO_SCALE;
|
|
|
|
scaleFactor = 1;
|
|
|
|
} else {
|
|
|
|
entry->status &= ~TexCacheEntry::STATUS_TO_SCALE;
|
2016-06-06 19:35:58 -07:00
|
|
|
entry->status |= TexCacheEntry::STATUS_IS_SCALED;
|
2016-01-03 23:09:37 +01:00
|
|
|
texelsScaledThisFrame_ += w * h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 21:26:48 +01:00
|
|
|
// TODO
|
|
|
|
if (scaleFactor > 1) {
|
|
|
|
maxLevel = 0;
|
|
|
|
}
|
|
|
|
|
2016-03-26 14:28:03 -07:00
|
|
|
VkFormat actualFmt = scaleFactor > 1 ? VULKAN_8888_FORMAT : dstFmt;
|
2016-04-30 13:44:31 -07:00
|
|
|
if (replaced.Valid()) {
|
|
|
|
actualFmt = ToVulkanFormat(replaced.Format(0));
|
|
|
|
}
|
2016-04-09 10:30:23 +02:00
|
|
|
if (!entry->vkTex) {
|
2016-01-09 21:54:57 +01:00
|
|
|
entry->vkTex = new CachedTextureVulkan();
|
2016-03-26 18:22:21 -07:00
|
|
|
entry->vkTex->texture_ = new VulkanTexture(vulkan_, allocator_);
|
2016-01-09 21:54:57 +01:00
|
|
|
VulkanTexture *image = entry->vkTex->texture_;
|
2016-03-26 13:17:17 -07:00
|
|
|
|
|
|
|
const VkComponentMapping *mapping;
|
2016-03-26 14:28:03 -07:00
|
|
|
switch (actualFmt) {
|
2016-03-26 13:17:17 -07:00
|
|
|
case VULKAN_4444_FORMAT:
|
|
|
|
mapping = &VULKAN_4444_SWIZZLE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VULKAN_1555_FORMAT:
|
|
|
|
mapping = &VULKAN_1555_SWIZZLE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VULKAN_565_FORMAT:
|
|
|
|
mapping = &VULKAN_565_SWIZZLE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
mapping = &VULKAN_8888_SWIZZLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-08-19 17:32:10 +02:00
|
|
|
bool allocSuccess = image->CreateDirect(cmdInit, w * scaleFactor, h * scaleFactor, maxLevel + 1, actualFmt, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, mapping);
|
2016-03-26 18:41:53 -07:00
|
|
|
if (!allocSuccess && !lowMemoryMode_) {
|
|
|
|
WARN_LOG_REPORT(G3D, "Texture cache ran out of GPU memory; switching to low memory mode");
|
|
|
|
lowMemoryMode_ = true;
|
|
|
|
decimationCounter_ = 0;
|
|
|
|
Decimate();
|
|
|
|
// TODO: We should stall the GPU here and wipe things out of memory.
|
|
|
|
// As is, it will almost definitely fail the second time, but next frame it may recover.
|
|
|
|
|
|
|
|
I18NCategory *err = GetI18NCategory("Error");
|
|
|
|
if (scaleFactor > 1) {
|
2016-05-27 22:00:14 -07:00
|
|
|
host->NotifyUserMessage(err->T("Warning: Video memory FULL, reducing upscaling and switching to slow caching mode"), 2.0f);
|
2016-03-26 18:41:53 -07:00
|
|
|
} else {
|
2016-05-27 22:00:14 -07:00
|
|
|
host->NotifyUserMessage(err->T("Warning: Video memory FULL, switching to slow caching mode"), 2.0f);
|
2016-03-26 18:41:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
scaleFactor = 1;
|
|
|
|
actualFmt = dstFmt;
|
|
|
|
|
2017-08-19 17:32:10 +02:00
|
|
|
allocSuccess = image->CreateDirect(cmdInit, w * scaleFactor, h * scaleFactor, maxLevel + 1, actualFmt, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, mapping);
|
2016-03-26 18:41:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!allocSuccess) {
|
|
|
|
delete entry->vkTex;
|
|
|
|
entry->vkTex = nullptr;
|
|
|
|
}
|
2016-03-30 21:17:20 +02:00
|
|
|
} else {
|
2017-08-19 17:32:10 +02:00
|
|
|
entry->vkTex->texture_->TransitionForUpload(cmdInit);
|
2016-01-09 21:54:57 +01:00
|
|
|
}
|
|
|
|
lastBoundTexture = entry->vkTex;
|
2016-01-09 21:19:18 +01:00
|
|
|
|
2016-05-01 08:54:43 -07:00
|
|
|
ReplacedTextureDecodeInfo replacedInfo;
|
2017-02-20 00:19:58 +01:00
|
|
|
if (replacer_.Enabled() && !replaced.Valid()) {
|
2016-05-01 08:54:43 -07:00
|
|
|
replacedInfo.cachekey = cachekey;
|
|
|
|
replacedInfo.hash = entry->fullhash;
|
2016-05-01 17:27:14 -07:00
|
|
|
replacedInfo.addr = entry->addr;
|
|
|
|
replacedInfo.isVideo = videos_.find(entry->addr & 0x3FFFFFFF) != videos_.end();
|
2016-05-01 08:54:43 -07:00
|
|
|
replacedInfo.isFinal = (entry->status & TexCacheEntry::STATUS_TO_SCALE) == 0;
|
|
|
|
replacedInfo.scaleFactor = scaleFactor;
|
|
|
|
replacedInfo.fmt = FromVulkanFormat(actualFmt);
|
|
|
|
}
|
|
|
|
|
2016-03-26 18:41:53 -07:00
|
|
|
if (entry->vkTex) {
|
2017-05-31 21:42:07 -07:00
|
|
|
// NOTE: Since the level is not part of the cache key, we assume it never changes.
|
|
|
|
u8 level = std::max(0, gstate.getTexLevelOffset16() / 16);
|
2017-02-15 00:56:53 +09:00
|
|
|
bool fakeMipmap = IsFakeMipmapChange() && level > 0;
|
2016-03-26 18:41:53 -07:00
|
|
|
// Upload the texture data.
|
|
|
|
for (int i = 0; i <= maxLevel; i++) {
|
|
|
|
int mipWidth = gstate.getTextureWidth(i) * scaleFactor;
|
|
|
|
int mipHeight = gstate.getTextureHeight(i) * scaleFactor;
|
2016-04-30 13:44:31 -07:00
|
|
|
if (replaced.Valid()) {
|
|
|
|
replaced.GetSize(i, mipWidth, mipHeight);
|
|
|
|
}
|
2016-03-26 18:41:53 -07:00
|
|
|
int bpp = actualFmt == VULKAN_8888_FORMAT ? 4 : 2;
|
|
|
|
int stride = (mipWidth * bpp + 15) & ~15;
|
|
|
|
int size = stride * mipHeight;
|
|
|
|
uint32_t bufferOffset;
|
|
|
|
VkBuffer texBuf;
|
2017-02-19 23:07:00 +01:00
|
|
|
void *data = drawEngine_->GetPushBufferForTextureData()->Push(size, &bufferOffset, &texBuf);
|
2016-04-30 13:44:31 -07:00
|
|
|
if (replaced.Valid()) {
|
|
|
|
replaced.Load(i, data, stride);
|
|
|
|
} else {
|
2017-02-12 03:27:38 +09:00
|
|
|
if (fakeMipmap) {
|
|
|
|
LoadTextureLevel(*entry, (uint8_t *)data, stride, level, scaleFactor, dstFmt);
|
2017-08-19 17:32:10 +02:00
|
|
|
entry->vkTex->texture_->UploadMip(cmdInit, 0, mipWidth, mipHeight, texBuf, bufferOffset, stride / bpp);
|
2017-02-12 03:27:38 +09:00
|
|
|
break;
|
2017-05-17 02:21:03 +02:00
|
|
|
} else {
|
2017-02-12 03:27:38 +09:00
|
|
|
LoadTextureLevel(*entry, (uint8_t *)data, stride, i, scaleFactor, dstFmt);
|
2017-05-17 02:21:03 +02:00
|
|
|
}
|
2017-02-20 00:19:58 +01:00
|
|
|
if (replacer_.Enabled()) {
|
|
|
|
replacer_.NotifyTextureDecoded(replacedInfo, data, stride, i, mipWidth, mipHeight);
|
2016-04-30 13:44:31 -07:00
|
|
|
}
|
|
|
|
}
|
2017-08-19 17:32:10 +02:00
|
|
|
entry->vkTex->texture_->UploadMip(cmdInit, i, mipWidth, mipHeight, texBuf, bufferOffset, stride / bpp);
|
2016-03-26 18:41:53 -07:00
|
|
|
}
|
2016-04-30 13:44:31 -07:00
|
|
|
|
2017-05-31 21:23:20 -07:00
|
|
|
if (maxLevel == 0) {
|
|
|
|
entry->status |= TexCacheEntry::STATUS_BAD_MIPS;
|
|
|
|
} else {
|
|
|
|
entry->status &= ~TexCacheEntry::STATUS_BAD_MIPS;
|
|
|
|
}
|
2016-04-30 13:44:31 -07:00
|
|
|
if (replaced.Valid()) {
|
|
|
|
entry->SetAlphaStatus(TexCacheEntry::Status(replaced.AlphaStatus()));
|
|
|
|
}
|
2016-01-03 23:09:37 +01:00
|
|
|
}
|
2016-03-15 00:37:14 +01:00
|
|
|
|
2017-08-19 17:32:10 +02:00
|
|
|
entry->vkTex->texture_->EndCreate(cmdInit);
|
2016-03-30 21:17:20 +02:00
|
|
|
|
2017-04-03 17:04:58 +02:00
|
|
|
gstate_c.SetTextureFullAlpha(entry->GetAlphaStatus() == TexCacheEntry::STATUS_ALPHA_FULL);
|
|
|
|
gstate_c.SetTextureSimpleAlpha(entry->GetAlphaStatus() != TexCacheEntry::STATUS_ALPHA_UNKNOWN);
|
2016-01-03 23:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
VkFormat TextureCacheVulkan::GetDestFormat(GETextureFormat format, GEPaletteFormat clutFormat) const {
|
|
|
|
switch (format) {
|
|
|
|
case GE_TFMT_CLUT4:
|
|
|
|
case GE_TFMT_CLUT8:
|
|
|
|
case GE_TFMT_CLUT16:
|
|
|
|
case GE_TFMT_CLUT32:
|
|
|
|
return getClutDestFormatVulkan(clutFormat);
|
|
|
|
case GE_TFMT_4444:
|
2016-01-09 21:19:18 +01:00
|
|
|
return VULKAN_4444_FORMAT;
|
2016-01-03 23:09:37 +01:00
|
|
|
case GE_TFMT_5551:
|
2016-01-09 21:19:18 +01:00
|
|
|
return VULKAN_1555_FORMAT;
|
2016-01-03 23:09:37 +01:00
|
|
|
case GE_TFMT_5650:
|
2016-01-09 21:19:18 +01:00
|
|
|
return VULKAN_565_FORMAT;
|
2016-01-03 23:09:37 +01:00
|
|
|
case GE_TFMT_8888:
|
|
|
|
case GE_TFMT_DXT1:
|
|
|
|
case GE_TFMT_DXT3:
|
|
|
|
case GE_TFMT_DXT5:
|
|
|
|
default:
|
2016-01-09 23:27:53 +01:00
|
|
|
return VULKAN_8888_FORMAT;
|
2016-01-03 23:09:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-19 18:53:30 +01:00
|
|
|
TexCacheEntry::Status TextureCacheVulkan::CheckAlpha(const u32 *pixelData, VkFormat dstFmt, int stride, int w, int h) {
|
2016-01-03 23:09:37 +01:00
|
|
|
CheckAlphaResult res;
|
|
|
|
switch (dstFmt) {
|
2016-01-09 23:27:53 +01:00
|
|
|
case VULKAN_4444_FORMAT:
|
2016-03-26 13:17:17 -07:00
|
|
|
res = CheckAlphaRGBA4444Basic(pixelData, stride, w, h);
|
2016-01-03 23:09:37 +01:00
|
|
|
break;
|
2016-01-09 23:27:53 +01:00
|
|
|
case VULKAN_1555_FORMAT:
|
2016-03-26 13:17:17 -07:00
|
|
|
res = CheckAlphaRGBA5551Basic(pixelData, stride, w, h);
|
2016-01-03 23:09:37 +01:00
|
|
|
break;
|
2016-01-09 23:27:53 +01:00
|
|
|
case VULKAN_565_FORMAT:
|
2016-01-03 23:09:37 +01:00
|
|
|
// Never has any alpha.
|
|
|
|
res = CHECKALPHA_FULL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res = CheckAlphaRGBA8888Basic(pixelData, stride, w, h);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (TexCacheEntry::Status)res;
|
|
|
|
}
|
|
|
|
|
2016-04-09 10:30:23 +02:00
|
|
|
void TextureCacheVulkan::LoadTextureLevel(TexCacheEntry &entry, uint8_t *writePtr, int rowPitch, int level, int scaleFactor, VkFormat dstFmt) {
|
2016-01-09 21:19:18 +01:00
|
|
|
CachedTextureVulkan *tex = entry.vkTex;
|
2016-01-03 23:09:37 +01:00
|
|
|
int w = gstate.getTextureWidth(level);
|
|
|
|
int h = gstate.getTextureHeight(level);
|
2016-06-25 09:10:55 -07:00
|
|
|
|
2016-01-03 23:09:37 +01:00
|
|
|
{
|
|
|
|
PROFILE_THIS_SCOPE("decodetex");
|
|
|
|
|
2016-01-10 12:27:45 +01:00
|
|
|
GETextureFormat tfmt = (GETextureFormat)entry.format;
|
2016-01-03 23:09:37 +01:00
|
|
|
GEPaletteFormat clutformat = gstate.getClutPaletteFormat();
|
2016-03-22 19:00:57 +01:00
|
|
|
u32 texaddr = gstate.getTextureAddress(level);
|
|
|
|
int bufw = GetTextureBufw(level, texaddr, tfmt);
|
2016-03-26 13:33:18 -07:00
|
|
|
int bpp = dstFmt == VULKAN_8888_FORMAT ? 4 : 2;
|
|
|
|
|
2016-06-25 09:10:55 -07:00
|
|
|
u32 *pixelData = (u32 *)writePtr;
|
|
|
|
int decPitch = rowPitch;
|
2016-03-26 13:33:18 -07:00
|
|
|
if (scaleFactor > 1) {
|
2017-02-20 00:19:58 +01:00
|
|
|
tmpTexBufRearrange_.resize(std::max(bufw, w) * h);
|
|
|
|
pixelData = tmpTexBufRearrange_.data();
|
2016-03-26 14:28:03 -07:00
|
|
|
// We want to end up with a neatly packed texture for scaling.
|
|
|
|
decPitch = w * bpp;
|
2016-03-26 13:33:18 -07:00
|
|
|
}
|
|
|
|
|
2017-02-22 16:23:04 +01:00
|
|
|
DecodeTextureLevel((u8 *)pixelData, decPitch, tfmt, clutformat, texaddr, level, bufw, false, false, false);
|
2016-01-03 23:09:37 +01:00
|
|
|
gpuStats.numTexturesDecoded++;
|
|
|
|
|
|
|
|
if (scaleFactor > 1) {
|
|
|
|
u32 fmt = dstFmt;
|
2016-06-19 12:21:32 -07:00
|
|
|
scaler.ScaleAlways((u32 *)writePtr, pixelData, fmt, w, h, scaleFactor);
|
|
|
|
pixelData = (u32 *)writePtr;
|
2016-01-03 23:09:37 +01:00
|
|
|
dstFmt = (VkFormat)fmt;
|
2016-03-26 14:28:03 -07:00
|
|
|
|
2016-03-26 16:36:20 -07:00
|
|
|
// We always end up at 8888. Other parts assume this.
|
2016-03-26 14:28:03 -07:00
|
|
|
assert(dstFmt == VULKAN_8888_FORMAT);
|
2016-04-30 23:59:17 -07:00
|
|
|
bpp = sizeof(u32);
|
|
|
|
decPitch = w * bpp;
|
2016-06-19 12:21:32 -07:00
|
|
|
|
|
|
|
if (decPitch != rowPitch) {
|
|
|
|
// Rearrange in place to match the requested pitch.
|
|
|
|
// (it can only be larger than w * bpp, and a match is likely.)
|
|
|
|
for (int y = h - 1; y >= 0; --y) {
|
2016-06-25 09:10:55 -07:00
|
|
|
memcpy(writePtr + rowPitch * y, writePtr + decPitch * y, w * bpp);
|
2016-06-19 12:21:32 -07:00
|
|
|
}
|
2016-06-25 09:10:55 -07:00
|
|
|
decPitch = rowPitch;
|
2016-06-19 12:21:32 -07:00
|
|
|
}
|
2016-01-03 23:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((entry.status & TexCacheEntry::STATUS_CHANGE_FREQUENT) == 0) {
|
2016-04-30 23:59:17 -07:00
|
|
|
TexCacheEntry::Status alphaStatus = CheckAlpha(pixelData, dstFmt, decPitch / bpp, w, h);
|
2016-01-03 23:09:37 +01:00
|
|
|
entry.SetAlphaStatus(alphaStatus, level);
|
|
|
|
} else {
|
|
|
|
entry.SetAlphaStatus(TexCacheEntry::STATUS_ALPHA_UNKNOWN);
|
|
|
|
}
|
|
|
|
}
|
2016-01-03 00:46:41 +01:00
|
|
|
}
|
2017-10-18 13:03:49 +02:00
|
|
|
|
|
|
|
bool TextureCacheVulkan::GetCurrentTextureDebug(GPUDebugBuffer &buffer, int level) {
|
|
|
|
// TODO
|
|
|
|
return false;
|
|
|
|
}
|