2016-08-13 12:57:50 +00:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2016-10-01 03:07:50 +00:00
|
|
|
#include "VideoBackends/Vulkan/ObjectCache.h"
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
#include <algorithm>
|
2019-06-01 11:55:09 +00:00
|
|
|
#include <array>
|
2016-08-13 12:57:50 +00:00
|
|
|
#include <type_traits>
|
|
|
|
|
2017-02-01 04:29:29 +00:00
|
|
|
#include "Common/Assert.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "Common/CommonFuncs.h"
|
2020-11-04 19:59:34 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "Common/LinearDiskCache.h"
|
2017-02-01 04:29:29 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
|
2017-07-20 05:25:35 +00:00
|
|
|
#include "VideoBackends/Vulkan/CommandBufferManager.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "VideoBackends/Vulkan/ShaderCompiler.h"
|
2020-09-15 12:43:41 +00:00
|
|
|
#include "VideoBackends/Vulkan/VKStreamBuffer.h"
|
2019-02-15 01:59:50 +00:00
|
|
|
#include "VideoBackends/Vulkan/VKTexture.h"
|
2020-09-15 12:43:41 +00:00
|
|
|
#include "VideoBackends/Vulkan/VKVertexFormat.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "VideoBackends/Vulkan/VulkanContext.h"
|
2023-02-10 06:28:06 +00:00
|
|
|
#include "VideoCommon/Constants.h"
|
2019-12-22 18:40:40 +00:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
namespace Vulkan
|
|
|
|
{
|
|
|
|
std::unique_ptr<ObjectCache> g_object_cache;
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
ObjectCache::ObjectCache() = default;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
ObjectCache::~ObjectCache()
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
DestroyPipelineCache();
|
2016-08-13 12:57:50 +00:00
|
|
|
DestroySamplers();
|
|
|
|
DestroyPipelineLayouts();
|
|
|
|
DestroyDescriptorSetLayouts();
|
2017-09-09 04:04:26 +00:00
|
|
|
DestroyRenderPassCache();
|
2019-02-15 01:59:50 +00:00
|
|
|
m_dummy_texture.reset();
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ObjectCache::Initialize()
|
|
|
|
{
|
|
|
|
if (!CreateDescriptorSetLayouts())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!CreatePipelineLayouts())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!CreateStaticSamplers())
|
|
|
|
return false;
|
|
|
|
|
2019-01-18 14:27:09 +00:00
|
|
|
m_texture_upload_buffer =
|
2019-02-15 01:59:50 +00:00
|
|
|
StreamBuffer::Create(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, TEXTURE_UPLOAD_BUFFER_SIZE);
|
2019-01-18 14:27:09 +00:00
|
|
|
if (!m_texture_upload_buffer)
|
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to create texture upload buffer");
|
2019-01-18 14:27:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
if (g_ActiveConfig.bShaderCache)
|
|
|
|
{
|
|
|
|
if (!LoadPipelineCache())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!CreatePipelineCache())
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-20 05:25:35 +00:00
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
void ObjectCache::Shutdown()
|
|
|
|
{
|
|
|
|
if (g_ActiveConfig.bShaderCache && m_pipeline_cache != VK_NULL_HANDLE)
|
|
|
|
SavePipelineCache();
|
|
|
|
}
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
void ObjectCache::ClearSamplerCache()
|
|
|
|
{
|
|
|
|
for (const auto& it : m_sampler_cache)
|
|
|
|
{
|
|
|
|
if (it.second != VK_NULL_HANDLE)
|
|
|
|
vkDestroySampler(g_vulkan_context->GetDevice(), it.second, nullptr);
|
|
|
|
}
|
|
|
|
m_sampler_cache.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectCache::DestroySamplers()
|
|
|
|
{
|
|
|
|
ClearSamplerCache();
|
|
|
|
|
|
|
|
if (m_point_sampler != VK_NULL_HANDLE)
|
|
|
|
{
|
|
|
|
vkDestroySampler(g_vulkan_context->GetDevice(), m_point_sampler, nullptr);
|
|
|
|
m_point_sampler = VK_NULL_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_linear_sampler != VK_NULL_HANDLE)
|
|
|
|
{
|
|
|
|
vkDestroySampler(g_vulkan_context->GetDevice(), m_linear_sampler, nullptr);
|
|
|
|
m_linear_sampler = VK_NULL_HANDLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObjectCache::CreateDescriptorSetLayouts()
|
|
|
|
{
|
2018-02-24 15:15:35 +00:00
|
|
|
// The geometry shader buffer must be last in this binding set, as we don't include it
|
|
|
|
// if geometry shaders are not supported by the device. See the decrement below.
|
2023-09-20 00:29:38 +00:00
|
|
|
static const std::array<VkDescriptorSetLayoutBinding, 4> standard_ubo_bindings{{
|
2016-08-13 12:57:50 +00:00
|
|
|
{UBO_DESCRIPTOR_SET_BINDING_PS, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1,
|
|
|
|
VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{UBO_DESCRIPTOR_SET_BINDING_VS, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1,
|
|
|
|
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT},
|
2023-09-20 00:29:38 +00:00
|
|
|
{UBO_DESCRIPTOR_SET_BINDING_PS_CUST, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1,
|
|
|
|
VK_SHADER_STAGE_FRAGMENT_BIT},
|
2016-08-13 12:57:50 +00:00
|
|
|
{UBO_DESCRIPTOR_SET_BINDING_GS, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1,
|
2019-06-01 11:55:09 +00:00
|
|
|
VK_SHADER_STAGE_GEOMETRY_BIT},
|
|
|
|
}};
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2023-12-09 21:07:35 +00:00
|
|
|
constexpr u32 MAX_PIXEL_SAMPLER_ARRAY_SIZE = 8;
|
|
|
|
constexpr u32 TOTAL_PIXEL_SAMPLER_BINDINGS =
|
|
|
|
1 + (VideoCommon::MAX_PIXEL_SHADER_SAMPLERS - MAX_PIXEL_SAMPLER_ARRAY_SIZE);
|
|
|
|
static_assert(VideoCommon::MAX_PIXEL_SHADER_SAMPLERS == 16, "Update descriptor sampler bindings");
|
|
|
|
|
|
|
|
static const std::array<VkDescriptorSetLayoutBinding, TOTAL_PIXEL_SAMPLER_BINDINGS>
|
|
|
|
standard_sampler_bindings{{
|
|
|
|
{0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, MAX_PIXEL_SAMPLER_ARRAY_SIZE,
|
|
|
|
VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{8, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{9, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{10, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{11, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{12, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{13, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{14, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{15, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
}};
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2022-06-19 03:49:03 +00:00
|
|
|
// The dynamic veretex loader's vertex buffer must be last here, for similar reasons
|
|
|
|
static const std::array<VkDescriptorSetLayoutBinding, 2> standard_ssbo_bindings{{
|
2019-06-01 11:55:09 +00:00
|
|
|
{0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
2022-06-19 03:49:03 +00:00
|
|
|
{1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT},
|
2019-06-01 11:55:09 +00:00
|
|
|
}};
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2019-06-01 11:55:09 +00:00
|
|
|
static const std::array<VkDescriptorSetLayoutBinding, 1> utility_ubo_bindings{{
|
|
|
|
{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1,
|
|
|
|
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT | VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
}};
|
2019-02-15 01:59:50 +00:00
|
|
|
|
|
|
|
// Utility samplers aren't dynamically indexed.
|
2019-06-01 11:55:09 +00:00
|
|
|
static const std::array<VkDescriptorSetLayoutBinding, 9> utility_sampler_bindings{{
|
2019-02-15 01:59:50 +00:00
|
|
|
{0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{2, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{3, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{4, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{5, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{6, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{7, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
|
|
|
{8, VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1, VK_SHADER_STAGE_FRAGMENT_BIT},
|
2019-06-01 11:55:09 +00:00
|
|
|
}};
|
2016-11-13 05:38:32 +00:00
|
|
|
|
2023-06-10 17:35:36 +00:00
|
|
|
static const std::array<VkDescriptorSetLayoutBinding, 19> compute_set_bindings{{
|
2016-12-09 12:23:04 +00:00
|
|
|
{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{2, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
2023-06-10 17:35:36 +00:00
|
|
|
{3, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{4, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{5, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{6, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{7, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{8, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{9, VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{10, VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{11, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{12, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{13, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{14, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{15, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{16, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{17, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
|
|
|
{18, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, VK_SHADER_STAGE_COMPUTE_BIT},
|
2019-06-01 11:55:09 +00:00
|
|
|
}};
|
2016-12-09 12:23:04 +00:00
|
|
|
|
2023-09-20 00:29:38 +00:00
|
|
|
std::array<VkDescriptorSetLayoutBinding, 4> ubo_bindings = standard_ubo_bindings;
|
2022-07-24 08:32:59 +00:00
|
|
|
|
2019-06-01 11:55:09 +00:00
|
|
|
std::array<VkDescriptorSetLayoutCreateInfo, NUM_DESCRIPTOR_SET_LAYOUTS> create_infos{{
|
2016-08-13 12:57:50 +00:00
|
|
|
{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, nullptr, 0,
|
2022-07-24 08:32:59 +00:00
|
|
|
static_cast<u32>(ubo_bindings.size()), ubo_bindings.data()},
|
2017-09-08 09:42:56 +00:00
|
|
|
{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, nullptr, 0,
|
2019-06-01 11:55:09 +00:00
|
|
|
static_cast<u32>(standard_sampler_bindings.size()), standard_sampler_bindings.data()},
|
2016-08-13 12:57:50 +00:00
|
|
|
{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, nullptr, 0,
|
2019-06-01 11:55:09 +00:00
|
|
|
static_cast<u32>(standard_ssbo_bindings.size()), standard_ssbo_bindings.data()},
|
2016-08-13 12:57:50 +00:00
|
|
|
{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, nullptr, 0,
|
2019-06-01 11:55:09 +00:00
|
|
|
static_cast<u32>(utility_ubo_bindings.size()), utility_ubo_bindings.data()},
|
2016-11-13 05:38:32 +00:00
|
|
|
{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, nullptr, 0,
|
2019-06-01 11:55:09 +00:00
|
|
|
static_cast<u32>(utility_sampler_bindings.size()), utility_sampler_bindings.data()},
|
2016-12-09 12:23:04 +00:00
|
|
|
{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, nullptr, 0,
|
2019-06-01 11:55:09 +00:00
|
|
|
static_cast<u32>(compute_set_bindings.size()), compute_set_bindings.data()},
|
|
|
|
}};
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2018-02-24 15:15:35 +00:00
|
|
|
// Don't set the GS bit if geometry shaders aren't available.
|
2022-07-24 08:32:59 +00:00
|
|
|
if (g_ActiveConfig.UseVSForLinePointExpand())
|
|
|
|
{
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGeometryShaders)
|
|
|
|
ubo_bindings[UBO_DESCRIPTOR_SET_BINDING_GS].stageFlags |= VK_SHADER_STAGE_VERTEX_BIT;
|
|
|
|
else
|
|
|
|
ubo_bindings[UBO_DESCRIPTOR_SET_BINDING_GS].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
|
|
|
|
}
|
|
|
|
else if (!g_ActiveConfig.backend_info.bSupportsGeometryShaders)
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
create_infos[DESCRIPTOR_SET_LAYOUT_STANDARD_UNIFORM_BUFFERS].bindingCount--;
|
2022-07-24 08:32:59 +00:00
|
|
|
}
|
2018-02-24 15:15:35 +00:00
|
|
|
|
2022-06-19 03:49:03 +00:00
|
|
|
// Remove the dynamic vertex loader's buffer if it'll never be needed
|
|
|
|
if (!g_ActiveConfig.backend_info.bSupportsDynamicVertexLoader)
|
|
|
|
create_infos[DESCRIPTOR_SET_LAYOUT_STANDARD_SHADER_STORAGE_BUFFERS].bindingCount--;
|
|
|
|
|
2019-06-01 11:55:09 +00:00
|
|
|
for (size_t i = 0; i < create_infos.size(); i++)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
VkResult res = vkCreateDescriptorSetLayout(g_vulkan_context->GetDevice(), &create_infos[i],
|
|
|
|
nullptr, &m_descriptor_set_layouts[i]);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkCreateDescriptorSetLayout failed: ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectCache::DestroyDescriptorSetLayouts()
|
|
|
|
{
|
|
|
|
for (VkDescriptorSetLayout layout : m_descriptor_set_layouts)
|
|
|
|
{
|
|
|
|
if (layout != VK_NULL_HANDLE)
|
|
|
|
vkDestroyDescriptorSetLayout(g_vulkan_context->GetDevice(), layout, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObjectCache::CreatePipelineLayouts()
|
|
|
|
{
|
2018-02-24 15:15:35 +00:00
|
|
|
// Descriptor sets for each pipeline layout.
|
|
|
|
// In the standard set, the SSBO must be the last descriptor, as we do not include it
|
|
|
|
// when fragment stores and atomics are not supported by the device.
|
2019-06-01 11:55:09 +00:00
|
|
|
const std::array<VkDescriptorSetLayout, 3> standard_sets{
|
2019-02-15 01:59:50 +00:00
|
|
|
m_descriptor_set_layouts[DESCRIPTOR_SET_LAYOUT_STANDARD_UNIFORM_BUFFERS],
|
|
|
|
m_descriptor_set_layouts[DESCRIPTOR_SET_LAYOUT_STANDARD_SAMPLERS],
|
2019-06-01 11:55:09 +00:00
|
|
|
m_descriptor_set_layouts[DESCRIPTOR_SET_LAYOUT_STANDARD_SHADER_STORAGE_BUFFERS],
|
|
|
|
};
|
2022-06-19 03:49:03 +00:00
|
|
|
const std::array<VkDescriptorSetLayout, 3> uber_sets{
|
|
|
|
m_descriptor_set_layouts[DESCRIPTOR_SET_LAYOUT_STANDARD_UNIFORM_BUFFERS],
|
|
|
|
m_descriptor_set_layouts[DESCRIPTOR_SET_LAYOUT_STANDARD_SAMPLERS],
|
|
|
|
m_descriptor_set_layouts[DESCRIPTOR_SET_LAYOUT_STANDARD_SHADER_STORAGE_BUFFERS],
|
|
|
|
};
|
2019-06-01 11:55:09 +00:00
|
|
|
const std::array<VkDescriptorSetLayout, 2> utility_sets{
|
2019-02-15 01:59:50 +00:00
|
|
|
m_descriptor_set_layouts[DESCRIPTOR_SET_LAYOUT_UTILITY_UNIFORM_BUFFER],
|
2019-06-01 11:55:09 +00:00
|
|
|
m_descriptor_set_layouts[DESCRIPTOR_SET_LAYOUT_UTILITY_SAMPLERS],
|
|
|
|
};
|
|
|
|
const std::array<VkDescriptorSetLayout, 1> compute_sets{
|
|
|
|
m_descriptor_set_layouts[DESCRIPTOR_SET_LAYOUT_COMPUTE],
|
|
|
|
};
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// Info for each pipeline layout
|
2019-06-01 11:55:09 +00:00
|
|
|
std::array<VkPipelineLayoutCreateInfo, NUM_PIPELINE_LAYOUTS> pipeline_layout_info{{
|
2016-11-13 05:24:55 +00:00
|
|
|
// Standard
|
|
|
|
{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, nullptr, 0,
|
2019-06-01 11:55:09 +00:00
|
|
|
static_cast<u32>(standard_sets.size()), standard_sets.data(), 0, nullptr},
|
2016-11-13 05:24:55 +00:00
|
|
|
|
2022-06-19 03:49:03 +00:00
|
|
|
// Uber
|
|
|
|
{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, nullptr, 0,
|
|
|
|
static_cast<u32>(uber_sets.size()), uber_sets.data(), 0, nullptr},
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
// Utility
|
2017-09-08 09:42:56 +00:00
|
|
|
{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, nullptr, 0,
|
2019-06-01 11:55:09 +00:00
|
|
|
static_cast<u32>(utility_sets.size()), utility_sets.data(), 0, nullptr},
|
2017-09-08 09:42:56 +00:00
|
|
|
|
2016-12-09 12:23:04 +00:00
|
|
|
// Compute
|
|
|
|
{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, nullptr, 0,
|
2019-06-01 11:55:09 +00:00
|
|
|
static_cast<u32>(compute_sets.size()), compute_sets.data(), 0, nullptr},
|
|
|
|
}};
|
2016-11-13 05:24:55 +00:00
|
|
|
|
2022-07-24 08:32:59 +00:00
|
|
|
const bool ssbos_in_standard =
|
|
|
|
g_ActiveConfig.backend_info.bSupportsBBox || g_ActiveConfig.UseVSForLinePointExpand();
|
|
|
|
|
2018-02-24 15:15:35 +00:00
|
|
|
// If bounding box is unsupported, don't bother with the SSBO descriptor set.
|
2022-07-24 08:32:59 +00:00
|
|
|
if (!ssbos_in_standard)
|
2018-02-24 15:15:35 +00:00
|
|
|
pipeline_layout_info[PIPELINE_LAYOUT_STANDARD].setLayoutCount--;
|
2022-07-11 01:01:38 +00:00
|
|
|
// If neither SSBO-using feature is supported, skip in ubershaders too
|
2022-07-24 08:32:59 +00:00
|
|
|
if (!ssbos_in_standard && !g_ActiveConfig.backend_info.bSupportsDynamicVertexLoader)
|
2022-07-11 01:01:38 +00:00
|
|
|
pipeline_layout_info[PIPELINE_LAYOUT_UBER].setLayoutCount--;
|
2018-02-24 15:15:35 +00:00
|
|
|
|
2019-06-01 11:55:09 +00:00
|
|
|
for (size_t i = 0; i < pipeline_layout_info.size(); i++)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-06-01 11:55:09 +00:00
|
|
|
VkResult res;
|
2016-11-13 05:24:55 +00:00
|
|
|
if ((res = vkCreatePipelineLayout(g_vulkan_context->GetDevice(), &pipeline_layout_info[i],
|
|
|
|
nullptr, &m_pipeline_layouts[i])) != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkCreatePipelineLayout failed: ");
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectCache::DestroyPipelineLayouts()
|
|
|
|
{
|
2016-11-13 05:24:55 +00:00
|
|
|
for (VkPipelineLayout layout : m_pipeline_layouts)
|
|
|
|
{
|
|
|
|
if (layout != VK_NULL_HANDLE)
|
|
|
|
vkDestroyPipelineLayout(g_vulkan_context->GetDevice(), layout, nullptr);
|
|
|
|
}
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ObjectCache::CreateStaticSamplers()
|
|
|
|
{
|
|
|
|
VkSamplerCreateInfo create_info = {
|
|
|
|
VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, // VkStructureType sType
|
|
|
|
nullptr, // const void* pNext
|
|
|
|
0, // VkSamplerCreateFlags flags
|
|
|
|
VK_FILTER_NEAREST, // VkFilter magFilter
|
|
|
|
VK_FILTER_NEAREST, // VkFilter minFilter
|
|
|
|
VK_SAMPLER_MIPMAP_MODE_NEAREST, // VkSamplerMipmapMode mipmapMode
|
|
|
|
VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER, // VkSamplerAddressMode addressModeU
|
|
|
|
VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER, // VkSamplerAddressMode addressModeV
|
|
|
|
VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, // VkSamplerAddressMode addressModeW
|
|
|
|
0.0f, // float mipLodBias
|
|
|
|
VK_FALSE, // VkBool32 anisotropyEnable
|
|
|
|
1.0f, // float maxAnisotropy
|
|
|
|
VK_FALSE, // VkBool32 compareEnable
|
|
|
|
VK_COMPARE_OP_ALWAYS, // VkCompareOp compareOp
|
|
|
|
std::numeric_limits<float>::min(), // float minLod
|
|
|
|
std::numeric_limits<float>::max(), // float maxLod
|
|
|
|
VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK, // VkBorderColor borderColor
|
|
|
|
VK_FALSE // VkBool32 unnormalizedCoordinates
|
|
|
|
};
|
|
|
|
|
|
|
|
VkResult res =
|
|
|
|
vkCreateSampler(g_vulkan_context->GetDevice(), &create_info, nullptr, &m_point_sampler);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkCreateSampler failed: ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Most fields are shared across point<->linear samplers, so only change those necessary.
|
|
|
|
create_info.minFilter = VK_FILTER_LINEAR;
|
|
|
|
create_info.magFilter = VK_FILTER_LINEAR;
|
|
|
|
create_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
|
|
|
res = vkCreateSampler(g_vulkan_context->GetDevice(), &create_info, nullptr, &m_linear_sampler);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkCreateSampler failed: ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
VkSampler ObjectCache::GetSampler(const SamplerState& info)
|
|
|
|
{
|
|
|
|
auto iter = m_sampler_cache.find(info);
|
|
|
|
if (iter != m_sampler_cache.end())
|
|
|
|
return iter->second;
|
|
|
|
|
2017-09-09 08:30:15 +00:00
|
|
|
static constexpr std::array<VkFilter, 4> filters = {{VK_FILTER_NEAREST, VK_FILTER_LINEAR}};
|
|
|
|
static constexpr std::array<VkSamplerMipmapMode, 2> mipmap_modes = {
|
|
|
|
{VK_SAMPLER_MIPMAP_MODE_NEAREST, VK_SAMPLER_MIPMAP_MODE_LINEAR}};
|
|
|
|
static constexpr std::array<VkSamplerAddressMode, 4> address_modes = {
|
|
|
|
{VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, VK_SAMPLER_ADDRESS_MODE_REPEAT,
|
|
|
|
VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT}};
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
VkSamplerCreateInfo create_info = {
|
2021-08-09 04:11:50 +00:00
|
|
|
VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, // VkStructureType sType
|
|
|
|
nullptr, // const void* pNext
|
|
|
|
0, // VkSamplerCreateFlags flags
|
|
|
|
filters[u32(info.tm0.mag_filter.Value())], // VkFilter magFilter
|
|
|
|
filters[u32(info.tm0.min_filter.Value())], // VkFilter minFilter
|
|
|
|
mipmap_modes[u32(info.tm0.mipmap_filter.Value())], // VkSamplerMipmapMode mipmapMode
|
|
|
|
address_modes[u32(info.tm0.wrap_u.Value())], // VkSamplerAddressMode addressModeU
|
|
|
|
address_modes[u32(info.tm0.wrap_v.Value())], // VkSamplerAddressMode addressModeV
|
|
|
|
VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, // VkSamplerAddressMode addressModeW
|
|
|
|
info.tm0.lod_bias / 256.0f, // float mipLodBias
|
2017-09-09 08:30:15 +00:00
|
|
|
VK_FALSE, // VkBool32 anisotropyEnable
|
|
|
|
0.0f, // float maxAnisotropy
|
|
|
|
VK_FALSE, // VkBool32 compareEnable
|
|
|
|
VK_COMPARE_OP_ALWAYS, // VkCompareOp compareOp
|
2021-08-09 04:11:50 +00:00
|
|
|
info.tm1.min_lod / 16.0f, // float minLod
|
|
|
|
info.tm1.max_lod / 16.0f, // float maxLod
|
2017-09-09 08:30:15 +00:00
|
|
|
VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK, // VkBorderColor borderColor
|
|
|
|
VK_FALSE // VkBool32 unnormalizedCoordinates
|
2016-08-13 12:57:50 +00:00
|
|
|
};
|
|
|
|
|
2016-10-02 13:53:09 +00:00
|
|
|
// Can we use anisotropic filtering with this sampler?
|
2021-08-09 04:11:50 +00:00
|
|
|
if (info.tm0.anisotropic_filtering && g_vulkan_context->SupportsAnisotropicFiltering())
|
2016-10-02 13:53:09 +00:00
|
|
|
{
|
|
|
|
// Cap anisotropy to device limits.
|
|
|
|
create_info.anisotropyEnable = VK_TRUE;
|
|
|
|
create_info.maxAnisotropy = std::min(static_cast<float>(1 << g_ActiveConfig.iMaxAnisotropy),
|
|
|
|
g_vulkan_context->GetMaxSamplerAnisotropy());
|
|
|
|
}
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
VkSampler sampler = VK_NULL_HANDLE;
|
|
|
|
VkResult res = vkCreateSampler(g_vulkan_context->GetDevice(), &create_info, nullptr, &sampler);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
LOG_VULKAN_ERROR(res, "vkCreateSampler failed: ");
|
|
|
|
|
|
|
|
// Store it even if it failed
|
|
|
|
m_sampler_cache.emplace(info, sampler);
|
|
|
|
return sampler;
|
|
|
|
}
|
2017-09-09 04:04:26 +00:00
|
|
|
|
|
|
|
VkRenderPass ObjectCache::GetRenderPass(VkFormat color_format, VkFormat depth_format,
|
2023-05-29 01:59:02 +00:00
|
|
|
u32 multisamples, VkAttachmentLoadOp load_op,
|
|
|
|
u8 additional_attachment_count)
|
2017-09-09 04:04:26 +00:00
|
|
|
{
|
2023-05-29 01:59:02 +00:00
|
|
|
auto key =
|
|
|
|
std::tie(color_format, depth_format, multisamples, load_op, additional_attachment_count);
|
2017-09-09 04:04:26 +00:00
|
|
|
auto it = m_render_pass_cache.find(key);
|
|
|
|
if (it != m_render_pass_cache.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
VkAttachmentReference depth_reference;
|
|
|
|
VkAttachmentReference* depth_reference_ptr = nullptr;
|
2023-05-29 01:59:02 +00:00
|
|
|
std::vector<VkAttachmentDescription> attachments;
|
|
|
|
std::vector<VkAttachmentReference> color_attachment_references;
|
2017-09-09 04:04:26 +00:00
|
|
|
if (color_format != VK_FORMAT_UNDEFINED)
|
|
|
|
{
|
2023-05-29 01:59:02 +00:00
|
|
|
VkAttachmentReference color_reference;
|
|
|
|
color_reference.attachment = static_cast<uint32_t>(attachments.size());
|
2017-09-09 04:04:26 +00:00
|
|
|
color_reference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
2023-05-29 01:59:02 +00:00
|
|
|
color_attachment_references.push_back(std::move(color_reference));
|
|
|
|
attachments.push_back({0, color_format, static_cast<VkSampleCountFlagBits>(multisamples),
|
|
|
|
load_op, VK_ATTACHMENT_STORE_OP_STORE, VK_ATTACHMENT_LOAD_OP_DONT_CARE,
|
|
|
|
VK_ATTACHMENT_STORE_OP_DONT_CARE,
|
|
|
|
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
|
|
|
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL});
|
2017-09-09 04:04:26 +00:00
|
|
|
}
|
|
|
|
if (depth_format != VK_FORMAT_UNDEFINED)
|
|
|
|
{
|
2023-05-29 01:59:02 +00:00
|
|
|
depth_reference.attachment = static_cast<uint32_t>(attachments.size());
|
2017-09-09 04:04:26 +00:00
|
|
|
depth_reference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
|
|
depth_reference_ptr = &depth_reference;
|
2023-05-29 01:59:02 +00:00
|
|
|
attachments.push_back({0, depth_format, static_cast<VkSampleCountFlagBits>(multisamples),
|
|
|
|
load_op, VK_ATTACHMENT_STORE_OP_STORE, VK_ATTACHMENT_LOAD_OP_DONT_CARE,
|
|
|
|
VK_ATTACHMENT_STORE_OP_DONT_CARE,
|
|
|
|
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
|
|
|
|
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL});
|
2017-09-09 04:04:26 +00:00
|
|
|
}
|
|
|
|
|
2023-05-29 01:59:02 +00:00
|
|
|
for (u8 i = 0; i < additional_attachment_count; i++)
|
|
|
|
{
|
|
|
|
VkAttachmentReference color_reference;
|
|
|
|
color_reference.attachment = static_cast<uint32_t>(attachments.size());
|
|
|
|
color_reference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
|
|
|
color_attachment_references.push_back(std::move(color_reference));
|
|
|
|
attachments.push_back({0, color_format, static_cast<VkSampleCountFlagBits>(multisamples),
|
|
|
|
load_op, VK_ATTACHMENT_STORE_OP_STORE, VK_ATTACHMENT_LOAD_OP_DONT_CARE,
|
|
|
|
VK_ATTACHMENT_STORE_OP_DONT_CARE,
|
|
|
|
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
|
|
|
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL});
|
|
|
|
}
|
|
|
|
|
|
|
|
VkSubpassDescription subpass = {
|
|
|
|
0,
|
|
|
|
VK_PIPELINE_BIND_POINT_GRAPHICS,
|
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
static_cast<uint32_t>(color_attachment_references.size()),
|
|
|
|
color_attachment_references.empty() ? nullptr : color_attachment_references.data(),
|
|
|
|
nullptr,
|
|
|
|
depth_reference_ptr,
|
|
|
|
0,
|
|
|
|
nullptr};
|
2017-09-09 04:04:26 +00:00
|
|
|
VkRenderPassCreateInfo pass_info = {VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
|
|
|
|
nullptr,
|
|
|
|
0,
|
2023-05-29 01:59:02 +00:00
|
|
|
static_cast<uint32_t>(attachments.size()),
|
2017-09-09 04:04:26 +00:00
|
|
|
attachments.data(),
|
|
|
|
1,
|
|
|
|
&subpass,
|
|
|
|
0,
|
|
|
|
nullptr};
|
|
|
|
|
|
|
|
VkRenderPass pass;
|
|
|
|
VkResult res = vkCreateRenderPass(g_vulkan_context->GetDevice(), &pass_info, nullptr, &pass);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkCreateRenderPass failed: ");
|
|
|
|
return VK_NULL_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_render_pass_cache.emplace(key, pass);
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectCache::DestroyRenderPassCache()
|
|
|
|
{
|
|
|
|
for (auto& it : m_render_pass_cache)
|
|
|
|
vkDestroyRenderPass(g_vulkan_context->GetDevice(), it.second, nullptr);
|
|
|
|
m_render_pass_cache.clear();
|
|
|
|
}
|
2019-02-15 01:59:50 +00:00
|
|
|
|
2023-04-19 13:14:36 +00:00
|
|
|
class PipelineCacheReadCallback : public Common::LinearDiskCacheReader<u32, u8>
|
2019-02-15 01:59:50 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
PipelineCacheReadCallback(std::vector<u8>* data) : m_data(data) {}
|
|
|
|
void Read(const u32& key, const u8* value, u32 value_size) override
|
|
|
|
{
|
|
|
|
m_data->resize(value_size);
|
|
|
|
if (value_size > 0)
|
|
|
|
memcpy(m_data->data(), value, value_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<u8>* m_data;
|
|
|
|
};
|
|
|
|
|
2023-04-19 13:14:36 +00:00
|
|
|
class PipelineCacheReadIgnoreCallback : public Common::LinearDiskCacheReader<u32, u8>
|
2019-02-15 01:59:50 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Read(const u32& key, const u8* value, u32 value_size) override {}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ObjectCache::CreatePipelineCache()
|
|
|
|
{
|
|
|
|
// Vulkan pipeline caches can be shared between games for shader compile time reduction.
|
|
|
|
// This assumes that drivers don't create all pipelines in the cache on load time, only
|
|
|
|
// when a lookup occurs that matches a pipeline (or pipeline data) in the cache.
|
|
|
|
m_pipeline_cache_filename = GetDiskShaderCacheFileName(APIType::Vulkan, "Pipeline", false, true);
|
|
|
|
|
|
|
|
VkPipelineCacheCreateInfo info = {
|
|
|
|
VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO, // VkStructureType sType
|
|
|
|
nullptr, // const void* pNext
|
|
|
|
0, // VkPipelineCacheCreateFlags flags
|
|
|
|
0, // size_t initialDataSize
|
|
|
|
nullptr // const void* pInitialData
|
|
|
|
};
|
|
|
|
|
|
|
|
VkResult res =
|
|
|
|
vkCreatePipelineCache(g_vulkan_context->GetDevice(), &info, nullptr, &m_pipeline_cache);
|
|
|
|
if (res == VK_SUCCESS)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
LOG_VULKAN_ERROR(res, "vkCreatePipelineCache failed: ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObjectCache::LoadPipelineCache()
|
|
|
|
{
|
|
|
|
// We have to keep the pipeline cache file name around since when we save it
|
|
|
|
// we delete the old one, by which time the game's unique ID is already cleared.
|
|
|
|
m_pipeline_cache_filename = GetDiskShaderCacheFileName(APIType::Vulkan, "Pipeline", false, true);
|
|
|
|
|
|
|
|
std::vector<u8> disk_data;
|
2023-04-19 13:14:36 +00:00
|
|
|
Common::LinearDiskCache<u32, u8> disk_cache;
|
2019-02-15 01:59:50 +00:00
|
|
|
PipelineCacheReadCallback read_callback(&disk_data);
|
|
|
|
if (disk_cache.OpenAndRead(m_pipeline_cache_filename, read_callback) != 1)
|
|
|
|
disk_data.clear();
|
|
|
|
|
|
|
|
if (!disk_data.empty() && !ValidatePipelineCache(disk_data.data(), disk_data.size()))
|
|
|
|
{
|
|
|
|
// Don't use this data. In fact, we should delete it to prevent it from being used next time.
|
|
|
|
File::Delete(m_pipeline_cache_filename);
|
|
|
|
return CreatePipelineCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
VkPipelineCacheCreateInfo info = {
|
|
|
|
VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO, // VkStructureType sType
|
|
|
|
nullptr, // const void* pNext
|
|
|
|
0, // VkPipelineCacheCreateFlags flags
|
|
|
|
disk_data.size(), // size_t initialDataSize
|
|
|
|
disk_data.data() // const void* pInitialData
|
|
|
|
};
|
|
|
|
|
|
|
|
VkResult res =
|
|
|
|
vkCreatePipelineCache(g_vulkan_context->GetDevice(), &info, nullptr, &m_pipeline_cache);
|
|
|
|
if (res == VK_SUCCESS)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Failed to create pipeline cache, try with it empty.
|
|
|
|
LOG_VULKAN_ERROR(res, "vkCreatePipelineCache failed, trying empty cache: ");
|
|
|
|
return CreatePipelineCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Based on Vulkan 1.0 specification,
|
|
|
|
// Table 9.1. Layout for pipeline cache header version VK_PIPELINE_CACHE_HEADER_VERSION_ONE
|
|
|
|
// NOTE: This data is assumed to be in little-endian format.
|
|
|
|
#pragma pack(push, 4)
|
|
|
|
struct VK_PIPELINE_CACHE_HEADER
|
|
|
|
{
|
|
|
|
u32 header_length;
|
|
|
|
u32 header_version;
|
|
|
|
u32 vendor_id;
|
|
|
|
u32 device_id;
|
|
|
|
u8 uuid[VK_UUID_SIZE];
|
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
static_assert(std::is_trivially_copyable<VK_PIPELINE_CACHE_HEADER>::value,
|
|
|
|
"VK_PIPELINE_CACHE_HEADER must be trivially copyable");
|
|
|
|
|
|
|
|
bool ObjectCache::ValidatePipelineCache(const u8* data, size_t data_length)
|
|
|
|
{
|
|
|
|
if (data_length < sizeof(VK_PIPELINE_CACHE_HEADER))
|
|
|
|
{
|
2020-11-09 08:26:14 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Pipeline cache failed validation: Invalid header");
|
2019-02-15 01:59:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
VK_PIPELINE_CACHE_HEADER header;
|
|
|
|
std::memcpy(&header, data, sizeof(header));
|
|
|
|
if (header.header_length < sizeof(VK_PIPELINE_CACHE_HEADER))
|
|
|
|
{
|
2020-11-09 08:26:14 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Pipeline cache failed validation: Invalid header length");
|
2019-02-15 01:59:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.header_version != VK_PIPELINE_CACHE_HEADER_VERSION_ONE)
|
|
|
|
{
|
2020-11-09 08:26:14 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Pipeline cache failed validation: Invalid header version");
|
2019-02-15 01:59:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.vendor_id != g_vulkan_context->GetDeviceProperties().vendorID)
|
|
|
|
{
|
2020-11-09 08:26:14 +00:00
|
|
|
ERROR_LOG_FMT(
|
|
|
|
VIDEO, "Pipeline cache failed validation: Incorrect vendor ID (file: {:#X}, device: {:#X})",
|
|
|
|
header.vendor_id, g_vulkan_context->GetDeviceProperties().vendorID);
|
2019-02-15 01:59:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.device_id != g_vulkan_context->GetDeviceProperties().deviceID)
|
|
|
|
{
|
2020-11-09 08:26:14 +00:00
|
|
|
ERROR_LOG_FMT(
|
|
|
|
VIDEO, "Pipeline cache failed validation: Incorrect device ID (file: {:#X}, device: {:#X})",
|
|
|
|
header.device_id, g_vulkan_context->GetDeviceProperties().deviceID);
|
2019-02-15 01:59:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::memcmp(header.uuid, g_vulkan_context->GetDeviceProperties().pipelineCacheUUID,
|
|
|
|
VK_UUID_SIZE) != 0)
|
|
|
|
{
|
2020-11-09 08:26:14 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Pipeline cache failed validation: Incorrect UUID");
|
2019-02-15 01:59:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectCache::DestroyPipelineCache()
|
|
|
|
{
|
|
|
|
vkDestroyPipelineCache(g_vulkan_context->GetDevice(), m_pipeline_cache, nullptr);
|
|
|
|
m_pipeline_cache = VK_NULL_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectCache::SavePipelineCache()
|
|
|
|
{
|
|
|
|
size_t data_size;
|
|
|
|
VkResult res =
|
|
|
|
vkGetPipelineCacheData(g_vulkan_context->GetDevice(), m_pipeline_cache, &data_size, nullptr);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkGetPipelineCacheData failed: ");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> data(data_size);
|
|
|
|
res = vkGetPipelineCacheData(g_vulkan_context->GetDevice(), m_pipeline_cache, &data_size,
|
|
|
|
data.data());
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkGetPipelineCacheData failed: ");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the old cache and re-create.
|
|
|
|
File::Delete(m_pipeline_cache_filename);
|
|
|
|
|
|
|
|
// We write a single key of 1, with the entire pipeline cache data.
|
|
|
|
// Not ideal, but our disk cache class does not support just writing a single blob
|
|
|
|
// of data without specifying a key.
|
2023-04-19 13:14:36 +00:00
|
|
|
Common::LinearDiskCache<u32, u8> disk_cache;
|
2019-02-15 01:59:50 +00:00
|
|
|
PipelineCacheReadIgnoreCallback callback;
|
|
|
|
disk_cache.OpenAndRead(m_pipeline_cache_filename, callback);
|
|
|
|
disk_cache.Append(1, data.data(), static_cast<u32>(data.size()));
|
|
|
|
disk_cache.Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectCache::ReloadPipelineCache()
|
|
|
|
{
|
|
|
|
SavePipelineCache();
|
|
|
|
|
|
|
|
if (g_ActiveConfig.bShaderCache)
|
|
|
|
LoadPipelineCache();
|
|
|
|
else
|
|
|
|
CreatePipelineCache();
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
2019-02-15 01:59:50 +00:00
|
|
|
} // namespace Vulkan
|