updated to Swift 4

This commit is contained in:
Marius Horga 2017-10-02 15:16:35 -05:00
parent 3bb25af63a
commit 1b040d7fb2
72 changed files with 408 additions and 204 deletions

View File

@ -1,6 +1,7 @@
# Metal
Repository to accompany the following blog posts:
- [Introducing the Metal framework](http://metalkit.org/2016/01/04/introducing-the-metal-framework.html)
- [Using MetalKit part 1](http://metalkit.org/2016/01/11/using-metalkit-part-1.html)
- [Using MetalKit part 2](http://metalkit.org/2016/01/18/using-metalkit-part-2.html)

View File

@ -38,12 +38,12 @@ public class MetalView: NSObject, MTKViewDelegate {
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()
if let drawable = view.currentDrawable,
let commandBuffer = queue.makeCommandBuffer(),
let commandEncoder = commandBuffer.makeComputeCommandEncoder() {
commandEncoder.setComputePipelineState(cps)
commandEncoder.setTexture(drawable.texture, at: 0)
commandEncoder.setBuffer(timerBuffer, offset: 0, at: 0)
commandEncoder.setTexture(drawable.texture, index: 0)
commandEncoder.setBuffer(timerBuffer, offset: 0, index: 0)
update()
let threadGroupCount = MTLSizeMake(8, 8, 1)
let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1)

View File

@ -93,12 +93,12 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0810;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = "Marius Horga";
TargetAttributes = {
29400C781C3AEE1300345568 = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0800;
LastSwiftMigration = 0900;
};
};
};
@ -163,14 +163,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@ -209,14 +215,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@ -247,7 +259,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter01;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
@ -260,7 +273,8 @@
PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter01;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Release;
};

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>chapter01.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

View File

@ -23,7 +23,7 @@ class ViewController: NSViewController {
}
label.stringValue = "Your system has the following GPU(s):\n"
for device in devices {
label.stringValue += "\(device.name!)\n"
label.stringValue += "\(device.name)\n"
}
}
}

View File

@ -93,12 +93,12 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0810;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = "Marius Horga";
TargetAttributes = {
29E74ACC1C4159D60023A39A = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0800;
LastSwiftMigration = 0900;
};
};
};
@ -163,14 +163,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@ -209,14 +215,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@ -247,7 +259,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter02;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
@ -260,7 +273,8 @@
PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter02;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Release;
};

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>chapter02.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

View File

@ -20,11 +20,11 @@ class MetalView: MTKView {
rpd.colorAttachments[0].texture = currentDrawable!.texture
rpd.colorAttachments[0].clearColor = MTLClearColor(red: 0, green: 0.5, blue: 0.5, alpha: 1)
rpd.colorAttachments[0].loadAction = .clear
let commandBuffer = device!.makeCommandQueue().makeCommandBuffer()
let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd)
commandEncoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
let commandBuffer = device!.makeCommandQueue()?.makeCommandBuffer()
let commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: rpd)
commandEncoder?.endEncoding()
commandBuffer?.present(drawable)
commandBuffer?.commit()
}
}
}

View File

@ -96,12 +96,12 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0810;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = "Marius Horga";
TargetAttributes = {
29CB598B1C4461E90004DB55 = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0800;
LastSwiftMigration = 0900;
};
};
};
@ -167,14 +167,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@ -213,14 +219,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@ -251,7 +263,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter03;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
@ -264,7 +277,8 @@
PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter03;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Release;
};

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>chapter03.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

View File

