metalkit / metal Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
@mhorga is it possible to add a license to this repo please? ๐
I'm not sure if these examples are supposed to run on iPad, but I tried this guide and it worked but with strange results: `http://metalkit.org/2016/10/01/using-metalkit-part-2-3-2.html
On Macbook Pro (15 2015, macos 10.14.3) it looks like this:
But on iPad Pro (11 2018, ios 12.2) it looks like this:
This code was used on both:
let shader =
"#include <metal_stdlib>\n" +
"using namespace metal;" +
"kernel void k(texture2d<float,access::write> 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);" +
"}"
import MetalKit
import PlaygroundSupport
public class MetalView: NSObject, MTKViewDelegate {
weak var view: MTKView!
let commandQueue: MTLCommandQueue!
let device: MTLDevice!
let cps: MTLComputePipelineState!
public init?(mtkView: MTKView, shader: String) {
view = mtkView
view.clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1)
view.colorPixelFormat = .bgra8Unorm
device = MTLCreateSystemDefaultDevice()!
commandQueue = device.makeCommandQueue()
let library = try! device.makeLibrary(source: shader, options: nil)
let function = library.makeFunction(name:"k")!
cps = try! device.makeComputePipelineState(function: function)
super.init()
view.delegate = self
view.device = device
}
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}
public func draw(in view: MTKView) {
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()
}
}
}
let frame = CGRect(x: 0, y: 0, width: 300, height: 300)
let view = MTKView(frame: frame)
let delegate = MetalView(mtkView: view, shader: shader)
view.delegate = delegate
PlaygroundPage.current.liveView = view
Questions:
When you git clone on Mac, all files named by .playground contains nothing.
Thank you for these resources. I really like the introduction on metal and I am following the blogs as well. Running the playgrounds I ran into many issues. Some I could fix but not this one yet.
I suppose the ambient_occlusion playground was working at the time of creation. I don't get any error messages and I followed the fixes for the path of metal files on m1 playgrounds. Still:
Any clue what Apple might possibly have changed this time?
Thank you again
Probably nothing to do with your code, I just wanted to note here, maybe other users are affected as well.
Running particle3.playground results in a cold restart, after about a minute of hard working on my Macbook Pro.
MacBook Pro (Retina, 15-inch, Late 2013)
Intel Iris Pro 1536 MB
macOS 10.13.3 (17D47)
Marius, I've very much enjoyed your MetalKit sequence and learned much from it, so thank you.
From the Chapter 8 code accompanying the blogs onwards where you use playgrounds rather than projects I find that the path search in Metal.swift using guard let path =
never manages to find the Shaders.metal file inside Resources, so the code won't build. Replacing the whole line with an absolute path fixes it, but I am not sure whether this is my setup or something that has changed in Swift 5.3, XCode 12.0 or somewhere else both of which I am using.
All the fixes for this error on SO seem to relate to XCode projects rather than playgrounds, and I haven't found any suggestions about how to fix the problem directly in the playground files and avoid introducing an absolute path.
Thanks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.