GithubHelp home page GithubHelp logo

metalbuilder's Introduction

MetalBuilder

iPadOS Swift 5.5 Metal 2.4 Swift Playgrounds 4 License: MIT

Overview

MetalBuilder is an open source Swift Package for declarative dispatching of Metal shaders.

It can extremely simplify your Swift and Metal code, especially if you use Metal in SwiftUI apps.

Also, if you're new to Metal or to GPU programming in general, MetalBuilder will make it easy for you to start.

Since MetalBuilder is purely Swift Package, you can import it in Swift Playgrounds on an iPad.

How to use (WIP)

MetalBuilder is a wrapper that alllows you to manage Metal objects in a functional manner mimicking SwiftUI. E.g., buffers and textures are created using property wrappers like this:

@MetalBuffer<Particle>(count: particlesCount, metalName: "particles") var particlesBuffer
@MetalTexture(textureDescriptor
        .pixelFormat(.rgba16Float)) var texture

To show what you're are rendering you use MetalBuilderView struct just like any other SwiftUI view. It takes the following main parameters: librarySource - a string that contains shaders code, and metalContent - a resultBuilder closure that describes shader dispatch like this:

Compute("integration")
                    .buffer(particlesBuffer, space: "device",
                            fitThreads: true)
Render(vertex: "vertexShader", fragment: "fragmentShader", 
       type: .point, count: particlesCount)
                    .vertexBuf(particlesBuffer)
                    .vertexBytes(context.$viewportSize)
                    .fragBytes($flameType, name: "type")
                    .toTexture(renderTexture)

You can use Binding as a shader parameter. Along with it you can pass a name of the metal property that the binded Swift property would represent in shader code. This allows MetalBuilder to generate automatically most of the declarations of the shader functions at the time the view loads. This considerably reduces the amount of Metal code and makes Metal much more simple to use with SwiftUI, making it approachable even for beginners.

See the Example app to get the idea of how MetalBuilder works.

This is a work in progress! I will add a lot more documentation and examples in the nearest future!

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.