@ -28,7 +28,7 @@ class MetalView: MTKView {
0.0, 1.0, 0.0, 1.0]
let dataSize = vertexData!.count * MemoryLayout<Float>.size
vertexBuffer = device!.makeBuffer(bytes: vertexData!, length: dataSize, options: [])
let library = device!.newDefaultLibrary()!
let library = device!.makeDefaultLibrary()!
let vertex_func = library.makeFunction(name: "vertex_func")
let frag_func = library.makeFunction(name: "fragment_func")
let rpld = MTLRenderPipelineDescriptor()
@ -38,7 +38,7 @@ class MetalView: MTKView {
do {
try rps = device!.makeRenderPipelineState(descriptor: rpld)
} catch let error {
self.print("\(error)")
self.printView("\(error)")
}
}
@ -46,13 +46,13 @@ class MetalView: MTKView {
if let drawable = currentDrawable, let rpd = currentRenderPassDescriptor {
rpd.colorAttachments[0].clearColor = MTLClearColorMake(0, 0.5, 0.5, 1.0)
let commandBuffer = commandQueue!.makeCommandBuffer()
let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd)
commandEncoder.setRenderPipelineState(rps!)
commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0)
commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1)
commandEncoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
let commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: rpd)
commandEncoder?.setRenderPipelineState(rps!)
commandEncoder?.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
commandEncoder?.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1)
commandEncoder?.endEncoding()
commandBuffer?.present(drawable)
commandBuffer?.commit()
}
}
}

View File

@ -96,12 +96,12 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0810;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = "Marius Horga";
TargetAttributes = {
29C87D3C1C56E93A005F4615 = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0800;
LastSwiftMigration = 0900;
};
};
};
@ -167,14 +167,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@ -213,14 +219,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@ -251,7 +263,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter04;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
@ -264,7 +277,8 @@
PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter04;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Release;
};

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>chapter04.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

View File

@ -35,7 +35,7 @@ class MetalView: MTKView {
}
func registerShaders() {
let library = device!.newDefaultLibrary()!
let library = device!.makeDefaultLibrary()!
let vertex_func = library.makeFunction(name: "vertex_func")
let frag_func = library.makeFunction(name: "fragment_func")
let rpld = MTLRenderPipelineDescriptor()
@ -49,13 +49,13 @@ class MetalView: MTKView {
if let drawable = currentDrawable, let rpd = currentRenderPassDescriptor {
rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0)
let commandBuffer = commandQueue!.makeCommandBuffer()
let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd)
commandEncoder.setRenderPipelineState(rps!)
commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0)
commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1)
commandEncoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
let commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: rpd)
commandEncoder?.setRenderPipelineState(rps!)
commandEncoder?.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
commandEncoder?.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1)
commandEncoder?.endEncoding()
commandBuffer?.present(drawable)
commandBuffer?.commit()
}
}
}

View File

@ -96,12 +96,12 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0810;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = "Marius Horga";
TargetAttributes = {
2980371E1C62437900FBBC6A = {
CreatedOnToolsVersion = 7.2.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 0900;
};
};
};
@ -167,14 +167,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@ -213,14 +219,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@ -253,7 +265,8 @@
PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter05;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
@ -267,7 +280,8 @@
PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter05;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Release;
};

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>chapter05.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

View File

@ -87,7 +87,7 @@ class MetalView: MTKView {
}
func registerShaders() {
let library = device!.newDefaultLibrary()!
let library = device!.makeDefaultLibrary()!
let vertex_func = library.makeFunction(name: "vertex_func")
let frag_func = library.makeFunction(name: "fragment_func")
let rpld = MTLRenderPipelineDescriptor()
@ -97,22 +97,22 @@ class MetalView: MTKView {
do {
try rps = device!.makeRenderPipelineState(descriptor: rpld)
} catch let error {
self.print("\(error)")
self.printView("\(error)")
}
}
override func draw(_ dirtyRect: NSRect) {
if let rpd = currentRenderPassDescriptor, let drawable = currentDrawable {
rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0)
let commandBuffer = device!.makeCommandQueue().makeCommandBuffer()
let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd)
commandEncoder.setRenderPipelineState(rps!)
commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0)
commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, at: 1)
commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1)
commandEncoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
let commandBuffer = device!.makeCommandQueue()?.makeCommandBuffer()
let commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: rpd)
commandEncoder?.setRenderPipelineState(rps!)
commandEncoder?.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
commandEncoder?.setVertexBuffer(uniformBuffer, offset: 0, index: 1)
commandEncoder?.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1)
commandEncoder?.endEncoding()
commandBuffer?.present(drawable)
commandBuffer?.commit()
}
}
}

