Metal fragment function becomes slow

I have a MTLBuffer attached to my fragment shader and data format of this MTLBuffer is an struct which has an array of another struct and a count variable as given below

struct Data{//has some vector variables};

Struct Lights {

Int light_count;

Data light_data[10]; };

I just populate 1 light data in the mtlbuffer means mtlbuffer is not fully populated . So in this case, fragment function becomes too slow if I change array size of light_data to 1 and then fragment shader becomes fast.

Another thing if I populate mtlbuffer fully and access light_data in for loop in my fragment function then again it becomes too slow.

Does anybody have any idea why this problem is there? and please tell me any solution to make fragment function fast ? or should I format my Lights struct in different way so that fragment function can access it fast.

Issue was using metal data types not simd data types in structs and "usage of for loops". now I used simd data types and unrolled "for loops" manually then fragment function works very fast. but why metal compiler is not unrolling for loops automatically? and why is simd data types are faster?

There is always a tradeoff with unrolling loops and register pressure. The more you unroll, the higher the register pressure and the fewer threads you can run. If you want to hint to the compiler what to do, you can use the clang unroll_count pragma to specify how much unrolling to do.

Metal fragment function becomes slow
 
 
Q