Metal Shaders at Runtime

Hi,

I have a shader that needs one line of code inserted at runtime, based on a choice made by the user.

I can do this using the .newLibraryWithSource( ... method on my MTLDevice.

In order to do this, I load the contents of my .metal file into a String, and then pass that String as an argument to newLibrary WithSource, after making the appropriate modification.

The problem is that this requires runtime compilation of an entire Metal library. It doesn't seem to have much of a negative impact on performance because it does not need to be done repeatedly; however, it seems very cumbersome from the perspective of code management.

Is there any way to merge Metal Libraries? If there is, then I could convert the line that needs to be changed to a function, and minimize the amount of code that needs to be compiled at runtime.

Thank you.


How many potential options could a kernel have based upon and UI condition?

For instance if there is a menu that a user selects requiring your app to change the code, there will be a finite number of paths possible. In this case, it's probably best to just conditionalize the different paths using a function constant and build the pipeline differently based on the user selection. Or you could just build all versions of the pipeline up front and select the one corresponding to the user's selection.

If you have a infinite possibilities of different shaders/kernels based upon user input then you can build a dynamic library generated by the user's options and link it in to a calling pipeline with most of your code. For instance, if your app has a source editor allowing your user to type virtually any Metal code, you could put the code into a function and build just a dynamic library.
Metal Shaders at Runtime
 
 
Q