Skip to content

Commit

Permalink
added memory
Browse files Browse the repository at this point in the history
  • Loading branch information
mhorga authored May 26, 2017
1 parent 8f79a70 commit 1a715c0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
34 changes: 34 additions & 0 deletions memory_part_2.playground/Contents.swift
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())
4 changes: 4 additions & 0 deletions memory_part_2.playground/contents.xcplayground
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>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

0 comments on commit 1a715c0

Please sign in to comment.