Thank you!
Here's a test SwiftUI View similar to yours, but without the Dynamic Member enum
I encountered the same crash, even though I verified that the passthrough shader existed. However, I decided to test it in Swift Playgrounds instead of Xcode, and surprisingly, it worked! I’m not sure why the shader fails to load in Xcode but runs fine in Swift Playgrounds.
After confirming that it worked in Playgrounds, I reopened the project in Xcode, and the bug reappeared. This makes me fairly certain that the issue stems from differences between Xcode and Swift Playgrounds. Hopefully, this helps others who run into the same problem!
Post
Replies
Boosts
Views
Activity
Small update:
This is my current code:
import SwiftUI
@dynamicMemberLookup
enum MyShaderLibrary {
private static let library: ShaderLibrary = {
let shaderPath = Bundle.main.path(forResource: "Shaders", ofType: "metallib")
let shaderURL = URL(fileURLWithPath: shaderPath!)
let shaderData = try! Data(contentsOf: shaderURL)
let library = ShaderLibrary(data: shaderData)
return library
}()
static subscript(dynamicMember name: String) -> ShaderFunction {
let function = library[dynamicMember: name]
print(function) // Important!
return function
}
}
And I have a passthrough Metal shader:
[[ stitchable ]]
half4 passthrough(float2 position, half4 color) {
return color;
}
When my SwiftUI view calls the shader with:
View()
.colorEffect(MyShaderLibrary.passthrough())
the print function shown above correctly prints this:
ShaderFunction(library: SwiftUI.ShaderLibrary(rbLibrary: <RBShaderLibrary: 0x60000000cf80>), name: "passthrough")
Which tells me that the shader library is being loaded correctly? What else could be going wrong? The code doesn't seem to crash when initializing the ShaderLibrary, but rather, when trying to render the shader?