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
139 additions
and
3 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
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,9 @@ | ||
|
||
import MetalKit | ||
import PlaygroundSupport | ||
|
||
let frame = NSRect(x: 0, y: 0, width: 600, height: 600) | ||
let mView = MetalView() | ||
let view = MTKView(frame: frame, device: mView.device) | ||
view.delegate = mView | ||
PlaygroundPage.current.liveView = view |
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,35 @@ | ||
|
||
#include <metal_stdlib> | ||
using namespace metal; | ||
|
||
struct Particle { | ||
float2 position; | ||
float2 velocity; | ||
}; | ||
|
||
kernel void firstPass(texture2d<half, access::write> output [[texture(0)]], | ||
uint2 id [[thread_position_in_grid]]) { | ||
output.write(half4(0., 0., 0., 1.), id); | ||
} | ||
|
||
kernel void secondPass(texture2d<half, access::write> output [[texture(0)]], | ||
device Particle *particles [[buffer(0)]], | ||
uint id [[thread_position_in_grid]]) { | ||
Particle particle = particles[id]; | ||
float2 position = particle.position; | ||
float2 velocity = particle.velocity; | ||
int width = output.get_width(); | ||
int height = output.get_height(); | ||
if (position.x < 0 || position.x > width) { velocity.x *= -1; } | ||
if (position.y < 0 || position.y > height) { velocity.y *= -1; } | ||
position += velocity; | ||
particle.position = position; | ||
particle.velocity = velocity; | ||
particles[id] = particle; | ||
uint2 pos = uint2(position.x, position.y); | ||
output.write(half4(1.), pos); | ||
output.write(half4(1.), pos + uint2( 1, 0)); | ||
output.write(half4(1.), pos + uint2( 0, 1)); | ||
output.write(half4(1.), pos - uint2( 1, 0)); | ||
output.write(half4(1.), pos - uint2( 0, 1)); | ||
} |
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,74 @@ | ||
|
||
import MetalKit | ||
|
||
struct Particle { | ||
var position: float2 | ||
var velocity: float2 | ||
} | ||
|
||
public class MetalView: NSObject, MTKViewDelegate { | ||
|
||
public var device: MTLDevice! | ||
var queue: MTLCommandQueue! | ||
var firstState: MTLComputePipelineState! | ||
var secondState: MTLComputePipelineState! | ||
var particleBuffer: MTLBuffer! | ||
let particleCount = 10000 | ||
var particles = [Particle]() | ||
let side = 1200 | ||
|
||
override public init() { | ||
super.init() | ||
initializeMetal() | ||
initializeBuffers() | ||
} | ||
|
||
func initializeBuffers() { | ||
for _ in 0 ..< particleCount { | ||
let particle = Particle(position: float2(Float(arc4random() % UInt32(side)), Float(arc4random() % UInt32(side))), velocity: float2((Float(arc4random() % 10) - 5) / 10, (Float(arc4random() % 10) - 5) / 10)) | ||
particles.append(particle) | ||
} | ||
let size = particles.count * MemoryLayout<Particle>.size | ||
particleBuffer = device.makeBuffer(bytes: &particles, length: size, options: []) | ||
} | ||
|
||
func initializeMetal() { | ||
device = MTLCreateSystemDefaultDevice() | ||
queue = device.makeCommandQueue() | ||
guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { return } | ||
do { | ||
let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8) | ||
let library = try device.makeLibrary(source: input, options: nil) | ||
guard let firstPass = library.makeFunction(name: "firstPass") else { return } | ||
firstState = try device.makeComputePipelineState(function: firstPass) | ||
guard let secondPass = library.makeFunction(name: "secondPass") else { return } | ||
secondState = try device.makeComputePipelineState(function: secondPass) | ||
} catch let e { print(e) } | ||
} | ||
|
||
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {} | ||
|
||
public func draw(in view: MTKView) { | ||
if let drawable = view.currentDrawable, | ||
let commandBuffer = queue.makeCommandBuffer(), | ||
let commandEncoder = commandBuffer.makeComputeCommandEncoder() { | ||
// first pass | ||
commandEncoder.setComputePipelineState(firstState) | ||
commandEncoder.setTexture(drawable.texture, index: 0) | ||
let w = firstState.threadExecutionWidth | ||
let h = firstState.maxTotalThreadsPerThreadgroup / w | ||
let threadsPerGroup = MTLSizeMake(w, h, 1) | ||
var threadsPerGrid = MTLSizeMake(side, side, 1) | ||
commandEncoder.dispatchThreads(threadsPerGrid, threadsPerThreadgroup: threadsPerGroup) | ||
// second pass | ||
commandEncoder.setComputePipelineState(secondState) | ||
commandEncoder.setTexture(drawable.texture, index: 0) | ||
commandEncoder.setBuffer(particleBuffer, offset: 0, index: 0) | ||
threadsPerGrid = MTLSizeMake(particleCount, 1, 1) | ||
commandEncoder.dispatchThreads(threadsPerGrid, threadsPerThreadgroup: threadsPerGroup) | ||
commandEncoder.endEncoding() | ||
commandBuffer.present(drawable) | ||
commandBuffer.commit() | ||
} | ||
} | ||
} |
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' executeOnSourceChanges='false'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
7 changes: 7 additions & 0 deletions
7
particles/particle3.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
+30.6 KB
...round/playground.xcworkspace/xcuserdata/marius.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
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,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Timeline | ||
version = "3.0"> | ||
<TimelineItems> | ||
</TimelineItems> | ||
</Timeline> |