aboutsummaryrefslogtreecommitdiff
path: root/src/video_core/renderer_vulkan/vk_render_pass_cache.h
blob: 0edad4639ec7425451992fef4a704996e2553fa6 (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
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later

// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <mutex>
#include "common/container/unordered_map.h"

#include "common/container_hash.h"
#include "video_core/surface.h"
#include "video_core/vulkan_common/vulkan_wrapper.h"

namespace Vulkan {

struct RenderPassKey {
    bool operator==(const RenderPassKey&) const noexcept = default;

    std::array<VideoCore::Surface::PixelFormat, 8> color_formats;
    VideoCore::Surface::PixelFormat depth_format;
    VkSampleCountFlagBits samples;
    bool resolve_color;
    bool resolve_depth_stencil;
    u32 color_clear_mask;
    bool depth_stencil_clear;
    u32 color_discard_mask;
    bool depth_stencil_discard;
};

} // namespace Vulkan

namespace std {
template <>
struct hash<Vulkan::RenderPassKey> {
    static_assert(std::tuple_size_v<decltype(Vulkan::RenderPassKey::color_formats)> <= 8);
    static_assert(static_cast<u32>(VideoCore::Surface::PixelFormat::Invalid) <= 0xFF);
    static_assert(static_cast<u32>(VideoCore::Surface::PixelFormat::Max) <= 0xFF);
    static_assert(VK_SAMPLE_COUNT_64_BIT <= 0xFF);

    [[nodiscard]] size_t operator()(const Vulkan::RenderPassKey& key) const noexcept {
        u64 formats = 0;
        for (size_t index = 0; index < key.color_formats.size(); ++index) {
            formats |= static_cast<u64>(key.color_formats[index]) << (index * 8);
        }
        const u64 state = static_cast<u64>(key.depth_format) |
                          (static_cast<u64>(key.samples) << 8) |
                          (static_cast<u64>(key.color_clear_mask) << 16) |
                          (static_cast<u64>(key.color_discard_mask) << 24) |
                          (static_cast<u64>(key.resolve_color) << 32) |
                          (static_cast<u64>(key.depth_stencil_clear) << 33) |
                          (static_cast<u64>(key.resolve_depth_stencil) << 34) |
                          (static_cast<u64>(key.depth_stencil_discard) << 35);
        size_t seed = 0;
        Common::HashCombine(seed, formats);
        Common::HashCombine(seed, state);
        return seed;
    }
};
} // namespace std

namespace Vulkan {

class Device;

[[nodiscard]] bool SupportsDepthStencilResolve(const Device& device,
                                               VideoCore::Surface::PixelFormat depth_format);

class RenderPassCache {
public:
    explicit RenderPassCache(const Device& device_);

    VkRenderPass Get(const RenderPassKey& key);

private:
    const Device* device{};
    ::Common::unordered_map<RenderPassKey, vk::RenderPass> cache;
    std::mutex mutex;
};

} // namespace Vulkan