diff options
Diffstat (limited to '')
| -rw-r--r-- | src/video_core/CMakeLists.txt | 6 | ||||
| -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 | ||||
| -rw-r--r-- | src/video_core/renderer_vulkan/vk_update_descriptor.cpp | 57 | ||||
| -rw-r--r-- | src/video_core/renderer_vulkan/vk_update_descriptor.h | 86 |
5 files changed, 345 insertions, 1 deletions
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt index abbf218f5..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 |
| @@ -176,7 +178,9 @@ if (ENABLE_VULKAN) | |||
| 176 | renderer_vulkan/vk_stream_buffer.cpp | 178 | renderer_vulkan/vk_stream_buffer.cpp |
| 177 | renderer_vulkan/vk_stream_buffer.h | 179 | renderer_vulkan/vk_stream_buffer.h |
| 178 | renderer_vulkan/vk_swapchain.cpp | 180 | renderer_vulkan/vk_swapchain.cpp |
| 179 | renderer_vulkan/vk_swapchain.h) | 181 | renderer_vulkan/vk_swapchain.h |
| 182 | renderer_vulkan/vk_update_descriptor.cpp | ||
| 183 | renderer_vulkan/vk_update_descriptor.h) | ||
| 180 | 184 | ||
| 181 | target_include_directories(video_core PRIVATE sirit ../../externals/Vulkan-Headers/include) | 185 | target_include_directories(video_core PRIVATE sirit ../../externals/Vulkan-Headers/include) |
| 182 | target_compile_definitions(video_core PRIVATE HAS_VULKAN) | 186 | target_compile_definitions(video_core PRIVATE HAS_VULKAN) |
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 | ||
diff --git a/src/video_core/renderer_vulkan/vk_update_descriptor.cpp b/src/video_core/renderer_vulkan/vk_update_descriptor.cpp new file mode 100644 index 000000000..0e577b9ff --- /dev/null +++ b/src/video_core/renderer_vulkan/vk_update_descriptor.cpp | |||
| @@ -0,0 +1,57 @@ | |||
| 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 <variant> | ||
| 6 | #include <boost/container/static_vector.hpp> | ||
| 7 | |||
| 8 | #include "common/assert.h" | ||
| 9 | #include "common/logging/log.h" | ||
| 10 | #include "video_core/renderer_vulkan/declarations.h" | ||
| 11 | #include "video_core/renderer_vulkan/vk_device.h" | ||
| 12 | #include "video_core/renderer_vulkan/vk_scheduler.h" | ||
| 13 | #include "video_core/renderer_vulkan/vk_update_descriptor.h" | ||
| 14 | |||
| 15 | namespace Vulkan { | ||
| 16 | |||
| 17 | VKUpdateDescriptorQueue::VKUpdateDescriptorQueue(const VKDevice& device, VKScheduler& scheduler) | ||
| 18 | : device{device}, scheduler{scheduler} {} | ||
| 19 | |||
| 20 | VKUpdateDescriptorQueue::~VKUpdateDescriptorQueue() = default; | ||
| 21 | |||
| 22 | void VKUpdateDescriptorQueue::TickFrame() { | ||
| 23 | payload.clear(); | ||
| 24 | } | ||
| 25 | |||
| 26 | void VKUpdateDescriptorQueue::Acquire() { | ||
| 27 | entries.clear(); | ||
| 28 | } | ||
| 29 | |||
| 30 | void VKUpdateDescriptorQueue::Send(vk::DescriptorUpdateTemplate update_template, | ||
| 31 | vk::DescriptorSet set) { | ||
| 32 | if (payload.size() + entries.size() >= payload.max_size()) { | ||
| 33 | LOG_WARNING(Render_Vulkan, "Payload overflow, waiting for worker thread"); | ||
| 34 | scheduler.WaitWorker(); | ||
| 35 | payload.clear(); | ||
| 36 | } | ||
| 37 | |||
| 38 | const auto payload_start = payload.data() + payload.size(); | ||
| 39 | for (const auto& entry : entries) { | ||
| 40 | if (const auto image = std::get_if<vk::DescriptorImageInfo>(&entry)) { | ||
| 41 | payload.push_back(*image); | ||
| 42 | } else if (const auto buffer = std::get_if<Buffer>(&entry)) { | ||
| 43 | payload.emplace_back(*buffer->buffer, buffer->offset, buffer->size); | ||
| 44 | } else if (const auto texel = std::get_if<vk::BufferView>(&entry)) { | ||
| 45 | payload.push_back(*texel); | ||
| 46 | } else { | ||
| 47 | UNREACHABLE(); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | scheduler.Record([dev = device.GetLogical(), payload_start, set, | ||
| 52 | update_template]([[maybe_unused]] auto cmdbuf, auto& dld) { | ||
| 53 | dev.updateDescriptorSetWithTemplate(set, update_template, payload_start, dld); | ||
| 54 | }); | ||
| 55 | } | ||
| 56 | |||
| 57 | } // namespace Vulkan | ||
diff --git a/src/video_core/renderer_vulkan/vk_update_descriptor.h b/src/video_core/renderer_vulkan/vk_update_descriptor.h new file mode 100644 index 000000000..8c825aa29 --- /dev/null +++ b/src/video_core/renderer_vulkan/vk_update_descriptor.h | |||
| @@ -0,0 +1,86 @@ | |||
| 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 <type_traits> | ||
| 8 | #include <variant> | ||
| 9 | #include <boost/container/static_vector.hpp> | ||
| 10 | |||
| 11 | #include "common/common_types.h" | ||
| 12 | #include "video_core/renderer_vulkan/declarations.h" | ||
| 13 | |||
| 14 | namespace Vulkan { | ||
| 15 | |||
| 16 | class VKDevice; | ||
| 17 | class VKScheduler; | ||
| 18 | |||
| 19 | class DescriptorUpdateEntry { | ||
| 20 | public: | ||
| 21 | explicit DescriptorUpdateEntry() : image{} {} | ||
| 22 | |||
| 23 | DescriptorUpdateEntry(vk::DescriptorImageInfo image) : image{image} {} | ||
| 24 | |||
| 25 | DescriptorUpdateEntry(vk::Buffer buffer, vk::DeviceSize offset, vk::DeviceSize size) | ||
| 26 | : buffer{buffer, offset, size} {} | ||
| 27 | |||
| 28 | DescriptorUpdateEntry(vk::BufferView texel_buffer) : texel_buffer{texel_buffer} {} | ||
| 29 | |||
| 30 | private: | ||
| 31 | union { | ||
| 32 | vk::DescriptorImageInfo image; | ||
| 33 | vk::DescriptorBufferInfo buffer; | ||
| 34 | vk::BufferView texel_buffer; | ||
| 35 | }; | ||
| 36 | }; | ||
| 37 | |||
| 38 | class VKUpdateDescriptorQueue final { | ||
| 39 | public: | ||
| 40 | explicit VKUpdateDescriptorQueue(const VKDevice& device, VKScheduler& scheduler); | ||
| 41 | ~VKUpdateDescriptorQueue(); | ||
| 42 | |||
| 43 | void TickFrame(); | ||
| 44 | |||
| 45 | void Acquire(); | ||
| 46 | |||
| 47 | void Send(vk::DescriptorUpdateTemplate update_template, vk::DescriptorSet set); | ||
| 48 | |||
| 49 | void AddSampledImage(vk::Sampler sampler, vk::ImageView image_view) { | ||
| 50 | entries.emplace_back(vk::DescriptorImageInfo{sampler, image_view, {}}); | ||
| 51 | } | ||
| 52 | |||
| 53 | void AddImage(vk::ImageView image_view) { | ||
| 54 | entries.emplace_back(vk::DescriptorImageInfo{{}, image_view, {}}); | ||
| 55 | } | ||
| 56 | |||
| 57 | void AddBuffer(const vk::Buffer* buffer, u64 offset, std::size_t size) { | ||
| 58 | entries.push_back(Buffer{buffer, offset, size}); | ||
| 59 | } | ||
| 60 | |||
| 61 | void AddTexelBuffer(vk::BufferView texel_buffer) { | ||
| 62 | entries.emplace_back(texel_buffer); | ||
| 63 | } | ||
| 64 | |||
| 65 | vk::ImageLayout* GetLastImageLayout() { | ||
| 66 | return &std::get<vk::DescriptorImageInfo>(entries.back()).imageLayout; | ||
| 67 | } | ||
| 68 | |||
| 69 | private: | ||
| 70 | struct Buffer { | ||
| 71 | const vk::Buffer* buffer{}; | ||
| 72 | u64 offset{}; | ||
| 73 | std::size_t size{}; | ||
| 74 | }; | ||
| 75 | using Variant = std::variant<vk::DescriptorImageInfo, Buffer, vk::BufferView>; | ||
| 76 | // Old gcc versions don't consider this trivially copyable. | ||
| 77 | // static_assert(std::is_trivially_copyable_v<Variant>); | ||
| 78 | |||
| 79 | const VKDevice& device; | ||
| 80 | VKScheduler& scheduler; | ||
| 81 | |||
| 82 | boost::container::static_vector<Variant, 0x400> entries; | ||
| 83 | boost::container::static_vector<DescriptorUpdateEntry, 0x10000> payload; | ||
| 84 | }; | ||
| 85 | |||
| 86 | } // namespace Vulkan | ||