diff --git a/ambient_occlusion/ao.playground/Resources/Shaders.metal b/ambient_occlusion/ao.playground/Resources/Shaders.txt similarity index 100% rename from ambient_occlusion/ao.playground/Resources/Shaders.metal rename to ambient_occlusion/ao.playground/Resources/Shaders.txt diff --git a/ambient_occlusion/ao.playground/Sources/MetalView.swift b/ambient_occlusion/ao.playground/Sources/MetalView.swift index d4bd02e..f0115a2 100644 --- a/ambient_occlusion/ao.playground/Sources/MetalView.swift +++ b/ambient_occlusion/ao.playground/Sources/MetalView.swift @@ -17,7 +17,7 @@ public class MetalView: NSObject, MTKViewDelegate { } func registerShaders() { - guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { return } + guard let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") else { return } do { let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8) let library = try device.makeLibrary(source: input, options: nil) diff --git a/ambient_occlusion/ao.playground/contents.xcplayground b/ambient_occlusion/ao.playground/contents.xcplayground index a93d484..63b6dd8 100644 --- a/ambient_occlusion/ao.playground/contents.xcplayground +++ b/ambient_occlusion/ao.playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/ch08/chapter08.playground/Resources/Shaders.metal b/ch08/chapter08.playground/Resources/Shaders.txt similarity index 100% rename from ch08/chapter08.playground/Resources/Shaders.metal rename to ch08/chapter08.playground/Resources/Shaders.txt diff --git a/ch08/chapter08.playground/Sources/MetalView.swift b/ch08/chapter08.playground/Sources/MetalView.swift index 0423b27..345a544 100644 --- a/ch08/chapter08.playground/Sources/MetalView.swift +++ b/ch08/chapter08.playground/Sources/MetalView.swift @@ -29,7 +29,7 @@ public class MetalView: NSObject, MTKViewDelegate { } func registerShaders() { - let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") + let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") let input: String? let library: MTLLibrary let vert_func: MTLFunction diff --git a/ch09/chapter09.playground/Resources/Shaders.metal b/ch09/chapter09.playground/Resources/Shaders.txt similarity index 100% rename from ch09/chapter09.playground/Resources/Shaders.metal rename to ch09/chapter09.playground/Resources/Shaders.txt diff --git a/ch09/chapter09.playground/Sources/MetalView.swift b/ch09/chapter09.playground/Sources/MetalView.swift index b4b69ac..dee66c0 100755 --- a/ch09/chapter09.playground/Sources/MetalView.swift +++ b/ch09/chapter09.playground/Sources/MetalView.swift @@ -43,7 +43,7 @@ public class MetalView: NSObject, MTKViewDelegate { } func createPipeline() { - let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") + let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") let input: String? let library: MTLLibrary let vert_func: MTLFunction diff --git a/ch10/chapter10.playground/Resources/Shaders.metal b/ch10/chapter10.playground/Resources/Shaders.txt similarity index 100% rename from ch10/chapter10.playground/Resources/Shaders.metal rename to ch10/chapter10.playground/Resources/Shaders.txt diff --git a/ch10/chapter10.playground/Sources/MetalView.swift b/ch10/chapter10.playground/Sources/MetalView.swift index 082a3f9..7da0980 100755 --- a/ch10/chapter10.playground/Sources/MetalView.swift +++ b/ch10/chapter10.playground/Sources/MetalView.swift @@ -15,7 +15,7 @@ public class MetalView: NSObject, MTKViewDelegate { func registerShaders() { device = MTLCreateSystemDefaultDevice()! queue = device.makeCommandQueue() - let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") + let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") do { let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8) let library = try device.makeLibrary(source: input, options: nil) diff --git a/ch11/chapter11.playground/Resources/Shaders.metal b/ch11/chapter11.playground/Resources/Shaders.txt similarity index 100% rename from ch11/chapter11.playground/Resources/Shaders.metal rename to ch11/chapter11.playground/Resources/Shaders.txt diff --git a/ch11/chapter11.playground/Sources/MetalView.swift b/ch11/chapter11.playground/Sources/MetalView.swift index 411a03e..9bcbe68 100755 --- a/ch11/chapter11.playground/Sources/MetalView.swift +++ b/ch11/chapter11.playground/Sources/MetalView.swift @@ -15,7 +15,7 @@ public class MetalView: NSObject, MTKViewDelegate { func registerShaders() { device = MTLCreateSystemDefaultDevice()! queue = device.makeCommandQueue() - let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") + let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") do { let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8) let library = try device.makeLibrary(source: input, options: nil) diff --git a/ch12/chapter12.playground/Resources/Shaders.metal b/ch12/chapter12.playground/Resources/Shaders.txt similarity index 100% rename from ch12/chapter12.playground/Resources/Shaders.metal rename to ch12/chapter12.playground/Resources/Shaders.txt diff --git a/ch12/chapter12.playground/Sources/MetalView.swift b/ch12/chapter12.playground/Sources/MetalView.swift index dfdae7d..a3eacf5 100755 --- a/ch12/chapter12.playground/Sources/MetalView.swift +++ b/ch12/chapter12.playground/Sources/MetalView.swift @@ -56,7 +56,7 @@ public class MetalView: MTKView, NSWindowDelegate { func registerShaders() { queue = device!.makeCommandQueue() - let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") + let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") do { let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8) let library = try device!.makeLibrary(source: input, options: nil) diff --git a/ch13/chapter13.playground/Resources/Shaders.metal b/ch13/chapter13.playground/Resources/Shaders.txt similarity index 100% rename from ch13/chapter13.playground/Resources/Shaders.metal rename to ch13/chapter13.playground/Resources/Shaders.txt diff --git a/ch13/chapter13.playground/Sources/MetalView.swift b/ch13/chapter13.playground/Sources/MetalView.swift index d1595ab..594fe34 100755 --- a/ch13/chapter13.playground/Sources/MetalView.swift +++ b/ch13/chapter13.playground/Sources/MetalView.swift @@ -55,7 +55,7 @@ public class MetalView: MTKView, NSWindowDelegate { func registerShaders() { queue = device!.makeCommandQueue() - let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") + let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") do { let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8) let library = try device!.makeLibrary(source: input, options: nil) diff --git a/ch14/chapter14.playground/Resources/Shaders.metal b/ch14/chapter14.playground/Resources/Shaders.txt similarity index 100% rename from ch14/chapter14.playground/Resources/Shaders.metal rename to ch14/chapter14.playground/Resources/Shaders.txt diff --git a/ch14/chapter14.playground/Sources/MetalView.swift b/ch14/chapter14.playground/Sources/MetalView.swift index e66e923..e7847a1 100755 --- a/ch14/chapter14.playground/Sources/MetalView.swift +++ b/ch14/chapter14.playground/Sources/MetalView.swift @@ -43,7 +43,7 @@ public class MetalView: MTKView, NSWindowDelegate { func registerShaders() { queue = device!.makeCommandQueue() - let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") + let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") do { let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8) let library = try device!.makeLibrary(source: input, options: nil) diff --git a/ch15/chapter15.playground/Resources/Shaders.metal b/ch15/chapter15.playground/Resources/Shaders.txt similarity index 100% rename from ch15/chapter15.playground/Resources/Shaders.metal rename to ch15/chapter15.playground/Resources/Shaders.txt diff --git a/ch15/chapter15.playground/Sources/MetalView.swift b/ch15/chapter15.playground/Sources/MetalView.swift index b2081e0..e9f37cc 100755 --- a/ch15/chapter15.playground/Sources/MetalView.swift +++ b/ch15/chapter15.playground/Sources/MetalView.swift @@ -25,7 +25,7 @@ public class MetalView: NSObject, MTKViewDelegate { } func registerShaders() { - let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") + let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") do { let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8) let library = try device!.makeLibrary(source: input, options: nil) diff --git a/ch17/chapter17.playground/Resources/Shaders.metal b/ch17/chapter17.playground/Resources/Shaders.txt similarity index 100% rename from ch17/chapter17.playground/Resources/Shaders.metal rename to ch17/chapter17.playground/Resources/Shaders.txt diff --git a/ch17/chapter17.playground/Sources/Render.swift b/ch17/chapter17.playground/Sources/Render.swift index e5a6d48..a85ffd9 100644 --- a/ch17/chapter17.playground/Sources/Render.swift +++ b/ch17/chapter17.playground/Sources/Render.swift @@ -42,7 +42,7 @@ public class Render : NSObject, MTKViewDelegate { } class func buildRenderPipelineWithDevice(device: MTLDevice, view: MTKView) throws -> MTLRenderPipelineState { - guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { fatalError() } + guard let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") 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") @@ -54,7 +54,7 @@ public class Render : NSObject, MTKViewDelegate { pipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm return try device.makeRenderPipelineState(descriptor: pipelineDescriptor) } - + public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {} public func draw(in view: MTKView) { diff --git a/ch18/chapter18.playground/Resources/Shaders.txt b/ch18/chapter18.playground/Resources/Shaders.txt new file mode 100644 index 0000000..0835d9c --- /dev/null +++ b/ch18/chapter18.playground/Resources/Shaders.txt @@ -0,0 +1,16 @@ + +#include +using namespace metal; +kernel void k(texture2d o[[texture(0)]], + uint2 gid[[thread_position_in_grid]]) { + int width = o.get_width(); + int height = o.get_height(); + float2 uv = float2(gid) / float2(width, height); + float3 color = mix(float3(1.0, 0.6, 0.1), float3(0.5, 0.8, 1.0), sqrt(1 - uv.y)); + float2 q = uv - float2(0.67, 0.25); + float r = 0.2 + 0.1 * cos(atan2(q.x, q.y) * 9.0 + 20.0 * q.x); + color *= smoothstep(r, r + 0.01, length(q)); + r = 0.03 + 0.002 * cos(120.0 * q.y) + exp(-50.0 * (1.0 - uv.y)); + color *= 1.0 - (1.0 - smoothstep(r, r + 0.002, abs(q.x - 0.25 * sin(2.0 * q.y)))) * smoothstep(0.0, 0.1, q.y); + o.write(float4(color, 1.0), gid); +} diff --git a/ch18/chapter18.playground/Sources/MetalView.swift b/ch18/chapter18.playground/Sources/MetalView.swift index 8c8373b..120f22a 100644 --- a/ch18/chapter18.playground/Sources/MetalView.swift +++ b/ch18/chapter18.playground/Sources/MetalView.swift @@ -13,7 +13,9 @@ public class MetalView: NSObject, MTKViewDelegate { view.colorPixelFormat = .bgra8Unorm device = MTLCreateSystemDefaultDevice()! commandQueue = device.makeCommandQueue() - let library = try! device.makeLibrary(source: shader, options: nil) + guard let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") else { fatalError() } + let input = try! String(contentsOfFile: path, encoding: String.Encoding.utf8) + let library = try! device.makeLibrary(source: input, options: nil) let function = library.makeFunction(name:"k")! cps = try! device.makeComputePipelineState(function: function) diff --git a/compute/compute.playground/Resources/Shaders.metal b/compute/compute.playground/Resources/Shaders.txt similarity index 100% rename from compute/compute.playground/Resources/Shaders.metal rename to compute/compute.playground/Resources/Shaders.txt diff --git a/compute/compute.playground/Sources/MetalView.swift b/compute/compute.playground/Sources/MetalView.swift index 6ba430a..39ab8a6 100644 --- a/compute/compute.playground/Sources/MetalView.swift +++ b/compute/compute.playground/Sources/MetalView.swift @@ -18,7 +18,7 @@ public class Renderer: NSObject, MTKViewDelegate { queue = device.makeCommandQueue() let textureLoader = MTKTextureLoader(device: device) let url = Bundle.main.url(forResource: "nature", withExtension: "jpg")! - guard let file = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { return } + guard let file = Bundle.main.path(forResource: "Shaders", ofType: "txt") else { return } do { let source = try String(contentsOfFile: file, encoding: String.Encoding.utf8) let library = try device.makeLibrary(source: source, options: nil) diff --git a/compute/compute.playground/contents.xcplayground b/compute/compute.playground/contents.xcplayground index b478c1d..faac376 100644 --- a/compute/compute.playground/contents.xcplayground +++ b/compute/compute.playground/contents.xcplayground @@ -1,2 +1,2 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/memory/memory_1.playground/Contents.swift b/memory/memory_1.playground/Contents.swift index 5096ea6..24400f5 100644 --- a/memory/memory_1.playground/Contents.swift +++ b/memory/memory_1.playground/Contents.swift @@ -11,7 +11,7 @@ guard let outBuffer = device.makeBuffer(bytes: myVector, length: length, options 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") +let path = Bundle.main.path(forResource: "memory", ofType: "txt") 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")! diff --git a/memory/memory_1.playground/Resources/memory.metal b/memory/memory_1.playground/Resources/memory.txt similarity index 100% rename from memory/memory_1.playground/Resources/memory.metal rename to memory/memory_1.playground/Resources/memory.txt diff --git a/memory/memory_1.playground/contents.xcplayground b/memory/memory_1.playground/contents.xcplayground index a93d484..63b6dd8 100644 --- a/memory/memory_1.playground/contents.xcplayground +++ b/memory/memory_1.playground/contents.xcplayground @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/memory/memory_1.playground/timeline.xctimeline b/memory/memory_1.playground/timeline.xctimeline index 8a9c7b3..eb4ad70 100644 --- a/memory/memory_1.playground/timeline.xctimeline +++ b/memory/memory_1.playground/timeline.xctimeline @@ -3,7 +3,7 @@ version = "3.0"> diff --git a/particles/particle.playground/Resources/Shaders.metal b/particles/particle.playground/Resources/Shaders.txt similarity index 100% rename from particles/particle.playground/Resources/Shaders.metal rename to particles/particle.playground/Resources/Shaders.txt diff --git a/particles/particle.playground/Sources/MetalView.swift b/particles/particle.playground/Sources/MetalView.swift index 4c786f5..e116a8e 100644 --- a/particles/particle.playground/Sources/MetalView.swift +++ b/particles/particle.playground/Sources/MetalView.swift @@ -17,7 +17,7 @@ public class MetalView: NSObject, MTKViewDelegate { } func registerShaders() { - guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { return } + guard let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") else { return } do { let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8) let library = try device.makeLibrary(source: input, options: nil) diff --git a/particles/particle2.playground/Resources/Shaders.metal b/particles/particle2.playground/Resources/Shaders.txt similarity index 100% rename from particles/particle2.playground/Resources/Shaders.metal rename to particles/particle2.playground/Resources/Shaders.txt diff --git a/particles/particle2.playground/Sources/MetalViewDelegate.swift b/particles/particle2.playground/Sources/MetalViewDelegate.swift index 4ae81f5..9676a56 100644 --- a/particles/particle2.playground/Sources/MetalViewDelegate.swift +++ b/particles/particle2.playground/Sources/MetalViewDelegate.swift @@ -43,7 +43,7 @@ public class MetalViewDelegate: NSObject, MTKViewDelegate { initializeBuffers() let library: MTLLibrary do { - let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") + let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") let source = try String(contentsOfFile: path!, encoding: .utf8) library = try device.makeLibrary(source: source, options: nil) let descriptor = MTLRenderPipelineDescriptor() diff --git a/particles/particle3.playground/Resources/Shaders.metal b/particles/particle3.playground/Resources/Shaders.txt similarity index 100% rename from particles/particle3.playground/Resources/Shaders.metal rename to particles/particle3.playground/Resources/Shaders.txt diff --git a/particles/particle3.playground/Sources/MetalView.swift b/particles/particle3.playground/Sources/MetalView.swift index fc1f7b6..95f853e 100644 --- a/particles/particle3.playground/Sources/MetalView.swift +++ b/particles/particle3.playground/Sources/MetalView.swift @@ -35,7 +35,7 @@ public class MetalView: NSObject, MTKViewDelegate { func initializeMetal() { device = MTLCreateSystemDefaultDevice() queue = device.makeCommandQueue() - guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { return } + guard let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") else { return } do { let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8) let library = try device.makeLibrary(source: input, options: nil) diff --git a/raymarching/raymarching.playground/Resources/Shaders.metal b/raymarching/raymarching.playground/Resources/Shaders.txt similarity index 100% rename from raymarching/raymarching.playground/Resources/Shaders.metal rename to raymarching/raymarching.playground/Resources/Shaders.txt diff --git a/raymarching/raymarching.playground/Sources/MetalView.swift b/raymarching/raymarching.playground/Sources/MetalView.swift index 4e93127..ee995e3 100755 --- a/raymarching/raymarching.playground/Sources/MetalView.swift +++ b/raymarching/raymarching.playground/Sources/MetalView.swift @@ -17,7 +17,7 @@ public class MetalView: NSObject, MTKViewDelegate { func registerShaders() { device = MTLCreateSystemDefaultDevice()! queue = device.makeCommandQueue() - let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") + let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") do { let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8) let library = try device.makeLibrary(source: input, options: nil) diff --git a/shadows/shadows.playground/Resources/Shaders.metal b/shadows/shadows.playground/Resources/Shaders.txt similarity index 100% rename from shadows/shadows.playground/Resources/Shaders.metal rename to shadows/shadows.playground/Resources/Shaders.txt diff --git a/shadows/shadows.playground/Sources/MetalView.swift b/shadows/shadows.playground/Sources/MetalView.swift index 6ffd9d2..3344444 100755 --- a/shadows/shadows.playground/Sources/MetalView.swift +++ b/shadows/shadows.playground/Sources/MetalView.swift @@ -17,7 +17,7 @@ public class MetalView: NSObject, MTKViewDelegate { } func registerShaders() { - guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { return } + guard let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") else { return } do { let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8) let library = try device.makeLibrary(source: input, options: nil) diff --git a/shadows/shadows2.playground/Resources/Shaders.metal b/shadows/shadows2.playground/Resources/Shaders.txt similarity index 100% rename from shadows/shadows2.playground/Resources/Shaders.metal rename to shadows/shadows2.playground/Resources/Shaders.txt diff --git a/shadows/shadows2.playground/Sources/MetalView.swift b/shadows/shadows2.playground/Sources/MetalView.swift index d4bd02e..f0115a2 100644 --- a/shadows/shadows2.playground/Sources/MetalView.swift +++ b/shadows/shadows2.playground/Sources/MetalView.swift @@ -17,7 +17,7 @@ public class MetalView: NSObject, MTKViewDelegate { } func registerShaders() { - guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { return } + guard let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") else { return } do { let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8) let library = try device.makeLibrary(source: input, options: nil)