Changeset View
Changeset View
Standalone View
Standalone View
source/blender/gpu/metal/mtl_memory.mm
| Show First 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | gpu::MTLBuffer *MTLBufferPool::allocate_with_data(uint64_t size, | ||||
| bool cpu_visible, | bool cpu_visible, | ||||
| const void *data) | const void *data) | ||||
| { | { | ||||
| /* Allocate buffer with default HW-compatible alignment of 256 bytes. | /* Allocate buffer with default HW-compatible alignment of 256 bytes. | ||||
| * See https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf for more. */ | * See https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf for more. */ | ||||
| return this->allocate_aligned_with_data(size, 256, cpu_visible, data); | return this->allocate_aligned_with_data(size, 256, cpu_visible, data); | ||||
| } | } | ||||
| gpu::MTLBuffer *MTLBufferPool::allocate_aligned(uint64_t size, uint alignment, bool cpu_visible) | gpu::MTLBuffer *MTLBufferPool::allocate_aligned(uint64_t size, | ||||
| uint32_t alignment, | |||||
| bool cpu_visible) | |||||
| { | { | ||||
| /* Check not required. Main GPU module usage considered thread-safe. */ | /* Check not required. Main GPU module usage considered thread-safe. */ | ||||
| // BLI_assert(BLI_thread_is_main()); | // BLI_assert(BLI_thread_is_main()); | ||||
| /* Calculate aligned size */ | /* Calculate aligned size */ | ||||
| BLI_assert(alignment > 0); | BLI_assert(alignment > 0); | ||||
| uint64_t aligned_alloc_size = ceil_to_multiple_ul(size, alignment); | uint64_t aligned_alloc_size = ceil_to_multiple_ul(size, alignment); | ||||
| ▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | |||||
| #if MTL_DEBUG_MEMORY_STATISTICS == 1 | #if MTL_DEBUG_MEMORY_STATISTICS == 1 | ||||
| this->per_frame_allocation_count++; | this->per_frame_allocation_count++; | ||||
| #endif | #endif | ||||
| return new_buffer; | return new_buffer; | ||||
| } | } | ||||
| gpu::MTLBuffer *MTLBufferPool::allocate_aligned_with_data(uint64_t size, | gpu::MTLBuffer *MTLBufferPool::allocate_aligned_with_data(uint64_t size, | ||||
| uint alignment, | uint32_t alignment, | ||||
| bool cpu_visible, | bool cpu_visible, | ||||
| const void *data) | const void *data) | ||||
| { | { | ||||
| gpu::MTLBuffer *buf = this->allocate_aligned(size, 256, cpu_visible); | gpu::MTLBuffer *buf = this->allocate_aligned(size, 256, cpu_visible); | ||||
| /* Upload initial data. */ | /* Upload initial data. */ | ||||
| BLI_assert(data != nullptr); | BLI_assert(data != nullptr); | ||||
| BLI_assert(!(buf->get_resource_options() & MTLResourceStorageModePrivate)); | BLI_assert(!(buf->get_resource_options() & MTLResourceStorageModePrivate)); | ||||
| ▲ Show 20 Lines • Show All 364 Lines • ▼ Show 20 Lines | |||||
| void gpu::MTLBuffer::set_label(NSString *str) | void gpu::MTLBuffer::set_label(NSString *str) | ||||
| { | { | ||||
| metal_buffer_.label = str; | metal_buffer_.label = str; | ||||
| } | } | ||||
| void gpu::MTLBuffer::debug_ensure_used() | void gpu::MTLBuffer::debug_ensure_used() | ||||
| { | { | ||||
| /* Debug: If buffer is not flagged as in-use, this is a problem. */ | /* Debug: If buffer is not flagged as in-use, this is a problem. */ | ||||
| BLI_assert(in_use_ && | BLI_assert_msg( | ||||
| in_use_, | |||||
| "Buffer should be marked as 'in-use' if being actively used by an instance. Buffer " | "Buffer should be marked as 'in-use' if being actively used by an instance. Buffer " | ||||
| "has likely already been freed."); | "has likely already been freed."); | ||||
| } | } | ||||
| void gpu::MTLBuffer::flush() | void gpu::MTLBuffer::flush() | ||||
| { | { | ||||
| this->debug_ensure_used(); | this->debug_ensure_used(); | ||||
| if (this->requires_flush()) { | if (this->requires_flush()) { | ||||
| [metal_buffer_ didModifyRange:NSMakeRange(0, size_)]; | [metal_buffer_ didModifyRange:NSMakeRange(0, size_)]; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| MTLTemporaryBuffer MTLScratchBufferManager::scratch_buffer_allocate_range_aligned( | MTLTemporaryBuffer MTLScratchBufferManager::scratch_buffer_allocate_range_aligned( | ||||
| uint64_t alloc_size, uint alignment) | uint64_t alloc_size, uint alignment) | ||||
| { | { | ||||
| /* Ensure scratch buffer allocation alignment adheres to offset alignment requirements. */ | /* Ensure scratch buffer allocation alignment adheres to offset alignment requirements. */ | ||||
| alignment = max_uu(alignment, 256); | alignment = max_uu(alignment, 256); | ||||
| BLI_assert(current_scratch_buffer_ >= 0 && "Scratch Buffer index not set"); | BLI_assert_msg(current_scratch_buffer_ >= 0, "Scratch Buffer index not set"); | ||||
| MTLCircularBuffer *current_scratch_buff = this->scratch_buffers_[current_scratch_buffer_]; | MTLCircularBuffer *current_scratch_buff = this->scratch_buffers_[current_scratch_buffer_]; | ||||
| BLI_assert(current_scratch_buff != nullptr && "Scratch Buffer does not exist"); | BLI_assert_msg(current_scratch_buff != nullptr, "Scratch Buffer does not exist"); | ||||
| MTLTemporaryBuffer allocated_range = current_scratch_buff->allocate_range_aligned(alloc_size, | MTLTemporaryBuffer allocated_range = current_scratch_buff->allocate_range_aligned(alloc_size, | ||||
| alignment); | alignment); | ||||
| BLI_assert(allocated_range.size >= alloc_size && allocated_range.size <= alloc_size + alignment); | BLI_assert(allocated_range.size >= alloc_size && allocated_range.size <= alloc_size + alignment); | ||||
| BLI_assert(allocated_range.metal_buffer != nil); | BLI_assert(allocated_range.metal_buffer != nil); | ||||
| return allocated_range; | return allocated_range; | ||||
| } | } | ||||
| void MTLScratchBufferManager::ensure_increment_scratch_buffer() | void MTLScratchBufferManager::ensure_increment_scratch_buffer() | ||||
| ▲ Show 20 Lines • Show All 217 Lines • Show Last 20 Lines | |||||