Giter Club home page Giter Club logo

metal's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

metal's Issues

Running in Playgrounds on iPad

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:
B545DD0D-B37C-471E-8D24-F54507CD3E59

But on iPad Pro (11 2018, ios 12.2) it looks like this:
04694315-4287-4D27-B38D-BB1D5BC2BA86

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:

  1. Are the playgrounds supposed to work on iPad?
  2. If so, does the code need adapting or is this a bug with Playgrounds?

Running Metal on playground and Apple silicon

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:

Screenshot 2024-07-02 at 4 23 15โ€ฏPM

Any clue what Apple might possibly have changed this time?

Thank you again

running particle3.playground results a cold restart

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)

Running Playground Chapters from MetalKit in MacOS

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.