1

所以我正在研究光线追踪器,为了减少渲染时间,我使用 std::async 独立进行像素计算。我使用了这个教程,一切都很好,实际上我能够节省大约 70% 的渲染时间。

不过,有些场景需要一段时间才能渲染,我想显示某种进度条。由于我是异步基础设施的新手,我不太确定如何做到这一点。我想要某种机制来仅打印“主”线程的进度。

这里是渲染循环——注意进度条的注释行——显然不应该去那里:

Image *image = new Image(width, height);

size_t max = width * height;
size_t cores = std::thread::hardware_concurrency();
volatile atomic<size_t> count(0);
vector<future<void>> future_vector;

while (cores--)
{
    future_vector.push_back(
        std::async(launch::async, [=, &camera, &scene, &count]()
        {
            while (true)
            {
                std::size_t index = count++;
                if (index >= max)
                    break;

                GLuint i = index % width;
                GLuint j = index / width;

                Ray ray = camera.generateRay(i + .5, j - .5);
                vec3 color = recursiveRayTrace(scene, ray, maxDepth);
                image->setPixel(i, j, color);

                // THIS IS BAD
                //std::cout << "Progress: [ "<< setprecision(1) << fixed << (count / (GLfloat)max) * 100.0 << "% ] \r";
                //std::cout.flush();

            }
        }));
}

for (auto& e : future_vector) {
    e.get();
}

return image;

更新: 所以我做了一些答案的建议 - 这里是结果 - 为未来的读者。当异步渲染时,我对几个场景的基准是 15 秒(我故意加快了速度)。当我使用互斥锁时,时间减慢到 26 秒。(在单线程上仍然优于 1.32)。我还积极等待其中一个工作线程,并每隔 100 英里左右更新一次进度 - 渲染时间为 16 秒。所以我对这个结果非常满意,因为印刷几乎没有减慢这个过程。

谢谢, 阿隆

4

1 回答 1

1

似乎你在使用时需要一个锁std::cout,否则异步任务会产生混乱的输出(它们都试图同时在控制台上打印)。但是,我建议您使用 GDIplus(似乎您在代码中使用)来打印文本,而不是显示在相当难看的控制台窗口上。

Image *image = new Image(width, height);

size_t max = width * height;
size_t cores = std::thread::hardware_concurrency();
volatile atomic<size_t> count(0);
vector<future<void>> future_vector;
mutex cout_lock;
while (cores--)
{
    future_vector.push_back(
        std::async(launch::async, [=, &camera, &scene, &count]()
        {
            while (true)
            {
                std::size_t index = count++;
                if (index >= max)
                    break;

                GLuint i = index % width;
                GLuint j = index / width;

                Ray ray = camera.generateRay(i + .5, j - .5);
                vec3 color = recursiveRayTrace(scene, ray, maxDepth);
                image->setPixel(i, j, color);

                {  //lock variable scope
                   lock_guard<mutex> lock(cout_lock)
                   std::cout << "Progress: [ "<< setprecision(1) << fixed << (count / (GLfloat)max) * 100.0 << "% ] \r";
                   std::cout.flush();
                }

            }
        }));
}

for (auto& e : future_vector) {
    e.get();
}

return image;
于 2018-04-18T14:57:57.667 回答