2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-03-19 23:28:31 +01:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include <boost/container/static_vector.hpp>
|
|
|
|
|
|
|
|
#include "video_core/renderer_vulkan/maxwell_to_vk.h"
|
|
|
|
#include "video_core/renderer_vulkan/vk_render_pass_cache.h"
|
|
|
|
#include "video_core/surface.h"
|
|
|
|
#include "video_core/vulkan_common/vulkan_device.h"
|
|
|
|
#include "video_core/vulkan_common/vulkan_wrapper.h"
|
|
|
|
|
|
|
|
namespace Vulkan {
|
|
|
|
namespace {
|
|
|
|
using VideoCore::Surface::PixelFormat;
|
|
|
|
|
|
|
|
VkAttachmentDescription AttachmentDescription(const Device& device, PixelFormat format,
|
|
|
|
VkSampleCountFlagBits samples) {
|
|
|
|
using MaxwellToVK::SurfaceFormat;
|
|
|
|
return {
|
|
|
|
.flags = VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT,
|
|
|
|
.format = SurfaceFormat(device, FormatType::Optimal, true, format).format,
|
|
|
|
.samples = samples,
|
|
|
|
.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
|
|
|
|
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
|
|
|
|
.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
|
|
|
|
.stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE,
|
|
|
|
.initialLayout = VK_IMAGE_LAYOUT_GENERAL,
|
|
|
|
.finalLayout = VK_IMAGE_LAYOUT_GENERAL,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
|
|
|
RenderPassCache::RenderPassCache(const Device& device_) : device{&device_} {}
|
|
|
|
|
|
|
|
VkRenderPass RenderPassCache::Get(const RenderPassKey& key) {
|
2022-04-07 20:32:40 +02:00
|
|
|
std::scoped_lock lock{mutex};
|
2021-03-19 23:28:31 +01:00
|
|
|
const auto [pair, is_new] = cache.try_emplace(key);
|
|
|
|
if (!is_new) {
|
|
|
|
return *pair->second;
|
|
|
|
}
|
|
|
|
boost::container::static_vector<VkAttachmentDescription, 9> descriptions;
|
2021-04-23 02:05:10 +02:00
|
|
|
std::array<VkAttachmentReference, 8> references{};
|
|
|
|
u32 num_attachments{};
|
|
|
|
u32 num_colors{};
|
2021-03-19 23:28:31 +01:00
|
|
|
for (size_t index = 0; index < key.color_formats.size(); ++index) {
|
|
|
|
const PixelFormat format{key.color_formats[index]};
|
2021-04-23 02:05:10 +02:00
|
|
|
const bool is_valid{format != PixelFormat::Invalid};
|
|
|
|
references[index] = VkAttachmentReference{
|
|
|
|
.attachment = is_valid ? num_colors : VK_ATTACHMENT_UNUSED,
|
|
|
|
.layout = VK_IMAGE_LAYOUT_GENERAL,
|
|
|
|
};
|
|
|
|
if (is_valid) {
|
|
|
|
descriptions.push_back(AttachmentDescription(*device, format, key.samples));
|
|
|
|
num_attachments = static_cast<u32>(index + 1);
|
|
|
|
++num_colors;
|
2021-03-19 23:28:31 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-23 02:05:10 +02:00
|
|
|
const bool has_depth{key.depth_format != PixelFormat::Invalid};
|
|
|
|
VkAttachmentReference depth_reference{};
|
2021-03-19 23:28:31 +01:00
|
|
|
if (key.depth_format != PixelFormat::Invalid) {
|
2021-04-23 02:05:10 +02:00
|
|
|
depth_reference = VkAttachmentReference{
|
|
|
|
.attachment = num_colors,
|
|
|
|
.layout = VK_IMAGE_LAYOUT_GENERAL,
|
|
|
|
};
|
2021-03-19 23:28:31 +01:00
|
|
|
descriptions.push_back(AttachmentDescription(*device, key.depth_format, key.samples));
|
|
|
|
}
|
|
|
|
const VkSubpassDescription subpass{
|
|
|
|
.flags = 0,
|
|
|
|
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
|
|
|
|
.inputAttachmentCount = 0,
|
|
|
|
.pInputAttachments = nullptr,
|
2021-04-23 02:05:10 +02:00
|
|
|
.colorAttachmentCount = num_attachments,
|
|
|
|
.pColorAttachments = references.data(),
|
2021-03-19 23:28:31 +01:00
|
|
|
.pResolveAttachments = nullptr,
|
2021-04-23 02:05:10 +02:00
|
|
|
.pDepthStencilAttachment = has_depth ? &depth_reference : nullptr,
|
2021-03-19 23:28:31 +01:00
|
|
|
.preserveAttachmentCount = 0,
|
|
|
|
.pPreserveAttachments = nullptr,
|
|
|
|
};
|
|
|
|
pair->second = device->GetLogical().CreateRenderPass({
|
|
|
|
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
|
|
|
|
.pNext = nullptr,
|
|
|
|
.flags = 0,
|
|
|
|
.attachmentCount = static_cast<u32>(descriptions.size()),
|
2021-03-26 22:55:07 +01:00
|
|
|
.pAttachments = descriptions.empty() ? nullptr : descriptions.data(),
|
2021-03-19 23:28:31 +01:00
|
|
|
.subpassCount = 1,
|
|
|
|
.pSubpasses = &subpass,
|
|
|
|
.dependencyCount = 0,
|
|
|
|
.pDependencies = nullptr,
|
|
|
|
});
|
|
|
|
return *pair->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Vulkan
|