I need to convert some C functions to the GLSL shading language, but there are some limitations of GLSL that make this difficult. It's not possible to use tail-recursive C functions in GLSL, like this one:

int fib(int n) { 
    if (n <= 1) 
        return n; 
    return fib(n-1) + fib(n-2); 

To use this function in GLSL, I would need to rewrite it as a non-recursive function:

int fib(int n)
  int a = 0, b = 1, c, i;
  if (n == 0)
    return a;
  for (i = 2; i <= n; i++)
     c = a + b;
     a = b;
     b = c;
  return b;

Is there any program that can refactor C functions to remove tail recursion, making them compatible with GLSL?

As GLSL shaders are usually cross-platform, it doesn't matter which OS the program runs on. I'm not concerned about the price, but I'd prefer to find an open-source or freeware solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.