Reputation: 1552
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.
Upvotes: 0
Views: 1074
Reputation: 90521
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.
Upvotes: 2
Reputation: 7892
Please look into the concept of "function constants".
You can define a value as a function_constant
in your .metal file. Then when you create the function using the MTLLibrary
you can pass in the actual value for these constants and Metal will compile a specialized version of your shader on-the-fly using these constant values.
Upvotes: 2