summaryrefslogtreecommitdiffstats
path: root/src/video_core/renderer_vulkan/vk_renderpass_cache.cpp
blob: 80284cf92cb4df854e6bc9de1c2653c33f3901a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2019 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <cstring>
#include <memory>
#include <vector>

#include "common/cityhash.h"
#include "video_core/engines/maxwell_3d.h"
#include "video_core/renderer_vulkan/maxwell_to_vk.h"
#include "video_core/renderer_vulkan/vk_device.h"
#include "video_core/renderer_vulkan/vk_renderpass_cache.h"
#include "video_core/renderer_vulkan/wrapper.h"

namespace Vulkan {

std::size_t RenderPassParams::Hash() const noexcept {
    const u64 hash = Common::CityHash64(reinterpret_cast<const char*>(this), sizeof *this);
    return static_cast<std::size_t>(hash);
}

bool RenderPassParams::operator==(const RenderPassParams& rhs) const noexcept {
    return std::memcmp(&rhs, this, sizeof *this) == 0;
}

VKRenderPassCache::VKRenderPassCache(const VKDevice& device) : device{device} {}

VKRenderPassCache::~VKRenderPassCache() = default;

VkRenderPass VKRenderPassCache::GetRenderPass(const RenderPassParams& params) {
    const auto [pair, is_cache_miss] = cache.try_emplace(params);
    auto& entry = pair->second;
    if (is_cache_miss) {
        entry = CreateRenderPass(params);
    }
    return *entry;
}

vk::RenderPass VKRenderPassCache::CreateRenderPass(const RenderPassParams& params) const {
    using namespace VideoCore::Surface;
    const std::size_t num_attachments = static_cast<std::size_t>(params.num_color_attachments);

    std::vector<VkAttachmentDescription> descriptors;
    descriptors.reserve(num_attachments);

    std::vector<VkAttachmentReference> color_references;
    color_references.reserve(num_attachments);

    for (std::size_t rt = 0; rt < num_attachments; ++rt) {
        const auto guest_format = static_cast<Tegra::RenderTargetFormat>(params.color_formats[rt]);
        const PixelFormat pixel_format = PixelFormatFromRenderTargetFormat(guest_format);
        const auto format = MaxwellToVK::SurfaceFormat(device, FormatType::Optimal, pixel_format);
        ASSERT_MSG(format.attachable, "Trying to attach a non-attachable format with format={}",
                   static_cast<int>(pixel_format));

        // TODO(Rodrigo): Add MAY_ALIAS_BIT when it's needed.
        const VkImageLayout color_layout = ((params.texceptions >> rt) & 1) != 0
                                               ? VK_IMAGE_LAYOUT_GENERAL
                                               : VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
        descriptors.push_back({
            .flags = VK_ATTACHMENT_DESCRIPTION_MAY_ALIAS_BIT,
            .format = format.format,
            .samples = VK_SAMPLE_COUNT_1_BIT,
            .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
            .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
            .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
            .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
            .initialLayout = color_layout,
            .finalLayout = color_layout,
        });

        color_references.push_back({
            .attachment = static_cast<u32>(rt),
            .layout = color_layout,
        });
    }

    VkAttachmentReference zeta_attachment_ref;
    const bool has_zeta = params.zeta_format != 0;
    if (has_zeta) {
        const auto guest_format = static_cast<Tegra::DepthFormat>(params.zeta_format);
        const PixelFormat pixel_format = PixelFormatFromDepthFormat(guest_format);
        const auto format = MaxwellToVK::SurfaceFormat(device, FormatType::Optimal, pixel_format);
        ASSERT_MSG(format.attachable, "Trying to attach a non-attachable format with format={}",
                   static_cast<int>(pixel_format));

        const VkImageLayout zeta_layout = params.zeta_texception != 0
                                              ? VK_IMAGE_LAYOUT_GENERAL
                                              : VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
        descriptors.push_back({
            .flags = 0,
            .format = format.format,
            .samples = VK_SAMPLE_COUNT_1_BIT,
            .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 = zeta_layout,
            .finalLayout = zeta_layout,
        });

        zeta_attachment_ref = {
            .attachment = static_cast<u32>(num_attachments),
            .layout = zeta_layout,
        };
    }

    const VkSubpassDescription subpass_description{
        .flags = 0,
        .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
        .inputAttachmentCount = 0,
        .pInputAttachments = nullptr,
        .colorAttachmentCount = static_cast<u32>(color_references.size()),
        .pColorAttachments = color_references.data(),
        .pResolveAttachments = nullptr,
        .pDepthStencilAttachment = has_zeta ? &zeta_attachment_ref : nullptr,
        .preserveAttachmentCount = 0,
        .pPreserveAttachments = nullptr,
    };

    VkAccessFlags access = 0;
    VkPipelineStageFlags stage = 0;
    if (!color_references.empty()) {
        access |= VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
        stage |= VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
    }

    if (has_zeta) {
        access |= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
                  VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
        stage |= VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
    }

    const VkSubpassDependency subpass_dependency{
        .srcSubpass = VK_SUBPASS_EXTERNAL,
        .dstSubpass = 0,
        .srcStageMask = stage,
        .dstStageMask = stage,
        .srcAccessMask = 0,
        .dstAccessMask = access,
        .dependencyFlags = 0,
    };

    return device.GetLogical().CreateRenderPass({
        .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
        .pNext = nullptr,
        .flags = 0,
        .attachmentCount = static_cast<u32>(descriptors.size()),
        .pAttachments = descriptors.data(),
        .subpassCount = 1,
        .pSubpasses = &subpass_description,
        .dependencyCount = 1,
        .pDependencies = &subpass_dependency,
    });
}

} // namespace Vulkan