Changeset View
Changeset View
Standalone View
Standalone View
source/blender/gpu/metal/mtl_memory.hh
| Show First 20 Lines • Show All 279 Lines • ▼ Show 20 Lines | |||||
| * cost of memory allocation. | * cost of memory allocation. | ||||
| */ | */ | ||||
| class MTLSafeFreeList { | class MTLSafeFreeList { | ||||
| friend class MTLBufferPool; | friend class MTLBufferPool; | ||||
| private: | private: | ||||
| std::atomic<int> reference_count_; | std::atomic<int> reference_count_; | ||||
| std::atomic<bool> in_free_queue_; | std::atomic<bool> in_free_queue_; | ||||
| std::atomic<bool> referenced_by_workload_; | |||||
| std::recursive_mutex lock_; | std::recursive_mutex lock_; | ||||
| /* Linked list of next MTLSafeFreeList chunk if current chunk is full. */ | /* Linked list of next MTLSafeFreeList chunk if current chunk is full. */ | ||||
| std::atomic<int> has_next_pool_; | std::atomic<int> has_next_pool_; | ||||
| std::atomic<MTLSafeFreeList *> next_; | std::atomic<MTLSafeFreeList *> next_; | ||||
| /* Lockless list. MAX_NUM_BUFFERS_ within a chunk based on considerations | /* Lockless list. MAX_NUM_BUFFERS_ within a chunk based on considerations | ||||
| * for performance and memory. */ | * for performance and memory. | ||||
| * MIN_BUFFER_FLUSH_COUNT refers to the minimum count of buffers in the MTLSafeFreeList | |||||
| * before buffers are returned to global memory pool. This is set at a point to reduce | |||||
| * overhead of small pool flushes, while ensuring floating memory overhead is not excessive. */ | |||||
| static const int MAX_NUM_BUFFERS_ = 1024; | static const int MAX_NUM_BUFFERS_ = 1024; | ||||
| static const int MIN_BUFFER_FLUSH_COUNT = 120; | |||||
| std::atomic<int> current_list_index_; | std::atomic<int> current_list_index_; | ||||
| gpu::MTLBuffer *safe_free_pool_[MAX_NUM_BUFFERS_]; | gpu::MTLBuffer *safe_free_pool_[MAX_NUM_BUFFERS_]; | ||||
| public: | public: | ||||
| MTLSafeFreeList(); | MTLSafeFreeList(); | ||||
| /* Add buffer to Safe Free List, can be called from secondary threads. | /* Add buffer to Safe Free List, can be called from secondary threads. | ||||
| * Performs a lockless list insert. */ | * Performs a lockless list insert. */ | ||||
| void insert_buffer(gpu::MTLBuffer *buffer); | void insert_buffer(gpu::MTLBuffer *buffer); | ||||
| /* Whether we need ot start a new safe free list, or can carry on using the existing one. */ | |||||
| bool should_flush(); | |||||
| /* Increments command buffer reference count. */ | /* Increments command buffer reference count. */ | ||||
| void increment_reference(); | void increment_reference(); | ||||
| /* Decrement and return of buffers to pool occur on MTLCommandBuffer completion callback thread. | /* Decrement and return of buffers to pool occur on MTLCommandBuffer completion callback. */ | ||||
| */ | |||||
| void decrement_reference(); | void decrement_reference(); | ||||
| void flag_in_queue() | void flag_in_queue() | ||||
| { | { | ||||
| in_free_queue_ = true; | in_free_queue_ = true; | ||||
| if (has_next_pool_) { | if (has_next_pool_) { | ||||
| MTLSafeFreeList *next_pool = next_.load(); | MTLSafeFreeList *next_pool = next_.load(); | ||||
| BLI_assert(next_pool != nullptr); | BLI_assert(next_pool != nullptr); | ||||
| ▲ Show 20 Lines • Show All 167 Lines • Show Last 20 Lines | |||||