diff --git a/memory_part_2.playground/Contents.swift b/memory_part_2.playground/Contents.swift new file mode 100644 index 0000000..8c3ecbb --- /dev/null +++ b/memory_part_2.playground/Contents.swift @@ -0,0 +1,34 @@ + +import MetalKit + +guard let device = MTLCreateSystemDefaultDevice() else { fatalError() } + +let count = 2000 +let length = count * MemoryLayout< Float >.stride + +// 1. makeBuffer(length:) +// +//let myBuffer = device.makeBuffer(length: length, options: []) +//print(myBuffer.contents()) + +// 2. makeBuffer(bytes:) +// +//var myVector = [Float](repeating: 0, count: count) +//let myBuffer = device.makeBuffer(bytes: myVector, length: length, options: []) +//withUnsafePointer(to: &myVector) { print($0) } +//print(myBuffer.contents()) + +// 3. makeBuffer(bytesNoCopy:) +// +var memory: UnsafeMutableRawPointer? = nil +let alignment = 0x1000 +let allocationSize = (length + alignment - 1) & (~(alignment - 1)) +posix_memalign(&memory, alignment, allocationSize) +let myBuffer = device.makeBuffer(bytesNoCopy: memory!, + length: allocationSize, + options: [], + deallocator: { (pointer: UnsafeMutableRawPointer, _: Int) in + free(pointer) + }) +print(memory!) +print(myBuffer.contents()) diff --git a/memory_part_2.playground/contents.xcplayground b/memory_part_2.playground/contents.xcplayground new file mode 100644 index 0000000..63b6dd8 --- /dev/null +++ b/memory_part_2.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/memory_part_2.playground/playground.xcworkspace/contents.xcworkspacedata b/memory_part_2.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/memory_part_2.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/memory_part_2.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate b/memory_part_2.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..3835f78 Binary files /dev/null and b/memory_part_2.playground/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate differ