“展开”递归函数?

问题描述

| 我正在用C ++写一个路径跟踪器,我想尝试将最耗资源的代码实现到CUDA或OpenCL中(我不确定该选择哪一个)。 我听说我的图形卡版本的CUDA不支持递归,这是我的路径跟踪器大量使用的东西。 正如我用Python和C ++编写的代码一样,为了便于阅读,我将发布一些简化的Python代码:
def Trace(ray):
  hit = what_object_is_hit(ray)

  if not hit:
    return Color(0,0)

  newRay = hit.bouceChildRayOffSurface(ray)

  return hit.diffuse * (Trace(newRay) + hit.emittance)
我尝试手动展开功能,并且有一个确定的模式(
d
diffuse
e
emittance
):
Level 1:  d1 * e1 

Level 2:  d1 * d2 * e2
        + e1

Level 3:  d1 * d2 * d3 * e3
        + d1 * d2 * e2
        + e1

Level 4:  d1 * d2 * d3 * d4 * e4
        + d1 * d2 * d3 * e3
        + d1 * d2 * e2
        + e1
不过,我可能错了。 我的问题是,我将如何在
while
循环中实现此代码? 我在考虑使用这种格式的东西:
total = Color(0,0)
n = 1

while n < 10:   # Maximum recursion depth
  result = magical_function()

  if not result:  break

  total += result
  n += 1
我以前从未真正处理过解开递归函数的任务,因此将不胜感激。谢谢!     

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)