slow init vs init slow

15, Sep, 2020

The way of initialization may result in quite interesting bottlenecks..

The first way needs separate allocation:

void *data = malloc();
load(data);
glBufferStorage(target, sz, data, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_DYNAMIC_STORAGE_BIT);
free(data);

which can be avoided using the second way:

glBufferStorage(target, sz, NULL, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_DYNAMIC_STORAGE_BIT);
void *data = glMapBufferRange(target, 0, sz, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
load(data);
glUnmapBuffer(target);

However, the second way resulted in 30% slowdown comparing to first on every draw call with this buffer.