duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

d3d12_stream_buffer.h (1959B)


      1 // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
      2 // SPDX-License-Identifier: (GPL-3.0 OR PolyForm-Strict-1.0.0)
      3 
      4 #pragma once
      5 
      6 #include "common/types.h"
      7 #include "common/windows_headers.h"
      8 
      9 #include <d3d12.h>
     10 #include <deque>
     11 #include <utility>
     12 #include <wrl/client.h>
     13 
     14 class Error;
     15 
     16 namespace D3D12MA {
     17 class Allocation;
     18 }
     19 
     20 class D3D12StreamBuffer
     21 {
     22 public:
     23   D3D12StreamBuffer();
     24   ~D3D12StreamBuffer();
     25 
     26   bool Create(u32 size, Error* error);
     27 
     28   ALWAYS_INLINE bool IsValid() const { return static_cast<bool>(m_buffer); }
     29   ALWAYS_INLINE ID3D12Resource* GetBuffer() const { return m_buffer.Get(); }
     30   ALWAYS_INLINE D3D12_GPU_VIRTUAL_ADDRESS GetGPUPointer() const { return m_gpu_pointer; }
     31   ALWAYS_INLINE void* GetHostPointer() const { return m_host_pointer; }
     32   ALWAYS_INLINE void* GetCurrentHostPointer() const { return m_host_pointer + m_current_offset; }
     33   ALWAYS_INLINE D3D12_GPU_VIRTUAL_ADDRESS GetCurrentGPUPointer() const { return m_gpu_pointer + m_current_offset; }
     34   ALWAYS_INLINE u32 GetSize() const { return m_size; }
     35   ALWAYS_INLINE u32 GetCurrentOffset() const { return m_current_offset; }
     36   ALWAYS_INLINE u32 GetCurrentSpace() const { return m_current_space; }
     37 
     38   bool ReserveMemory(u32 num_bytes, u32 alignment);
     39   void CommitMemory(u32 final_num_bytes);
     40 
     41   void Destroy(bool defer = true);
     42 
     43 private:
     44   void UpdateCurrentFencePosition();
     45   void UpdateGPUPosition();
     46 
     47   // Waits for as many fences as needed to allocate num_bytes bytes from the buffer.
     48   bool WaitForClearSpace(u32 num_bytes);
     49 
     50   u32 m_size = 0;
     51   u32 m_current_offset = 0;
     52   u32 m_current_space = 0;
     53   u32 m_current_gpu_position = 0;
     54 
     55   Microsoft::WRL::ComPtr<ID3D12Resource> m_buffer;
     56   Microsoft::WRL::ComPtr<D3D12MA::Allocation> m_allocation;
     57   D3D12_GPU_VIRTUAL_ADDRESS m_gpu_pointer = {};
     58   u8* m_host_pointer = nullptr;
     59 
     60   // List of fences and the corresponding positions in the buffer
     61   std::deque<std::pair<u64, u32>> m_tracked_fences;
     62 };