I am creating a class that needs differently coded shaders due to varying inputs that are known at initialization. The goal would be that when the object is initialized the shader is also initialized and compiled into a library.
My problem is I have no clue how to write a .metal file to my apps directory like I see in XCode and frankly I am not sure iOS permissions will even let me do that. I think once I have the metal file in place it becomes easy but how?
The shader I hope to edit at compile time will have a variable amount of texture inputs based on the initialization parameters.
You don't write a .metal file. You create a MTLLibrary
object directly from a string containing the shader program text.
You call either the -newLibraryWithSource:options:error:
method or the -newLibraryWithSource:options:completionHandler:
method of the MTLDevice
protocol.
That said, either Matthijs's answer or some other approach may be more appropriate. For example, depending on which versions of iOS and which GPU family you're targeting, you may be able to use texture arrays with function constants to accommodate a variable number of textures. You should show a real example of the sort of shader you're hoping to build and how it may vary.