summaryrefslogtreecommitdiffstats
path: root/src/video_core/renderer_vulkan/vk_stream_buffer.h
blob: 69d036ccd3a3b3e46aad2103316570c833e037fb (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
// Copyright 2019 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <memory>
#include <optional>
#include <tuple>
#include <vector>

#include "common/common_types.h"
#include "video_core/renderer_vulkan/declarations.h"
#include "video_core/renderer_vulkan/vk_memory_manager.h"

namespace Vulkan {

class VKDevice;
class VKFence;
class VKFenceWatch;
class VKResourceManager;
class VKScheduler;

class VKStreamBuffer {
public:
    explicit VKStreamBuffer(const VKDevice& device, VKMemoryManager& memory_manager,
                            VKScheduler& scheduler, u64 size, vk::BufferUsageFlags usage,
                            vk::AccessFlags access, vk::PipelineStageFlags pipeline_stage);
    ~VKStreamBuffer();

    /**
     * Reserves a region of memory from the stream buffer.
     * @param size Size to reserve.
     * @returns A tuple in the following order: Raw memory pointer (with offset added), buffer
     * offset and a boolean that's true when buffer has been invalidated.
     */
    std::tuple<u8*, u64, bool> Reserve(u64 size);

    /// Ensures that "size" bytes of memory are available to the GPU, potentially recording a copy.
    [[nodiscard]] VKExecutionContext Send(VKExecutionContext exctx, u64 size);

    vk::Buffer GetBuffer() const {
        return *buffer;
    }

private:
    /// Creates Vulkan buffer handles committing the required the required memory.
    void CreateBuffers(VKMemoryManager& memory_manager, vk::BufferUsageFlags usage);

    /// Increases the amount of watches available.
    void ReserveWatches(std::size_t grow_size);

    const VKDevice& device;                      ///< Vulkan device manager.
    VKScheduler& scheduler;                      ///< Command scheduler.
    const u64 buffer_size;                       ///< Total size of the stream buffer.
    const vk::AccessFlags access;                ///< Access usage of this stream buffer.
    const vk::PipelineStageFlags pipeline_stage; ///< Pipeline usage of this stream buffer.

    UniqueBuffer buffer;   ///< Mapped buffer.
    VKMemoryCommit commit; ///< Memory commit.
    u8* mapped_pointer{};  ///< Pointer to the host visible commit

    u64 offset{};      ///< Buffer iterator.
    u64 mapped_size{}; ///< Size reserved for the current copy.

    std::vector<std::unique_ptr<VKFenceWatch>> watches; ///< Total watches
    std::size_t used_watches{}; ///< Count of watches, reset on invalidation.
    std::optional<std::size_t>
        invalidation_mark{}; ///< Number of watches used in the current invalidation.
};

} // namespace Vulkan