Ошибка: квалификаторы отброшены в привязке ссылки типа «бла-бла» для инициализации «другого бла-бла» - PullRequest
0 голосов
/ 26 октября 2019

Итак, я получаю ошибку во время выполнения, когда я создаю переменные "boxes" и "boxbound" вне потока, однако ошибка исчезает, когда я перемещаю его внутри цикла for внутри потока, что может быть причиной этого?

void Flyscene::raytraceScene(int width, int height) {
std::cout << "ray tracing ..." << std::endl;

//start of acceleration structure
std::vector<std::vector<Tucano::Face>> boxes = firstBox(mesh);
std::vector<std::vector<Eigen::Vector3f>> boxbounds;
for (int i = 0; i < boxes.size(); i++) {
    boxbounds.push_back(getBoxLimits(boxes[i], mesh));
}
/////


// if no width or height passed, use dimensions of current viewport
Eigen::Vector2i image_size(width, height);
if (width == 0 || height == 0) {
    image_size = flycamera.getViewportSize();
}


// create 2d vector to hold pixel colors and resize to match image size
vector<vector<Eigen::Vector3f>> pixel_data;
pixel_data.resize(image_size[1]);
for (int i = 0; i < image_size[1]; ++i)
    pixel_data[i].resize(image_size[0]);


// origin of the ray is always the camera center
Eigen::Vector3f origin = flycamera.getCenter();
Eigen::Vector3f screen_coords;



// Multi Threading
// Comment this if you don't want multi-threading
//-----------------------------------------------------//
int max_pixels = (image_size[0] * image_size[1]); //width * height
// Get amount of cores of your CPU
int cores = std::thread::hardware_concurrency();
// Keep track of # of pixels (atomic making sure no 2 threads render the same pixel)
volatile std::atomic<std::size_t> curr_pixel(0);
// Stores all cores assigned to a task
std::vector<std::future<void>> future_vector;
cout << "Threads supported: " << cores << "\n";
while (cores--)
    future_vector.emplace_back(
        std::async([=, &origin, &curr_pixel, &pixel_data]()
            {
                while (true)
                {
                    int index = curr_pixel++;
                    if (index >= max_pixels)
                        break;
                    std::size_t i = index % image_size[1];
                    std::size_t j = index / image_size[1];
                    //cout << "at index: " << index << std::endl;


                    // create a ray from the camera passing through the pixel (i,j)
                    auto screen_coords = flycamera.screenToWorld(Eigen::Vector2f(i, j));
                    // launch raytracing for the given ray and write result to pixel data
                    pixel_data[i][j] = traceRay(0,origin, screen_coords, boxes, boxbounds);
                    if (index % 10000 == 0) {
                        std::cout << "Percentage done (mt): " << (float)(index / 10000) << "%" << std::endl;
                    }
                }
            }));

// Call futures (Async jobs), this will activate all process on the cores
for (auto& e : future_vector) {
    e.get();
}

однако, когда я перемещаю его внутрь, как показано ниже, ошибка исчезает;

void Flyscene::raytraceScene(int width, int height) {
    std::cout << "ray tracing ..." << std::endl;


    // if no width or height passed, use dimensions of current viewport
    Eigen::Vector2i image_size(width, height);
    if (width == 0 || height == 0) {
        image_size = flycamera.getViewportSize();
    }


    // create 2d vector to hold pixel colors and resize to match image size
    vector<vector<Eigen::Vector3f>> pixel_data;
    pixel_data.resize(image_size[1]);
    for (int i = 0; i < image_size[1]; ++i)
        pixel_data[i].resize(image_size[0]);


    // origin of the ray is always the camera center
    Eigen::Vector3f origin = flycamera.getCenter();
    Eigen::Vector3f screen_coords;



    // Multi Threading
    // Comment this if you don't want multi-threading
    //-----------------------------------------------------//
    int max_pixels = (image_size[0] * image_size[1]); //width * height
    // Get amount of cores of your CPU
    int cores = std::thread::hardware_concurrency();
    // Keep track of # of pixels (atomic making sure no 2 threads render the same pixel)
    volatile std::atomic<std::size_t> curr_pixel(0);
    // Stores all cores assigned to a task
    std::vector<std::future<void>> future_vector;
    cout << "Threads supported: " << cores << "\n";
    while (cores--)
        future_vector.emplace_back(
            std::async([=, &origin, &curr_pixel, &pixel_data]()
                {
                    while (true)
                    {
                        int index = curr_pixel++;
                        if (index >= max_pixels)
                            break;
                        std::size_t i = index % image_size[1];
                        std::size_t j = index / image_size[1];
                        //cout << "at index: " << index << std::endl;
     //start of acceleration structure
    std::vector<std::vector<Tucano::Face>> boxes = firstBox(mesh);
    std::vector<std::vector<Eigen::Vector3f>> boxbounds;
    for (int i = 0; i < boxes.size(); i++) {
        boxbounds.push_back(getBoxLimits(boxes[i], mesh));
    }
    /////


                        // create a ray from the camera passing through the pixel (i,j)
                        auto screen_coords = flycamera.screenToWorld(Eigen::Vector2f(i, j));
                        // launch raytracing for the given ray and write result to pixel data
                        pixel_data[i][j] = traceRay(0,origin, screen_coords, boxes, boxbounds);
                        if (index % 10000 == 0) {
                            std::cout << "Percentage done (mt): " << (float)(index / 10000) << "%" << std::endl;
                        }
                    }
                }));

    // Call futures (Async jobs), this will activate all process on the cores
    for (auto& e : future_vector) {
        e.get();
    }

вот и метод rayTrace:

Eigen::Vector3f Flyscene::traceRay(int level, Eigen::Vector3f& origin, Eigen::Vector3f& dest, std::vector<std::vector<Tucano::Face>>& boxes, std::vector<std::vector<Eigen::Vector3f>>& boxbounds)

Почему выдумаю, что это?

Вот полное описание ошибки:

Ошибка (активная). Спецификаторы E0433 отброшены в привязке ссылки типа "std :: vector>, std :: allocator >>> &"инициализатору типа "const std :: vector>, std :: allocator >>>" raytracing

Ошибка (активная) Спецификаторы E0433 отброшены в привязке ссылки типа "std :: vector>, std :: allocator>>> & "инициализатору типа" const std :: vector>, std :: allocator >>> "raytracing

1 Ответ

1 голос
/ 26 октября 2019

Вам нужно добавить mutable к вашей лямбде.

Векторы передаются по ссылке (traceRay), поэтому они могут быть изменены внутри этой функции. Ваша лямбда получает векторы по копии = (используется при захвате), объект, захваченный =, можно только читать, вы не можете их изменять.

Ваш код может быть уменьшен до этого примера:

void bar(std::vector<int>& v) {

}

void foo() {
    std::vector<int> v;

    auto l = [=]() /*mutable*/
    {
        bar(v); // works only with uncommented mutable
        // v can be modified only with mutable
    };
    l();
}

Когда вы создаете векторы внутри лямбды, они не фиксируются, поэтому вы можете изменить их в traceRay.

Поэтому в первом фрагменте вы добавляете mutable:

    std::async([=, &origin, &curr_pixel, &pixel_data]() mutable
        {                                               ^^^^^^^
            while (true)
            {
...