View File

@ -93,13 +93,14 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0800;
LastUpgradeCheck = 0810;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = Marius;
TargetAttributes = {
2984512B1D47EF08008D81B9 = {
CreatedOnToolsVersion = 8.0;
DevelopmentTeam = C2X9Q6VFVJ;
DevelopmentTeamName = "Marius Horga";
LastSwiftMigration = 0900;
ProvisioningStyle = Automatic;
};
};
@ -166,7 +167,9 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
@ -174,7 +177,11 @@
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@ -197,7 +204,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
@ -216,7 +223,9 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
@ -224,7 +233,11 @@
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@ -241,7 +254,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
@ -260,7 +273,8 @@
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
@ -272,7 +286,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = org.metalkit.chapter06;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Release;
};

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>chapter06.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

View File

@ -29,9 +29,9 @@ class MetalView: UIView {
descriptor.colorAttachments[0].clearColor = MTLClearColorMake(0, 0.5, 0.5, 1)
descriptor.colorAttachments[0].texture = drawable.texture
let commandBuffer = commandQueue.makeCommandBuffer()
let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: descriptor)
commandEncoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
let commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: descriptor)
commandEncoder?.endEncoding()
commandBuffer?.present(drawable)
commandBuffer?.commit()
}
}

View File

@ -99,12 +99,12 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0810;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = "Marius Horga";
TargetAttributes = {
29D878251C84D2B0007B5F17 = {
CreatedOnToolsVersion = 7.2.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 0900;
};
};
};
@ -171,14 +171,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@ -217,14 +223,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@ -255,7 +267,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter07;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
@ -268,7 +281,8 @@
PRODUCT_BUNDLE_IDENTIFIER = org.mhorga.chapter07;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Release;
};

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>chapter07.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

View File

@ -35,7 +35,7 @@ class MetalView: MTKView {
}
func registerShaders() {
let library = device!.newDefaultLibrary()!
let library = device!.makeDefaultLibrary()!
let vertex_func = library.makeFunction(name: "vertex_func")
let frag_func = library.makeFunction(name: "fragment_func")
let rpld = MTLRenderPipelineDescriptor()
@ -45,7 +45,7 @@ class MetalView: MTKView {
do {
try rps = device!.makeRenderPipelineState(descriptor: rpld)
} catch let error {
self.print("\(error)")
self.printView("\(error)")
}
}
@ -53,15 +53,15 @@ class MetalView: MTKView {
super.draw(dirtyRect)
if let rpd = currentRenderPassDescriptor, let drawable = currentDrawable {
rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0)
let commandBuffer = device!.makeCommandQueue().makeCommandBuffer()
let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd)
commandEncoder.setRenderPipelineState(rps!)
commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0)
commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, at: 1)
commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1)
commandEncoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
let commandBuffer = device!.makeCommandQueue()?.makeCommandBuffer()
let commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: rpd)
commandEncoder?.setRenderPipelineState(rps!)
commandEncoder?.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
commandEncoder?.setVertexBuffer(uniformBuffer, offset: 0, index: 1)
commandEncoder?.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1)
commandEncoder?.endEncoding()
commandBuffer?.present(drawable)
commandBuffer?.commit()
}
}
}

View File

@ -52,13 +52,14 @@ public class MetalView: NSObject, MTKViewDelegate {
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}
public func draw(in view: MTKView) {
if let rpd = view.currentRenderPassDescriptor, let drawable = view.currentDrawable {
if let rpd = view.currentRenderPassDescriptor,
let drawable = view.currentDrawable,
let commandBuffer = queue.makeCommandBuffer(),
let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd) {
rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0)
let commandBuffer = queue.makeCommandBuffer()
let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd)
commandEncoder.setRenderPipelineState(rps)
commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0)
commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, at: 1)
commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, index: 1)
commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1)
commandEncoder.endEncoding()
commandBuffer.present(drawable)

