forked from MetalKit/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
8 changed files
with
40,314 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,50 @@ | ||
|
||
import MetalKit | ||
|
||
let device = MTLCreateSystemDefaultDevice()! | ||
let queue = device.makeCommandQueue() | ||
let count = 1500 | ||
var myVector = [Float](repeating: 0, count: count) | ||
var length = count * MemoryLayout<Float>.size | ||
//print(length) | ||
var outBuffer = device.makeBuffer(bytes: myVector, length: length, options: []) | ||
for (index, value) in myVector.enumerated() { myVector[index] = Float(index) } | ||
var inBuffer = device.makeBuffer(bytes: myVector, length: length, options: []) | ||
|
||
let path = Bundle.main.path(forResource: "memory", ofType: "metal") | ||
let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8) | ||
let library = try device.makeLibrary(source: input, options: nil) | ||
let function = library.makeFunction(name: "compute")! | ||
let computePipelineState = try! device.makeComputePipelineState(function: function) | ||
|
||
let commandBuffer = queue.makeCommandBuffer() | ||
let encoder = commandBuffer.makeComputeCommandEncoder() | ||
encoder.setComputePipelineState(computePipelineState) | ||
encoder.setBuffer(inBuffer, offset: 0, at: 0) | ||
encoder.setBuffer(outBuffer, offset: 0, at: 1) | ||
let size = MTLSize(width: count, height: 1, depth: 1) | ||
encoder.dispatchThreadgroups(size, threadsPerThreadgroup: size) | ||
encoder.endEncoding() | ||
commandBuffer.commit() | ||
|
||
let result = outBuffer.contents().bindMemory(to: Float.self, capacity: count) | ||
var data = [Float](repeating:0, count: count) | ||
for i in 0 ..< count { data[i] = result[i] } | ||
data.map { $0 } | ||
|
||
import ModelIO | ||
|
||
guard let url = Bundle.main.url(forResource: "teapot", withExtension: "obj") else { fatalError() } | ||
let asset = MDLAsset(url: url) | ||
let voxelArray = MDLVoxelArray(asset: asset, divisions: 10, patchRadius: 0) | ||
if let data = voxelArray.voxelIndices() { | ||
data.withUnsafeBytes { (voxels: UnsafePointer<MDLVoxelIndex>) -> Void in | ||
let count = data.count / MemoryLayout<MDLVoxelIndex>.size | ||
var voxelIndex = voxels | ||
for _ in 0..<count { | ||
let position = voxelArray.spatialLocation(ofIndex: voxelIndex.pointee) | ||
print(position) | ||
voxelIndex = voxelIndex.successor() | ||
} | ||
} | ||
} |
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,10 @@ | ||
|
||
#include <metal_stdlib> | ||
using namespace metal; | ||
|
||
kernel void compute(const device float *inVector [[ buffer(0) ]], | ||
device float *outVector [[ buffer(1) ]], | ||
uint id [[ thread_position_in_grid ]]) | ||
{ | ||
outVector[id] = 1.0 / (1.0 + exp(-inVector[id])); | ||
} |
Oops, something went wrong.