diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/video_core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/video_core/renderer_vulkan/vk_renderpass_cache.cpp | 100 | ||||
| -rw-r--r-- | src/video_core/renderer_vulkan/vk_renderpass_cache.h | 97 |
3 files changed, 199 insertions, 0 deletions
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt index 47290cbcb..c80171fe6 100644 --- a/src/video_core/CMakeLists.txt +++ b/src/video_core/CMakeLists.txt | |||
| @@ -163,6 +163,8 @@ if (ENABLE_VULKAN) | |||
| 163 | renderer_vulkan/vk_image.h | 163 | renderer_vulkan/vk_image.h |
| 164 | renderer_vulkan/vk_memory_manager.cpp | 164 | renderer_vulkan/vk_memory_manager.cpp |
| 165 | renderer_vulkan/vk_memory_manager.h | 165 | renderer_vulkan/vk_memory_manager.h |
| 166 | renderer_vulkan/vk_renderpass_cache.cpp | ||
| 167 | renderer_vulkan/vk_renderpass_cache.h | ||
| 166 | renderer_vulkan/vk_resource_manager.cpp | 168 | renderer_vulkan/vk_resource_manager.cpp |
| 167 | renderer_vulkan/vk_resource_manager.h | 169 | renderer_vulkan/vk_resource_manager.h |
| 168 | renderer_vulkan/vk_sampler_cache.cpp | 170 | renderer_vulkan/vk_sampler_cache.cpp |
diff --git a/src/video_core/renderer_vulkan/vk_renderpass_cache.cpp b/src/video_core/renderer_vulkan/vk_renderpass_cache.cpp new file mode 100644 index 000000000..93f5d7ba0 --- /dev/null +++ b/src/video_core/renderer_vulkan/vk_renderpass_cache.cpp | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | // Copyright 2019 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <memory> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "video_core/engines/maxwell_3d.h" | ||
| 9 | #include "video_core/renderer_vulkan/declarations.h" | ||
| 10 | #include "video_core/renderer_vulkan/maxwell_to_vk.h" | ||
| 11 | #include "video_core/renderer_vulkan/vk_device.h" | ||
| 12 | #include "video_core/renderer_vulkan/vk_renderpass_cache.h" | ||
| 13 | |||
| 14 | namespace Vulkan { | ||
| 15 | |||
| 16 | VKRenderPassCache::VKRenderPassCache(const VKDevice& device) : device{device} {} | ||
| 17 | |||
| 18 | VKRenderPassCache::~VKRenderPassCache() = default; | ||
| 19 | |||
| 20 | vk::RenderPass VKRenderPassCache::GetRenderPass(const RenderPassParams& params) { | ||
| 21 | const auto [pair, is_cache_miss] = cache.try_emplace(params); | ||
| 22 | auto& entry = pair->second; | ||
| 23 | if (is_cache_miss) { | ||
| 24 | entry = CreateRenderPass(params); | ||
| 25 | } | ||
| 26 | return *entry; | ||
| 27 | } | ||
| 28 | |||
| 29 | UniqueRenderPass VKRenderPassCache::CreateRenderPass(const RenderPassParams& params) const { | ||
| 30 | std::vector<vk::AttachmentDescription> descriptors; | ||
| 31 | std::vector<vk::AttachmentReference> color_references; | ||
| 32 | |||
| 33 | for (std::size_t rt = 0; rt < params.color_attachments.size(); ++rt) { | ||
| 34 | const auto attachment = params.color_attachments[rt]; | ||
| 35 | const auto format = | ||
| 36 | MaxwellToVK::SurfaceFormat(device, FormatType::Optimal, attachment.pixel_format); | ||
| 37 | ASSERT_MSG(format.attachable, "Trying to attach a non-attachable format with format={}", | ||
| 38 | static_cast<u32>(attachment.pixel_format)); | ||
| 39 | |||
| 40 | // TODO(Rodrigo): Add eMayAlias when it's needed. | ||
| 41 | const auto color_layout = attachment.is_texception | ||
| 42 | ? vk::ImageLayout::eGeneral | ||
| 43 | : vk::ImageLayout::eColorAttachmentOptimal; | ||
| 44 | descriptors.emplace_back(vk::AttachmentDescriptionFlagBits::eMayAlias, format.format, | ||
| 45 | vk::SampleCountFlagBits::e1, vk::AttachmentLoadOp::eLoad, | ||
| 46 | vk::AttachmentStoreOp::eStore, vk::AttachmentLoadOp::eDontCare, | ||
| 47 | vk::AttachmentStoreOp::eDontCare, color_layout, color_layout); | ||
| 48 | color_references.emplace_back(static_cast<u32>(rt), color_layout); | ||
| 49 | } | ||
| 50 | |||
| 51 | vk::AttachmentReference zeta_attachment_ref; | ||
| 52 | if (params.has_zeta) { | ||
| 53 | const auto format = | ||
| 54 | MaxwellToVK::SurfaceFormat(device, FormatType::Optimal, params.zeta_pixel_format); | ||
| 55 | ASSERT_MSG(format.attachable, "Trying to attach a non-attachable format with format={}", | ||
| 56 | static_cast<u32>(params.zeta_pixel_format)); | ||
| 57 | |||
| 58 | const auto zeta_layout = params.zeta_texception | ||
| 59 | ? vk::ImageLayout::eGeneral | ||
| 60 | : vk::ImageLayout::eDepthStencilAttachmentOptimal; | ||
| 61 | descriptors.emplace_back(vk::AttachmentDescriptionFlags{}, format.format, | ||
| 62 | vk::SampleCountFlagBits::e1, vk::AttachmentLoadOp::eLoad, | ||
| 63 | vk::AttachmentStoreOp::eStore, vk::AttachmentLoadOp::eLoad, | ||
| 64 | vk::AttachmentStoreOp::eStore, zeta_layout, zeta_layout); | ||
| 65 | zeta_attachment_ref = | ||
| 66 | vk::AttachmentReference(static_cast<u32>(params.color_attachments.size()), zeta_layout); | ||
| 67 | } | ||
| 68 | |||
| 69 | const vk::SubpassDescription subpass_description( | ||
| 70 | {}, vk::PipelineBindPoint::eGraphics, 0, nullptr, static_cast<u32>(color_references.size()), | ||
| 71 | color_references.data(), nullptr, params.has_zeta ? &zeta_attachment_ref : nullptr, 0, | ||
| 72 | nullptr); | ||
| 73 | |||
| 74 | vk::AccessFlags access; | ||
| 75 | vk::PipelineStageFlags stage; | ||
| 76 | if (!color_references.empty()) { | ||
| 77 | access |= | ||
| 78 | vk::AccessFlagBits::eColorAttachmentRead | vk::AccessFlagBits::eColorAttachmentWrite; | ||
| 79 | stage |= vk::PipelineStageFlagBits::eColorAttachmentOutput; | ||
| 80 | } | ||
| 81 | |||
| 82 | if (params.has_zeta) { | ||
| 83 | access |= vk::AccessFlagBits::eDepthStencilAttachmentRead | | ||
| 84 | vk::AccessFlagBits::eDepthStencilAttachmentWrite; | ||
| 85 | stage |= vk::PipelineStageFlagBits::eLateFragmentTests; | ||
| 86 | } | ||
| 87 | |||
| 88 | const vk::SubpassDependency subpass_dependency(VK_SUBPASS_EXTERNAL, 0, stage, stage, {}, access, | ||
| 89 | {}); | ||
| 90 | |||
| 91 | const vk::RenderPassCreateInfo create_info({}, static_cast<u32>(descriptors.size()), | ||
| 92 | descriptors.data(), 1, &subpass_description, 1, | ||
| 93 | &subpass_dependency); | ||
| 94 | |||
| 95 | const auto dev = device.GetLogical(); | ||
| 96 | const auto& dld = device.GetDispatchLoader(); | ||
| 97 | return dev.createRenderPassUnique(create_info, nullptr, dld); | ||
| 98 | } | ||
| 99 | |||
| 100 | } // namespace Vulkan | ||
diff --git a/src/video_core/renderer_vulkan/vk_renderpass_cache.h b/src/video_core/renderer_vulkan/vk_renderpass_cache.h new file mode 100644 index 000000000..b49b2db48 --- /dev/null +++ b/src/video_core/renderer_vulkan/vk_renderpass_cache.h | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | // Copyright 2019 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <memory> | ||
| 8 | #include <tuple> | ||
| 9 | #include <unordered_map> | ||
| 10 | |||
| 11 | #include <boost/container/static_vector.hpp> | ||
| 12 | #include <boost/functional/hash.hpp> | ||
| 13 | |||
| 14 | #include "video_core/engines/maxwell_3d.h" | ||
| 15 | #include "video_core/renderer_vulkan/declarations.h" | ||
| 16 | #include "video_core/surface.h" | ||
| 17 | |||
| 18 | namespace Vulkan { | ||
| 19 | |||
| 20 | class VKDevice; | ||
| 21 | |||
| 22 | // TODO(Rodrigo): Optimize this structure for faster hashing | ||
| 23 | |||
| 24 | struct RenderPassParams { | ||
| 25 | struct ColorAttachment { | ||
| 26 | u32 index = 0; | ||
| 27 | VideoCore::Surface::PixelFormat pixel_format = VideoCore::Surface::PixelFormat::Invalid; | ||
| 28 | bool is_texception = false; | ||
| 29 | |||
| 30 | std::size_t Hash() const noexcept { | ||
| 31 | return static_cast<std::size_t>(pixel_format) | | ||
| 32 | static_cast<std::size_t>(is_texception) << 6 | | ||
| 33 | static_cast<std::size_t>(index) << 7; | ||
| 34 | } | ||
| 35 | |||
| 36 | bool operator==(const ColorAttachment& rhs) const noexcept { | ||
| 37 | return std::tie(index, pixel_format, is_texception) == | ||
| 38 | std::tie(rhs.index, rhs.pixel_format, rhs.is_texception); | ||
| 39 | } | ||
| 40 | }; | ||
| 41 | |||
| 42 | boost::container::static_vector<ColorAttachment, | ||
| 43 | Tegra::Engines::Maxwell3D::Regs::NumRenderTargets> | ||
| 44 | color_attachments{}; | ||
| 45 | // TODO(Rodrigo): Unify has_zeta into zeta_pixel_format and zeta_component_type. | ||
| 46 | VideoCore::Surface::PixelFormat zeta_pixel_format = VideoCore::Surface::PixelFormat::Invalid; | ||
| 47 | bool has_zeta = false; | ||
| 48 | bool zeta_texception = false; | ||
| 49 | |||
| 50 | std::size_t Hash() const noexcept { | ||
| 51 | std::size_t hash = 0; | ||
| 52 | for (const auto& rt : color_attachments) { | ||
| 53 | boost::hash_combine(hash, rt.Hash()); | ||
| 54 | } | ||
| 55 | boost::hash_combine(hash, zeta_pixel_format); | ||
| 56 | boost::hash_combine(hash, has_zeta); | ||
| 57 | boost::hash_combine(hash, zeta_texception); | ||
| 58 | return hash; | ||
| 59 | } | ||
| 60 | |||
| 61 | bool operator==(const RenderPassParams& rhs) const { | ||
| 62 | return std::tie(color_attachments, zeta_pixel_format, has_zeta, zeta_texception) == | ||
| 63 | std::tie(rhs.color_attachments, rhs.zeta_pixel_format, rhs.has_zeta, | ||
| 64 | rhs.zeta_texception); | ||
| 65 | } | ||
| 66 | }; | ||
| 67 | |||
| 68 | } // namespace Vulkan | ||
| 69 | |||
| 70 | namespace std { | ||
| 71 | |||
| 72 | template <> | ||
| 73 | struct hash<Vulkan::RenderPassParams> { | ||
| 74 | std::size_t operator()(const Vulkan::RenderPassParams& k) const noexcept { | ||
| 75 | return k.Hash(); | ||
| 76 | } | ||
| 77 | }; | ||
| 78 | |||
| 79 | } // namespace std | ||
| 80 | |||
| 81 | namespace Vulkan { | ||
| 82 | |||
| 83 | class VKRenderPassCache final { | ||
| 84 | public: | ||
| 85 | explicit VKRenderPassCache(const VKDevice& device); | ||
| 86 | ~VKRenderPassCache(); | ||
| 87 | |||
| 88 | vk::RenderPass GetRenderPass(const RenderPassParams& params); | ||
| 89 | |||
| 90 | private: | ||
| 91 | UniqueRenderPass CreateRenderPass(const RenderPassParams& params) const; | ||
| 92 | |||
| 93 | const VKDevice& device; | ||
| 94 | std::unordered_map<RenderPassParams, UniqueRenderPass> cache; | ||
| 95 | }; | ||
| 96 | |||
| 97 | } // namespace Vulkan | ||