View File

@ -65,9 +65,9 @@ public class MetalView: NSObject, MTKViewDelegate {
func update() {
let scaled = scalingMatrix(scale: 0.5)
rotation += 1 / 100 * Float(M_PI) / 4
rotation += 1 / 100 * Float.pi / 4
let rotatedY = rotationMatrix(angle: rotation, axis: float3(0, 1, 0))
let rotatedX = rotationMatrix(angle: Float(M_PI) / 4, axis: float3(1, 0, 0))
let rotatedX = rotationMatrix(angle: Float.pi / 4, axis: float3(1, 0, 0))
let modelMatrix = matrix_multiply(matrix_multiply(rotatedX, rotatedY), scaled)
let cameraPosition = vector_float3(0, 0, -3)
let viewMatrix = translationMatrix(position: cameraPosition)
@ -82,15 +82,16 @@ public class MetalView: NSObject, MTKViewDelegate {
public func draw(in view: MTKView) {
update()
if let rpd = view.currentRenderPassDescriptor, let drawable = view.currentDrawable {
if let rpd = view.currentRenderPassDescriptor,
let drawable = view.currentDrawable,
let commandBuffer = queue.makeCommandBuffer(),
let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd) {
rpd.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0)
let commandBuffer = queue.makeCommandBuffer()
let commandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: rpd)
commandEncoder.setRenderPipelineState(rps)
commandEncoder.setFrontFacing(.counterClockwise)
commandEncoder.setCullMode(.back)
commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0)
commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, at: 1)
commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, index: 1)
commandEncoder.drawIndexedPrimitives(type: .triangle, indexCount: indexBuffer.length / MemoryLayout<UInt16>.size, indexType: MTLIndexType.uint16, indexBuffer: indexBuffer, indexBufferOffset: 0)
commandEncoder.endEncoding()
commandBuffer.present(drawable)

View File

@ -29,11 +29,11 @@ public class MetalView: NSObject, MTKViewDelegate {
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()
if let drawable = view.currentDrawable,
let commandBuffer = queue.makeCommandBuffer(),
let commandEncoder = commandBuffer.makeComputeCommandEncoder() {
commandEncoder.setComputePipelineState(cps)
commandEncoder.setTexture(drawable.texture, at: 0)
commandEncoder.setTexture(drawable.texture, index: 0)
let threadGroupCount = MTLSizeMake(8, 8, 1)
let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1)
commandEncoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount)

View File

@ -29,11 +29,11 @@ public class MetalView: NSObject, MTKViewDelegate {
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()
if let drawable = view.currentDrawable,
let commandBuffer = queue.makeCommandBuffer(),
let commandEncoder = commandBuffer.makeComputeCommandEncoder() {
commandEncoder.setComputePipelineState(cps)
commandEncoder.setTexture(drawable.texture, at: 0)
commandEncoder.setTexture(drawable.texture, index: 0)
let threadGroupCount = MTLSizeMake(8, 8, 1)
let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1)
commandEncoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount)

View File

