vulkan_stream_buffer.h (1935B)
1 // SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com> 2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) 3 4 #pragma once 5 6 #include "vulkan_loader.h" 7 8 #include "common/types.h" 9 10 #include <deque> 11 #include <memory> 12 13 class VulkanStreamBuffer 14 { 15 public: 16 VulkanStreamBuffer(); 17 VulkanStreamBuffer(VulkanStreamBuffer&& move); 18 VulkanStreamBuffer(const VulkanStreamBuffer&) = delete; 19 ~VulkanStreamBuffer(); 20 21 VulkanStreamBuffer& operator=(VulkanStreamBuffer&& move); 22 VulkanStreamBuffer& operator=(const VulkanStreamBuffer&) = delete; 23 24 ALWAYS_INLINE bool IsValid() const { return (m_buffer != VK_NULL_HANDLE); } 25 ALWAYS_INLINE VkBuffer GetBuffer() const { return m_buffer; } 26 ALWAYS_INLINE const VkBuffer* GetBufferPtr() const { return &m_buffer; } 27 ALWAYS_INLINE u8* GetHostPointer() const { return m_host_pointer; } 28 ALWAYS_INLINE u8* GetCurrentHostPointer() const { return m_host_pointer + m_current_offset; } 29 ALWAYS_INLINE u32 GetCurrentSize() const { return m_size; } 30 ALWAYS_INLINE u32 GetCurrentSpace() const { return m_current_space; } 31 ALWAYS_INLINE u32 GetCurrentOffset() const { return m_current_offset; } 32 33 bool Create(VkBufferUsageFlags usage, u32 size); 34 void Destroy(bool defer); 35 36 bool ReserveMemory(u32 num_bytes, u32 alignment); 37 void CommitMemory(u32 final_num_bytes); 38 39 private: 40 bool AllocateBuffer(VkBufferUsageFlags usage, u32 size); 41 void UpdateCurrentFencePosition(); 42 void UpdateGPUPosition(); 43 44 // Waits for as many fences as needed to allocate num_bytes bytes from the buffer. 45 bool WaitForClearSpace(u32 num_bytes); 46 47 u32 m_size = 0; 48 u32 m_current_offset = 0; 49 u32 m_current_space = 0; 50 u32 m_current_gpu_position = 0; 51 52 VmaAllocation m_allocation = VK_NULL_HANDLE; 53 VkBuffer m_buffer = VK_NULL_HANDLE; 54 u8* m_host_pointer = nullptr; 55 56 // List of fences and the corresponding positions in the buffer 57 std::deque<std::pair<u64, u32>> m_tracked_fences; 58 };