added memory
This commit is contained in:
parent
8f79a70bb7
commit
1a715c0bb4
|
|
@ -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())
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||||
|
<playground version='5.0' target-platform='macos'>
|
||||||
|
<timeline fileName='timeline.xctimeline'/>
|
||||||
|
</playground>
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Workspace
|
||||||
|
version = "1.0">
|
||||||
|
<FileRef
|
||||||
|
location = "self:">
|
||||||
|
</FileRef>
|
||||||
|
</Workspace>
|
||||||
Binary file not shown.
Loading…
Reference in New Issue