GL_DYNAMIC_DRAW performance spikes

  buffer, c++, chrono, opengl

I’m working on my own game engine in C++ with OpenGL, and currently working on the UI. Because I wanted the UI to be scale nicely with different screens, every time there’s a window resize it subs in some vertex data to the buffer so the width is consistent.

void UICanvas::windowResize(int width, int height) {
    for (std::vector<int>::iterator itr = wIndices.begin(); itr != wIndices.end(); itr++) {
        UIWindow& window = uiWindows.at(*itr);
        float newX = window.size.x / (100.0f * (*aspectRatio));
        float newY = window.size.y / 100.0f;

        float newPositions = {
            newX, 0.0f,
            newX, -newY
        };

        glBindBuffer(GL_ARRAY_BUFFER, window.positions_vbo);
        std::chrono::high_resolution_clock::timepoint start, end;
        for (int i = 0; i < 1000; i++) {
            start = std::chrono::high_resolution_clock::now();
            glBufferSubData(GL_ARRAY_BUFFER, 16, 16, &newPositions[0]);
            end = std::chrono::high_resolution_clock::now();
            printEvent("windowResize", start, end); //Outputs to json for profiling
        }
    }
}

Knowing it would be updated occasionally, I knew I should probably use GL_DYNAMIC_DRAW. However, instead of just listening to people I wanted to benchmark the difference myself. For GL_DYNAMIC_DRAW, most of the resize function calls were 3-5 microseconds, with steady occasional spikes of 180-200 microseconds, and even more sparse spikes up to 4 milliseconds, 1000x the normal value. GL_STATIC_DRAW, while the average was 7-13 ms, only spiked up to 100-150 microseconds, but almost rarely. I know there are thousands of factors that can affect benchmarking, like the CPU caching values, or optimizing certain things, but it would almost make more sense if STATIC_DRAW followed the same giant spike pattern. I think I read somewhere that dynamic buffers are stored in VRAM and static ones in regular RAM, but would that be the reason for this behavior? If I use DYNAMIC_DRAW, will it have occasional spikes, or is it due to the repeated calls?

Source: Windows Questions C++

LEAVE A COMMENT