21#include <unordered_map>
23#include <Cocoa/Cocoa.h>
24#include <Metal/Metal.h>
25#include <QuartzCore/QuartzCore.h>
28@class MTLCommandQueue;
29@class MTLRenderPipelineState;
98#define MTL_DEBUG_MEMORY_STATISTICS 0
104class MTLCommandBufferManager;
134 bool is_external_ =
false;
137 MTLResourceOptions options_;
149 std::atomic<bool> in_use_;
254 this->buffer_size = this->buffer->
get_size();
255 this->insert_time = std::time(
nullptr);
260 this->buffer =
nullptr;
261 this->buffer_size = compare_size;
262 this->insert_time = 0;
305 std::atomic<int> reference_count_;
306 std::atomic<bool> in_free_queue_;
307 std::atomic<bool> referenced_by_workload_;
308 std::recursive_mutex lock_;
310 std::atomic<MTLSafeFreeList *> next_;
314 std::atomic<MTLSafeFreeList *> next_list_ =
nullptr;
324 static const int MAX_NUM_BUFFERS_ = 8192;
325 static const int MIN_BUFFER_FLUSH_COUNT = 120;
326 static const uint64_t MIN_FLUSH_SIZE = 256 * 1024 * 1024;
327 std::atomic<int> current_list_index_;
328 std::atomic<uint64_t> freed_size_ = 0;
349 in_free_queue_ =
true;
350 if (current_list_index_ >= MTLSafeFreeList::MAX_NUM_BUFFERS_) {
378#if MTL_DEBUG_MEMORY_STATISTICS == 1
380 std::atomic<int64_t> total_allocation_bytes_;
383 std::atomic<int> per_frame_allocation_count_;
384 std::atomic<int64_t> buffers_in_pool_;
388 bool initialized_ =
false;
399 static constexpr float mtl_buffer_size_threshold_factor_ = 1.26;
410 using MTLBufferPoolOrderedList = std::multiset<MTLBufferHandle, CompareMTLBuffer>;
411 using MTLBufferResourceOptions =
uint64_t;
413 std::mutex buffer_pool_lock_;
418 uint allocations_list_size_;
424 std::mutex safelist_lock_;
430 std::atomic<MTLSafeFreeList *> current_free_list_;
431 std::atomic<int64_t> allocations_in_pool_;
446 const void *
data =
nullptr);
465 void ensure_buffer_pool(MTLResourceOptions
options);
472 void allocations_list_delete_all();
504 bool initialised_ =
false;
507 uint current_scratch_buffer_ = 0;
void push_completed_safe_list(MTLSafeFreeList *list)
bool has_completed_safe_lists()
void init(id< MTLDevice > device)
gpu::MTLBuffer * allocate_with_data(uint64_t size, bool cpu_visible, const void *data=nullptr)
MTLSafeFreeList * acquire_current_safe_list()
MTLSafeFreeList * get_current_safe_list()
void update_memory_pools()
gpu::MTLBuffer * allocate(uint64_t size, bool cpu_visible)
gpu::MTLBuffer * allocate_aligned(uint64_t size, uint alignment, bool cpu_visible)
gpu::MTLBuffer * allocate_aligned_with_data(uint64_t size, uint alignment, bool cpu_visible, const void *data=nullptr)
bool free_buffer(gpu::MTLBuffer *buffer)
void begin_new_safe_list(bool only_if_should_flush=false)
void flag_in_use(bool used)
uint64_t get_size() const
void set_usage_size(uint64_t size_used)
uint64_t get_size_used() const
void * get_host_ptr() const
void flush_range(uint64_t offset, uint64_t length)
MTLBuffer(id< MTLDevice > device, uint64_t size, MTLResourceOptions options, uint alignment=1)
void set_label(NSString *str)
MTLResourceOptions get_resource_options()
id< MTLBuffer > get_metal_buffer() const
MEM_CXX_CLASS_ALLOC_FUNCS("MTLBuffer")
MTLCircularBuffer(MTLContext &ctx, uint64_t initial_size, bool allow_grow)
MTLTemporaryBuffer allocate_range_aligned(uint64_t alloc_size, uint alignment)
friend class MTLScratchBufferManager
MTLTemporaryBuffer allocate_range(uint64_t alloc_size)
void increment_reference()
void decrement_reference()
MEM_CXX_CLASS_ALLOC_FUNCS("MTLSafeFreeList")
void insert_buffer(gpu::MTLBuffer *buffer)
friend class MTLBufferPool
static constexpr uint mtl_scratch_buffer_max_size_
static constexpr uint mtl_scratch_buffer_initial_size_
MEM_CXX_CLASS_ALLOC_FUNCS("MTLBufferPool")
MTLScratchBufferManager(MTLContext &context)
MTLTemporaryBuffer scratch_buffer_allocate_range_aligned(uint64_t alloc_size, uint alignment)
void flush_active_scratch_buffer()
void ensure_increment_scratch_buffer()
MTLTemporaryBuffer scratch_buffer_allocate_range(uint64_t alloc_size)
void bind_as_ssbo(int slot)
CCL_NAMESPACE_BEGIN struct Options options
float length(VecOp< float, D >) RET
#define MTL_NUM_SAFE_FRAMES
void free(Subdiv *subdiv)
MTLBufferRange MTLTemporaryBuffer
unsigned __int64 uint64_t
BMesh const char void * data
bool operator()(const MTLBufferHandle &lhs, const MTLBufferHandle &rhs) const
MTLBufferHandle(gpu::MTLBuffer *buf)
MTLBufferHandle(uint64_t compare_size)
MTLResourceOptions options
id< MTLBuffer > metal_buffer