'Why glBufferSubData is slow?

When trying to access GL_ARRAY_BUFFER using glBufferSubData to 128 byte buffer size, accessing takes at worst 200 microseconds. Instead when allocating whole buffer again with glBufferData it takes only 3 microseconds. I had similar issues with glGetBufferSubData. My CPU is i5 8600k and gpu is gtx 1080 ti

 glBufferData(GL_ARRAY_BUFFER, sizeof(mat4) * 2, matrices.data(), GL_DYNAMIC_DRAW); // 3 us
    glBufferSubData(GL_ARRAY_BUFFER, sizeof(mat4) * index, sizeof(mat4), matrix.data()); // at worst 200 us

    using namespace std::chrono;
    // same measurement for both cases
    auto timer = high_resolution_clock::now();
    glBufferData(GL_ARRAY_BUFFER, sizeof(mat4) * 2, matrices.data(), GL_DYNAMIC_DRAW);
    std::cout << duration_cast<microseconds>(high_resolution_clock::now() - timer).count() << '\n';


Solution 1:[1]

This question might be overdue for an answer, but today I was considering using glGetBufferSubData and noticed a bit of lag despite running the and NVidia GEFORCE RTX 3060. There was a 0.006 second to download data to the CPU/RAM and a 0 second time to load to the GPU. I would suggest keeping a copy of your matrix in RAM and store the index of where the matrix is in the shader storage buffer so you can update it later in you program if you like. That is what I am doing at least.

You can see my original post here: Is it efficient to download matrix to CPU/RAM to update it and send it back to GPU?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Christopher Barrios Agosto