@ -28,13 +28,13 @@ public class MetalView: MTKView, NSWindowDelegate {
override public func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
if let drawable = currentDrawable {
let commandBuffer = queue.makeCommandBuffer()
let commandEncoder = commandBuffer.makeComputeCommandEncoder()
if let drawable = currentDrawable,
let commandBuffer = queue.makeCommandBuffer(),
let commandEncoder = commandBuffer.makeComputeCommandEncoder() {
commandEncoder.setComputePipelineState(cps)
commandEncoder.setTexture(drawable.texture, at: 0)
commandEncoder.setBuffer(mouseBuffer, offset: 0, at: 2)
commandEncoder.setBuffer(timerBuffer, offset: 0, at: 1)
commandEncoder.setTexture(drawable.texture, index: 0)
commandEncoder.setBuffer(mouseBuffer, offset: 0, index: 2)
commandEncoder.setBuffer(timerBuffer, offset: 0, index: 1)
update()
let threadGroupCount = MTLSizeMake(8, 8, 1)
let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1)

View File

@ -27,13 +27,13 @@ public class MetalView: MTKView, NSWindowDelegate {
}
override public func draw(_ dirtyRect: NSRect) {
if let drawable = currentDrawable {
let commandBuffer = queue.makeCommandBuffer()
let commandEncoder = commandBuffer.makeComputeCommandEncoder()
if let drawable = currentDrawable,
let commandBuffer = queue.makeCommandBuffer(),
let commandEncoder = commandBuffer.makeComputeCommandEncoder() {
commandEncoder.setComputePipelineState(cps)
commandEncoder.setTexture(drawable.texture, at: 0)
commandEncoder.setBuffer(mouseBuffer, offset: 0, at: 2)
commandEncoder.setBuffer(timerBuffer, offset: 0, at: 1)
commandEncoder.setTexture(drawable.texture, index: 0)
commandEncoder.setBuffer(mouseBuffer, offset: 0, index: 2)
commandEncoder.setBuffer(timerBuffer, offset: 0, index: 1)
update()
let threadGroupCount = MTLSizeMake(8, 8, 1)
let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1)

View File

@ -18,12 +18,12 @@ public class MetalView: MTKView, NSWindowDelegate {
}
override public func draw(_ dirtyRect: NSRect) {
if let drawable = currentDrawable {
let commandBuffer = queue.makeCommandBuffer()
let commandEncoder = commandBuffer.makeComputeCommandEncoder()
if let drawable = currentDrawable,
let commandBuffer = queue.makeCommandBuffer(),
let commandEncoder = commandBuffer.makeComputeCommandEncoder() {
commandEncoder.setComputePipelineState(cps)
commandEncoder.setTexture(drawable.texture, at: 0)
commandEncoder.setBuffer(timerBuffer, offset: 0, at: 0)
commandEncoder.setTexture(drawable.texture, index: 0)
commandEncoder.setBuffer(timerBuffer, offset: 0, index: 0)
update()
let threadGroupCount = MTLSizeMake(8, 8, 1)
let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1)

View File

@ -11,15 +11,15 @@ kernel void compute(texture2d<float, access::write> output [[texture(0)]],
int width = input.get_width();
int height = input.get_height();
float2 uv = float2(gid) / float2(width, height);
uv = uv * 2.0 - 1.0;
uv = uv * 2;
uv = uv * 2.0 - 0.5;
uv = uv * 4;
float radius = 1;
float distance = length(uv) - radius;
constexpr sampler textureSampler(coord::normalized,
address::repeat,
min_filter::linear,
mag_filter::linear,
mip_filter::linear );
mip_filter::linear);
float3 norm = float3(uv, sqrt(1.0 - dot(uv, uv)));
float pi = 3.14;
float s = atan2( norm.z, norm.x ) / (2 * pi);

View File

@ -21,7 +21,7 @@ public class MetalView: NSObject, MTKViewDelegate {
func setUpTexture() {
let path = Bundle.main.path(forResource: "texture", ofType: "jpg")
let textureLoader = MTKTextureLoader(device: device!)
texture = try! textureLoader.newTexture(withContentsOf: URL(fileURLWithPath: path!), options: nil)
texture = try! textureLoader.newTexture(URL: URL(fileURLWithPath: path!), options: nil)
}
func registerShaders() {
@ -46,13 +46,13 @@ public class MetalView: NSObject, MTKViewDelegate {
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()
if let drawable = view.currentDrawable,
let commandBuffer = queue.makeCommandBuffer(),
let commandEncoder = commandBuffer.makeComputeCommandEncoder() {
commandEncoder.setComputePipelineState(cps)
commandEncoder.setTexture(drawable.texture, at: 0)
commandEncoder.setTexture(texture, at: 1)
commandEncoder.setBuffer(timerBuffer, offset: 0, at: 0)
commandEncoder.setTexture(drawable.texture, index: 0)
commandEncoder.setTexture(texture, index: 1)
commandEncoder.setBuffer(timerBuffer, offset: 0, index: 0)
update()
let threadGroupCount = MTLSizeMake(8, 8, 1)
let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1)

View File

@ -4,11 +4,11 @@ import MetalKit
public class Render : NSObject, MTKViewDelegate {
weak var view: MTKView!
let commandQueue: MTLCommandQueue
let renderPipelineState: MTLRenderPipelineState
let device: MTLDevice
var vertexBuffer: MTLBuffer
var indexBuffer: MTLBuffer
let commandQueue: MTLCommandQueue!
let renderPipelineState: MTLRenderPipelineState!
let device: MTLDevice!
var vertexBuffer: MTLBuffer!
var indexBuffer: MTLBuffer!
struct Vertex {
var position: float4
@ -19,9 +19,9 @@ public class Render : NSObject, MTKViewDelegate {
view = mtkView
view.clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1)
view.colorPixelFormat = .bgra8Unorm
device = MTLCreateSystemDefaultDevice()!
device = MTLCreateSystemDefaultDevice()
commandQueue = device.makeCommandQueue()
renderPipelineState = try! Render.buildRenderPipelineWithDevice(device: device, view: mtkView)
try! renderPipelineState = Render.buildRenderPipelineWithDevice(device: device, view: mtkView)
var vertices = [Vertex]()
vertices.append(Vertex(position: float4(-0.5, -0.5, 0, 1), color: float4(1, 0, 0, 1)))
@ -42,9 +42,9 @@ public class Render : NSObject, MTKViewDelegate {
}
class func buildRenderPipelineWithDevice(device: MTLDevice, view: MTKView) throws -> MTLRenderPipelineState {
let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")!
let input = try! String(contentsOfFile: path, encoding: String.Encoding.utf8)
let library = try! device.makeLibrary(source: input, options: nil)
guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { fatalError() }
let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
let library = try device.makeLibrary(source: input, options: nil)
let vertexFunction = library.makeFunction(name: "vertex_transform")
let fragmentFunction = library.makeFunction(name: "fragment_lighting")
let pipelineDescriptor = MTLRenderPipelineDescriptor()
@ -58,16 +58,17 @@ public class Render : NSObject, MTKViewDelegate {
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}
public func draw(in view: MTKView) {
let commandBuffer = commandQueue.makeCommandBuffer()
let renderPassDescriptor = view.currentRenderPassDescriptor!
let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor)
renderEncoder.setVertexBuffer(vertexBuffer, offset:0, at:0)
renderEncoder.setRenderPipelineState(renderPipelineState)
renderEncoder.setTriangleFillMode(.lines)
renderEncoder.drawIndexedPrimitives(type: .triangle, indexCount: 3, indexType: .uint16, indexBuffer: indexBuffer,indexBufferOffset: 0)
renderEncoder.endEncoding()
commandBuffer.present(view.currentDrawable!)
commandBuffer.commit()
if let commandBuffer = commandQueue.makeCommandBuffer(),
let renderPassDescriptor = view.currentRenderPassDescriptor,
let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor) {
renderEncoder.setVertexBuffer(vertexBuffer, offset:0, index:0)
renderEncoder.setRenderPipelineState(renderPipelineState)
renderEncoder.setTriangleFillMode(.lines)
renderEncoder.drawIndexedPrimitives(type: .triangle, indexCount: 3, indexType: .uint16, indexBuffer: indexBuffer,indexBufferOffset: 0)
renderEncoder.endEncoding()
commandBuffer.present(view.currentDrawable!)
commandBuffer.commit()
}
}
}

View File

@ -3,9 +3,9 @@ import MetalKit
public class MetalView: NSObject, MTKViewDelegate {
weak var view: MTKView!
let commandQueue: MTLCommandQueue
let device: MTLDevice
let cps: MTLComputePipelineState
let commandQueue: MTLCommandQueue!
let device: MTLDevice!
let cps: MTLComputePipelineState!
public init?(mtkView: MTKView, shader: String) {
view = mtkView
@ -25,16 +25,17 @@ public class MetalView: NSObject, MTKViewDelegate {
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}
public func draw(in view: MTKView) {
let drawable = view.currentDrawable!
let commandBuffer = commandQueue.makeCommandBuffer()
let commandEncoder = commandBuffer.makeComputeCommandEncoder()
commandEncoder.setComputePipelineState(cps)
commandEncoder.setTexture(drawable.texture, at: 0)
let groups = MTLSize(width: Int(view.frame.width)/4, height: Int(view.frame.height)/4, depth: 1)
let threads = MTLSize(width: 8, height: 8,depth: 1)
commandEncoder.dispatchThreadgroups(groups,threadsPerThreadgroup: threads)
commandEncoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
if let drawable = view.currentDrawable,
let commandBuffer = commandQueue.makeCommandBuffer(),
let commandEncoder = commandBuffer.makeComputeCommandEncoder() {
commandEncoder.setComputePipelineState(cps)
commandEncoder.setTexture(drawable.texture, index: 0)
let groups = MTLSize(width: Int(view.frame.width)/4, height: Int(view.frame.height)/4, depth: 1)
let threads = MTLSize(width: 8, height: 8,depth: 1)
commandEncoder.dispatchThreadgroups(groups,threadsPerThreadgroup: threads)
commandEncoder.endEncoding()
commandBuffer.present(drawable)
commandBuffer.commit()
}
}
}

View File

@ -1,14 +1,14 @@
import MetalKit
let device = MTLCreateSystemDefaultDevice()!
let queue = device.makeCommandQueue()
guard let device = MTLCreateSystemDefaultDevice() else { fatalError() }
guard let queue = device.makeCommandQueue() else { fatalError() }
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) }
print(length)
guard let outBuffer = device.makeBuffer(bytes: myVector, length: length, options: []) else { fatalError() }
for (index, _) 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")
@ -17,11 +17,11 @@ 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()
guard let commandBuffer = queue.makeCommandBuffer() else { fatalError() }
guard let encoder = commandBuffer.makeComputeCommandEncoder() else { fatalError() }
encoder.setComputePipelineState(computePipelineState)
encoder.setBuffer(inBuffer, offset: 0, at: 0)
encoder.setBuffer(outBuffer, offset: 0, at: 1)
encoder.setBuffer(inBuffer, offset: 0, index: 0)
encoder.setBuffer(outBuffer, offset: 0, index: 1)
let size = MTLSize(width: count, height: 1, depth: 1)
encoder.dispatchThreadgroups(size, threadsPerThreadgroup: size)
encoder.endEncoding()

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos'>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>

View File

@ -3,7 +3,7 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=2&amp;CharacterRangeLoc=1415&amp;EndingColumnNumber=14&amp;EndingLineNumber=32&amp;StartingColumnNumber=12&amp;StartingLineNumber=32&amp;Timestamp=515338097.885755"
documentLocation = "file:///Users/mhorga/Desktop/metal/memory/memory_1.playground#CharacterRangeLen=2&amp;CharacterRangeLoc=1554&amp;EndingColumnNumber=14&amp;EndingLineNumber=32&amp;StartingColumnNumber=12&amp;StartingLineNumber=32&amp;Timestamp=528665184.878259"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>

View File

@ -5,16 +5,17 @@ guard let device = MTLCreateSystemDefaultDevice() else { fatalError() }
let count = 2000
let length = count * MemoryLayout< Float >.stride
var myBuffer: MTLBuffer!
// 1. makeBuffer(length:)
//
//let myBuffer = device.makeBuffer(length: length, options: [])
//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: [])
//myBuffer = device.makeBuffer(bytes: myVector, length: length, options: [])
//withUnsafePointer(to: &myVector) { print($0) }
//print(myBuffer.contents())
@ -24,11 +25,11 @@ 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)
})
myBuffer = device.makeBuffer(bytesNoCopy: memory!,
length: allocationSize,
options: [],
deallocator: { (pointer: UnsafeMutableRawPointer, _: Int) in
free(pointer)
})
print(memory!)
print(myBuffer.contents())
print(myBuffer!.contents())

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos'>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>

View File

@ -3,7 +3,7 @@ import Metal
let devices = MTLCopyAllDevices()
devices[0].name!
devices[0].name
devices[0].areRasterOrderGroupsSupported
devices[0].isDepth24Stencil8PixelFormatSupported
devices[0].isLowPower
@ -12,7 +12,7 @@ devices[0].maxThreadgroupMemoryLength
devices[0].supportsFeatureSet(MTLFeatureSet.macOS_GPUFamily1_v3)
devices[0].supportsFeatureSet(MTLFeatureSet.macOS_ReadWriteTextureTier2)
devices[1].name!
devices[1].name
devices[1].areRasterOrderGroupsSupported
devices[1].isDepth24Stencil8PixelFormatSupported
devices[1].isLowPower

View File

@ -3,7 +3,7 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/marius/Desktop/metal2.playground#CharacterRangeLen=0&amp;CharacterRangeLoc=0&amp;EndingColumnNumber=0&amp;EndingLineNumber=0&amp;StartingColumnNumber=0&amp;StartingLineNumber=0&amp;Timestamp=520575694.983919"
documentLocation = "file:///Users/mhorga/Desktop/metal/metal2/metal2.playground#CharacterRangeLen=0&amp;CharacterRangeLoc=0&amp;EndingColumnNumber=0&amp;EndingLineNumber=0&amp;StartingColumnNumber=0&amp;StartingLineNumber=0&amp;Timestamp=528667753.96448"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>

View File

@ -27,4 +27,4 @@ kernel void compute(texture2d<float, access::write> output [[texture(0)]],
float4 color = float4(1, 0.7, 0, 1);
if (distance > 0) { color = float4(0.2, 0.5, 0.7, 1); }
output.write(float4(color), gid);
}
}

View File

@ -55,3 +55,4 @@ public class MetalView: NSObject, MTKViewDelegate {
}
}
}

View File

@ -32,12 +32,12 @@ public class MetalView: NSObject, MTKViewDelegate {
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()
if let drawable = view.currentDrawable,
let commandBuffer = queue.makeCommandBuffer(),
let commandEncoder = commandBuffer.makeComputeCommandEncoder() {
commandEncoder.setComputePipelineState(cps)
commandEncoder.setTexture(drawable.texture, at: 0)
commandEncoder.setBuffer(timeBuffer, offset: 0, at: 0)
commandEncoder.setTexture(drawable.texture, index: 0)
commandEncoder.setBuffer(timeBuffer, offset: 0, index: 0)
time += 0.01
let bufferPointer = timeBuffer.contents()
memcpy(bufferPointer, &time, MemoryLayout<Float>.size)

View File

@ -38,12 +38,12 @@ public class MetalView: NSObject, MTKViewDelegate {
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()
if let drawable = view.currentDrawable,
let commandBuffer = queue.makeCommandBuffer(),
let commandEncoder = commandBuffer.makeComputeCommandEncoder() {
commandEncoder.setComputePipelineState(cps)
commandEncoder.setTexture(drawable.texture, at: 0)
commandEncoder.setBuffer(timerBuffer, offset: 0, at: 0)
commandEncoder.setTexture(drawable.texture, index: 0)
commandEncoder.setBuffer(timerBuffer, offset: 0, index: 0)
update()
let threadGroupCount = MTLSizeMake(8, 8, 1)
let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1)

View File

@ -38,12 +38,12 @@ public class MetalView: NSObject, MTKViewDelegate {
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()
if let drawable = view.currentDrawable,
let commandBuffer = queue.makeCommandBuffer(),
let commandEncoder = commandBuffer.makeComputeCommandEncoder() {
commandEncoder.setComputePipelineState(cps)
commandEncoder.setTexture(drawable.texture, at: 0)
commandEncoder.setBuffer(timerBuffer, offset: 0, at: 0)
commandEncoder.setTexture(drawable.texture, index: 0)
commandEncoder.setBuffer(timerBuffer, offset: 0, index: 0)
update()
let threadGroupCount = MTLSizeMake(8, 8, 1)
let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1)