【问题标题】:Is this ray tracing function that runs on the GPU, GPU safe?这个在GPU上运行的光线追踪功能,GPU安全吗?
【发布时间】:2021-04-12 07:58:15
【问题描述】:

我尝试在片段着色器中编写一个简单的光线追踪器。 我有这个函数应该创建一个漫射球体,如下所示:

这里是函数:

vec3 GetRayColor(Ray ray)
{
Ray new_ray = ray;
vec3 FinalColor = vec3(1.0f);

bool IntersectionFound = false;
int hit_times = 0;

for (int i = 0; i < RAY_BOUNCE_LIMIT; i++)
{
    RayHitRecord ClosestSphere = IntersectSceneSpheres(new_ray, 0.001f, MAX_RAY_HIT_DISTANCE);

    if (ClosestSphere.Hit == true)
    {
        // Get the final ray direction

        vec3 R;
        R.x = nextFloat(RNG_SEED, -1.0f, 1.0f); 
        R.y = nextFloat(RNG_SEED, -1.0f, 1.0f);
        R.z = nextFloat(RNG_SEED, -1.0f, 1.0f);

        vec3 S = normalize(ClosestSphere.Normal) + normalize(R);
        S = normalize(S);

        new_ray.Origin = ClosestSphere.Point;
        new_ray.Direction = S;

        hit_times += 1;
        IntersectionFound = true;
    }

    else
    {
        FinalColor = GetGradientColorAtRay(new_ray);
        break;
    }
}

if (IntersectionFound)
{
    FinalColor /= 2.0f; // Lambertian diffuse only absorbs half the light
    FinalColor = FinalColor / float(hit_times);
}

return FinalColor;
}

由于某种原因,hit_times 似乎是不变的。 这段完全相同的代码在 CPU 上运行并生成了所附的屏幕截图。

我不确定这是否与 GPU 有关。但我已经测试了所有其他功能,它们按预期工作。

  1. 法线很好且相同
  2. 随机数生成器工作正常并已可视化

这是Normal + RandomVecS 的可视化:

CPU上完成时完全相同。

这是 CPU

上可视化的hit_times

但在 GPU 上,所有三个球体都是白色的。 这是完整的片段着色器:https://pastebin.com/3xeA6LtT 这是在 CPU 上工作的代码:https://pastebin.com/eyLnHYzr

【问题讨论】:

  • 要实现光线追踪,您需要compute shader
  • 我似乎在片段着色器上多次运行
  • 你有没有尝试简化你的着色器,看看有什么变化?
  • 您确定这不是转换问题?即将无符号整数范围 [0..255] 中的颜色存储在期望值在 [0..1] 范围内的浮点数中?试试除以 255.0 看看是不是这样。

标签: c++ opengl glsl raytracing


【解决方案1】:

所有球体很可能都是白色的,因为GetRayColor 函数中的ClosestSphere.Hit 始终为真。

我认为问题出在您的 IntersectSceneSpheres 函数中。

在 CPU 代码中,您返回默认为 false 的 HitAnything。同时在片段着色器中返回 struct ClosestRecord,如果没有命中则保持未初始化。

IntersectSceneSpheres 函数的末尾显式添加ClosestRecord.Hit = HitAnything; 应该可以修复它

【讨论】:

    猜你喜欢
    • 2021-01-23
    • 2021-02-06
    • 2016-10-28
    • 2021-06-28
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    相关资源
    最近更新 更多