【发布时间】:2017-08-20 18:18:34
【问题描述】:
我的光线追踪器目前是多线程的,我基本上将图像分成系统拥有的多个块并并行渲染它们。但是,并不是所有的块都具有相同的渲染时间,所以大部分时间一半的运行时间只有 50% 的 cpu 使用率。
std::shared_ptr<bitmap_image> image = std::make_shared<bitmap_image>(WIDTH, HEIGHT);
auto nThreads = std::thread::hardware_concurrency();
std::cout << "Resolution: " << WIDTH << "x" << HEIGHT << std::endl;
std::cout << "Supersampling: " << SUPERSAMPLING << std::endl;
std::cout << "Ray depth: " << DEPTH << std::endl;
std::cout << "Threads: " << nThreads << std::endl;
std::vector<RenderThread> renderThreads(nThreads);
std::vector<std::thread> tt;
auto size = WIDTH*HEIGHT;
auto chunk = size / nThreads;
auto rem = size % nThreads;
//launch threads
for (unsigned i = 0; i < nThreads - 1; i++)
{
tt.emplace_back(std::thread(&RenderThread::LaunchThread, &renderThreads[i], i * chunk, (i + 1) * chunk, image));
}
tt.emplace_back(std::thread(&RenderThread::LaunchThread, &renderThreads[nThreads-1], (nThreads - 1)*chunk, nThreads*chunk + rem, image));
for (auto& t : tt)
t.join();
我想将图像分成 16x16 块或类似的东西并并行渲染,所以在每个块被渲染后,线程切换到下一个,依此类推......这将大大增加 CPU 使用率和运行时间。
如何设置我的光线追踪器以多线程方式渲染这些 16x16 块?
【问题讨论】:
-
那么问题是什么?
-
我的问题是如何分成16x16的块,然后将它们排队到线程中。
标签: multithreading graphics 3d raytracing