template in metal shading language

Hi,

This is my shader code, it is throwing runtime error during building library. It seems like compilation errors. How to use templates exactly?


template<typename T>
kernel void add(const device T *In1 [[buffer(0)]],
                const device T *In2 [[buffer(1)]],
                device T *Out [[buffer(2)]],
                uint tid [[thread_position_in_grid]]){
        Out[tid] = In1[tid] + In2[tid];
}


Thank you! 🙂

Templates can not be used on functions with kernel/vertex/fragment attributes because template code is compiled when it's instantiated with some new type. Of course, it would be nice to get such functions from library with newFunctionWithName:@"add<someType>", but it will require expensive source code compilation during runtime, which is, actually, what Metal design is avoiding.

template in metal shading language
 
 
Q