10 #include <unordered_map>
14 #include <Cocoa/Cocoa.h>
15 #include <Metal/Metal.h>
16 #include <QuartzCore/QuartzCore.h>
19 @class MTLCommandQueue;
20 @class MTLRenderPipelineState;
89 #define MTL_DEBUG_MEMORY_STATISTICS 0
93 #define MTL_SCRATCH_BUFFER_ALLOW_TEMPORARY_EXPANSION 1
99 class MTLCommandBufferManager;
111 id<MTLBuffer> metal_buffer_;
123 bool is_external_ =
false;
126 MTLResourceOptions options_;
127 id<MTLDevice> device_;
138 std::atomic<bool> in_use_;
142 MTLBuffer(id<MTLBuffer> external_buffer);
238 this->buffer_size = this->buffer->
get_size();
243 this->buffer =
nullptr;
286 std::atomic<int> reference_count_;
287 std::atomic<bool> in_free_queue_;
288 std::recursive_mutex lock_;
291 std::atomic<int> has_next_pool_;
292 std::atomic<MTLSafeFreeList *> next_;
296 static const int MAX_NUM_BUFFERS_ = 1024;
297 std::atomic<int> current_list_index_;
316 in_free_queue_ =
true;
317 if (has_next_pool_) {
343 long long int total_allocation_bytes_ = 0;
345 #if MTL_DEBUG_MEMORY_STATISTICS == 1
347 std::atomic<int> per_frame_allocation_count_;
348 std::atomic<long long int> allocations_in_pool_;
349 std::atomic<long long int> buffers_in_pool_;
353 bool ensure_initialised_ =
false;
354 id<MTLDevice> device_ = nil;
364 static constexpr
float mtl_buffer_size_threshold_factor_ = 1.26;
372 using MTLBufferPoolOrderedList = std::multiset<MTLBufferHandle, CompareMTLBuffer>;
373 using MTLBufferResourceOptions =
uint64_t;
388 std::atomic<MTLSafeFreeList *> current_free_list_;
391 void init(id<MTLDevice> device);
400 const void *
data =
nullptr);
415 void ensure_buffer_pool(MTLResourceOptions
options);
449 bool initialised_ =
false;
452 uint current_scratch_buffer_ = 0;
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
void push_completed_safe_list(MTLSafeFreeList *list)
void init(id< MTLDevice > device)
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)
id< MTLBuffer > get_metal_buffer() const
void flush_range(uint64_t offset, uint64_t length)
void set_usage_size(uint64_t size_used)
MTLResourceOptions get_resource_options()
void * get_host_ptr() const
MTLBuffer(id< MTLDevice > device, uint64_t size, MTLResourceOptions options, uint alignment=1)
uint64_t get_size_used() const
uint64_t get_size() const
void set_label(NSString *str)
MTLCircularBuffer(MTLContext &ctx, uint64_t initial_size, bool allow_grow)
MTLTemporaryBuffer allocate_range_aligned(uint64_t alloc_size, uint alignment)
MTLTemporaryBuffer allocate_range(uint64_t alloc_size)
void increment_reference()
void decrement_reference()
void insert_buffer(gpu::MTLBuffer *buffer)
static constexpr uint mtl_scratch_buffer_max_size_
static constexpr uint mtl_scratch_buffer_initial_size_
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)
CCL_NAMESPACE_BEGIN struct Options options
static int compare_size(void *user_data, const void *a1, const void *a2)
ccl_global float * buffer
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
#define MTL_NUM_SAFE_FRAMES
T length(const vec_base< T, Size > &a)
unsigned __int64 uint64_t
bool operator()(const MTLBufferHandle &lhs, const MTLBufferHandle &rhs) const
MTLBufferHandle(gpu::MTLBuffer *buf)
MTLBufferHandle(uint64_t compare_size)
MTLResourceOptions options
id< MTLBuffer > metal_buffer