Skip to content

Fire submission #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Roman Gaditskii/MetalBuilder fire.swiftpm.zip
Binary file not shown.
26 changes: 26 additions & 0 deletions Roman Gaditskii/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

Hi! Here's my submission.
I made it on my iPad in Swift Playgrounds app.

Though it's not pure SwiftUI and contains some Metal code, it is integrated into SwiftUI view and uses declarative approach for dispatching shaders.
I use my own open source package 'MetalBuilder' for this.
It's a wrapper that alllows 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
```
The Metal view is created just like any other SwiftUI view and takes 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)
```
As you can see, the wrapper can take 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, allowing MetalBuilder to generate automatically most of the declarations of the shader function at the time the view loads. This considerably reduces the amount of Metal code that should be written and makes Metal much more simple to use with SwiftUI, making it approachable even for beginners.
Binary file added Roman Gaditskii/fire.mov
Binary file not shown.