0
\$\begingroup\$

I have two threads. My main thread handles all the OpenGL calls, and my second thread batches all the data together in std::vector and then passes the data back to the main thread. Absolutely nowhere in the secondary thread do I call OpenGL.

When I step through the code, it always gives me an error in the thread.

The code that runs the thread

glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

this->batch_threads.push_back(
    std::thread(
        [this]{calculate_batch(
            this->batches.size() - 1
        );}
    )
);

And the actual thread code

this->batch_processing[id] = true;

// It's time to process all the 
// models that use materials[id] and
// batch them together

std::vector<Model> models = this->materials[id]->_get_models();

if (
    this->materials[id]->_get_texture_count() > this->max_textures
) {
    critical(
        "Current material requires %i textures. Only %i textures are supported at once\n",
        this->materials[id]->_get_texture_count(),
        this->max_textures
    );
}

// Batch together values
std::vector<float> data;
std::vector<unsigned int> inds;

std::vector<float> pos_data, normal_data, tangent_data, bitangent_data, uv_data;

for (size_t i = 0; i < models.size(); i++) {
    std::vector<unsigned int> model_inds = models[i]._get_inds();
    for (size_t j = 0; j < model_inds.size(); j++) {
        inds.push_back(model_inds[j]);
    }

    std::vector<float> model_pos = models[i]._get_pos();
    for (size_t j = 0; j < model_pos.size(); j++) {
       pos_data.push_back(model_pos[j]);
    }

    std::vector<float> model_normal = models[i]._get_normal();
    for (size_t j = 0; j < model_normal.size(); j++) {
        normal_data.push_back(model_normal[j]);
    }

    std::vector<float> model_tans = models[i]._get_tans();
    for (size_t j = 0; j < model_tans.size(); j++) {
        tangent_data.push_back(model_tans[j]);
    }

    std::vector<float> model_bitans = models[i]._get_bitans();
    for (size_t j = 0; j < model_bitans.size(); j++) {
        bitangent_data.push_back(model_bitans[j]);
    }

    std::vector<float> model_uvs = models[i]._get_uvs();
    for (size_t j = 0; j < model_uvs.size(); j++) {
       uv_data.push_back(model_uvs[j]);
    }
}

// Combine them all into one
for (size_t i = 0; i < pos_data.size(); i++) {
    data.push_back(pos_data[i]);
}

for (size_t i = 0; i < normal_data.size(); i++) {
    data.push_back(normal_data[i]);
}

for (size_t i = 0; i < tangent_data.size(); i++) {
    data.push_back(tangent_data[i]);
}

for (size_t i = 0; i < bitangent_data.size(); i++) {
    data.push_back(bitangent_data[i]);
}

for (size_t i = 0; i < uv_data.size(); i++) {
    data.push_back(uv_data[i]);
}

// Wait until we are not rendering
while (this->is_rendering) {}

this->batches[id].data = data;
this->batches[id].inds = inds;
this->batches[id].size = inds.size();
this->batches[id].vertex_size = pos_data.size();
this->batches[id].uv_size = uv_data.size();
this->batches[id].dirty = true;
this->batch_processing[id] = false;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.