Skip to content

Commit

Permalink
updated to Swift 4
Browse files Browse the repository at this point in the history
  • Loading branch information
mhorga committed Nov 1, 2017
1 parent bc61d4f commit 2b3a0dc
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 0 deletions.
10 changes: 10 additions & 0 deletions particles/particle2.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

import MetalKit
import PlaygroundSupport

let frame = NSRect(x: 0, y: 0, width: 400, height: 400)
let delegate = MetalViewDelegate()
let view = MTKView(frame: frame, device: delegate.device)
view.clearColor = MTLClearColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1)
view.delegate = delegate
PlaygroundPage.current.liveView = view
32 changes: 32 additions & 0 deletions particles/particle2.playground/Resources/Shaders.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

#include <metal_stdlib>
using namespace metal;

struct VertexIn {
float4 position [[attribute(0)]];
};

struct VertexOut {
float4 position [[position]];
float4 color;
};

struct Particle {
float4x4 initial_matrix;
float4x4 matrix;
float4 color;
};

vertex VertexOut vertex_main(const VertexIn vertex_in [[stage_in]],
constant Particle *particles [[buffer(1)]],
uint instanceid [[instance_id]]) {
VertexOut vertex_out;
Particle particle = particles[instanceid];
vertex_out.position = particle.matrix * vertex_in.position ;
vertex_out.color = particle.color;
return vertex_out;
}

fragment float4 fragment_main(VertexOut vertex_in [[stage_in]]) {
return vertex_in.color;
}
98 changes: 98 additions & 0 deletions particles/particle2.playground/Sources/MetalViewDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

import MetalKit

public class MetalViewDelegate: NSObject, MTKViewDelegate {

public var device: MTLDevice!
var queue: MTLCommandQueue!
var pipelineState: MTLRenderPipelineState!
var model: MTKMesh!
var particles: [Particle]!
var particlesBuffer: MTLBuffer!
var timer: Float = 0

struct Particle {
var initialMatrix = matrix_identity_float4x4
var matrix = matrix_identity_float4x4
var color = float4()
}

override public init() {
super.init()
initializeMetal()
}

func initializeBuffers() {
particles = [Particle](repeatElement(Particle(), count: 1000))
particlesBuffer = device.makeBuffer(length: particles.count * MemoryLayout<Particle>.stride, options: [])!
var pointer = particlesBuffer.contents().bindMemory(to: Particle.self, capacity: particles.count)
for _ in particles.enumerated() {
pointer.pointee.initialMatrix = translate(by: [Float(drand48()) / 10, Float(drand48()) * 10, 0])
pointer.pointee.color = float4(0.2, 0.6, 0.9, 1)
pointer = pointer.advanced(by: 1)
}
let allocator = MTKMeshBufferAllocator(device: device)
let sphere = MDLMesh(sphereWithExtent: [0.01, 0.01, 0.01], segments: [8, 8], inwardNormals: false, geometryType: .triangles, allocator: allocator)
do {
model = try MTKMesh(mesh: sphere, device: device)
} catch let e {
Swift.print("\(e)")
}
}

func initializeMetal() {
device = MTLCreateSystemDefaultDevice()
queue = device.makeCommandQueue()
initializeBuffers()
let library: MTLLibrary
do {
let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
let source = try String(contentsOfFile: path!, encoding: .utf8)
library = try device.makeLibrary(source: source, options: nil)
let descriptor = MTLRenderPipelineDescriptor()
descriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
descriptor.vertexFunction = library.makeFunction(name: "vertex_main")
descriptor.fragmentFunction = library.makeFunction(name: "fragment_main")
descriptor.vertexDescriptor = MTKMetalVertexDescriptorFromModelIO(model.vertexDescriptor)
pipelineState = try device.makeRenderPipelineState(descriptor: descriptor)
} catch let error as NSError {
fatalError("library error: " + error.description)
}
}

func translate(by: float3) -> float4x4 {
return float4x4(columns: (
float4( 1, 0, 0, 0),
float4( 0, 1, 0, 0),
float4( 0, 0, 1, 0),
float4( by.x, by.y, by.z, 1)
))
}

func update() {
timer += 0.01
var pointer = particlesBuffer.contents().bindMemory(to: Particle.self, capacity: particles.count)
for _ in particles {
pointer.pointee.matrix = translate(by: [0, -3 * timer, 0]) * pointer.pointee.initialMatrix
pointer = pointer.advanced(by: 1)
}
}

public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) { }

public func draw(in view: MTKView) {
update()
guard let commandBuffer = queue.makeCommandBuffer(),
let descriptor = view.currentRenderPassDescriptor,
let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: descriptor),
let drawable = view.currentDrawable else { fatalError() }
let submesh = model.submeshes[0]
commandEncoder.setRenderPipelineState(pipelineState)
commandEncoder.setVertexBuffer(model.vertexBuffers[0].buffer, offset: 0, index: 0)
commandEncoder.setVertexBuffer(particlesBuffer, offset: 0, index: 1)
commandEncoder.drawIndexedPrimitives(type: .triangle, indexCount: submesh.indexCount, indexType: submesh.indexType, indexBuffer: submesh.indexBuffer.buffer, indexBufferOffset: 0, instanceCount: particles.count)
commandEncoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
}
}
4 changes: 4 additions & 0 deletions particles/particle2.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' executeOnSourceChanges='false'>
<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 2b3a0dc

Please sign in to comment.