17#include <unordered_map>
19#include <Cocoa/Cocoa.h>
20#include <Metal/Metal.h>
21#include <QuartzCore/QuartzCore.h>
24@class MTLCommandQueue;
25@class MTLRenderPipelineState;
94#define MTL_DEBUG_MEMORY_STATISTICS 0
100class MTLCommandBufferManager;
118 id<MTLBuffer> metal_buffer_;
130 bool is_external_ =
false;
133 MTLResourceOptions options_;
134 id<MTLDevice> device_;
145 std::atomic<bool> in_use_;
149 MTLBuffer(id<MTLBuffer> external_buffer);
250 this->buffer_size = this->buffer->
get_size();
251 this->insert_time = std::time(
nullptr);
256 this->buffer =
nullptr;
258 this->insert_time = 0;
301 std::atomic<int> reference_count_;
302 std::atomic<bool> in_free_queue_;
303 std::atomic<bool> referenced_by_workload_;
304 std::recursive_mutex lock_;
306 std::atomic<MTLSafeFreeList *> next_;
315 static const int MAX_NUM_BUFFERS_ = 8192;
316 static const int MIN_BUFFER_FLUSH_COUNT = 120;
317 std::atomic<int> current_list_index_;
338 in_free_queue_ =
true;
339 if (current_list_index_ >= MTLSafeFreeList::MAX_NUM_BUFFERS_) {
367#if MTL_DEBUG_MEMORY_STATISTICS == 1
369 std::atomic<int64_t> total_allocation_bytes_;
372 std::atomic<int> per_frame_allocation_count_;
373 std::atomic<int64_t> buffers_in_pool_;
377 bool initialized_ =
false;
378 id<MTLDevice> device_ = nil;
388 static constexpr float mtl_buffer_size_threshold_factor_ = 1.26;
399 using MTLBufferPoolOrderedList = std::multiset<MTLBufferHandle, CompareMTLBuffer>;
400 using MTLBufferResourceOptions =
uint64_t;
402 std::mutex buffer_pool_lock_;
407 uint allocations_list_size_;
413 std::mutex safelist_lock_;
419 std::atomic<MTLSafeFreeList *> current_free_list_;
420 std::atomic<int64_t> allocations_in_pool_;
426 void init(id<MTLDevice> device);
435 const void *
data =
nullptr);
450 void ensure_buffer_pool(MTLResourceOptions
options);
457 void allocations_list_delete_all();
489 bool initialised_ =
false;
492 uint current_scratch_buffer_ = 0;
BMesh const char void * data
unsigned long long int uint64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
void push_completed_safe_list(MTLSafeFreeList *list)
gpu::MTLBuffer * allocate_with_data(uint64_t size, bool cpu_visible, const void *data=nullptr)
MTLSafeFreeList * get_current_safe_list()
void update_memory_pools()
void begin_new_safe_list()
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 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()
~MTLScratchBufferManager()
MTLTemporaryBuffer scratch_buffer_allocate_range(uint64_t alloc_size)
void bind_as_ssbo(int slot)
CCL_NAMESPACE_BEGIN struct Options options
static int compare_size(void *user_data, const void *a1, const void *a2)
float length(VecOp< float, D >) RET
#define MTL_NUM_SAFE_FRAMES
MTLBufferRange MTLTemporaryBuffer
bool operator()(const MTLBufferHandle &lhs, const MTLBufferHandle &rhs) const
MTLBufferHandle(gpu::MTLBuffer *buf)
MTLBufferHandle(uint64_t compare_size)
MTLResourceOptions options
id< MTLBuffer > metal_buffer