1 // Copyright 2016 Dolphin Emulator Project
2 // Licensed under GPLv2+
3 // Refer to the license.txt file included.
4 
5 #pragma once
6 
7 #include <cstddef>
8 #include <deque>
9 #include <memory>
10 #include <utility>
11 
12 #include "Common/CommonTypes.h"
13 #include "VideoBackends/Vulkan/Constants.h"
14 
15 namespace Vulkan
16 {
17 class StreamBuffer
18 {
19 public:
20   StreamBuffer(VkBufferUsageFlags usage, u32 size);
21   ~StreamBuffer();
22 
GetBuffer()23   VkBuffer GetBuffer() const { return m_buffer; }
GetDeviceMemory()24   VkDeviceMemory GetDeviceMemory() const { return m_memory; }
GetHostPointer()25   u8* GetHostPointer() const { return m_host_pointer; }
GetCurrentHostPointer()26   u8* GetCurrentHostPointer() const { return m_host_pointer + m_current_offset; }
GetCurrentSize()27   u32 GetCurrentSize() const { return m_size; }
GetCurrentOffset()28   u32 GetCurrentOffset() const { return m_current_offset; }
29   bool ReserveMemory(u32 num_bytes, u32 alignment);
30   void CommitMemory(u32 final_num_bytes);
31 
32   static std::unique_ptr<StreamBuffer> Create(VkBufferUsageFlags usage, u32 size);
33 
34 private:
35   bool AllocateBuffer();
36   void UpdateCurrentFencePosition();
37   void UpdateGPUPosition();
38 
39   // Waits for as many fences as needed to allocate num_bytes bytes from the buffer.
40   bool WaitForClearSpace(u32 num_bytes);
41 
42   VkBufferUsageFlags m_usage;
43   u32 m_size;
44   u32 m_current_offset = 0;
45   u32 m_current_gpu_position = 0;
46   u32 m_last_allocation_size = 0;
47 
48   VkBuffer m_buffer = VK_NULL_HANDLE;
49   VkDeviceMemory m_memory = VK_NULL_HANDLE;
50   u8* m_host_pointer = nullptr;
51 
52   // List of fences and the corresponding positions in the buffer
53   std::deque<std::pair<u64, u32>> m_tracked_fences;
54 
55   bool m_coherent_mapping = false;
56 };
57 
58 }  // namespace Vulkan
59