forked from EKey2210/metal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
7 changes: 7 additions & 0 deletions
7
memory_part_2.playground/playground.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file added
BIN
+7.34 KB
...round/playground.xcworkspace/xcuserdata/mhorga.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.