Chris Vig
Chris Vig

Reputation: 8772

GL_INVALID_ENUM with glMapBuffer()

I'm completely new to OpenGL, so I assume I'm probably doing something stupid here. Basically I'm just trying to memory map a buffer object I've created, but glMapBuffer() is returning NULL and giving an error code of GL_INVALID_ENUM. Here's the relevant code that's failing:

glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);

glGenBuffers(1, &m_vertex_buffer);
glNamedBufferStorage(m_vertex_buffer,
                     BUFFER_SIZE,
                     NULL,
                     GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_DYNAMIC_STORAGE_BIT);

glBindBuffer(GL_ARRAY_BUFFER, m_vertex_buffer);
void* vertex_buffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
if (!vertex_buffer)
{
  GLenum error = glGetError();
  fprintf(stderr, "Buffer map failed! %d (%s)\n", error, gluErrorString(error));
  return;
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);

This is printing:

Buffer map failed! 1280 (invalid enumerant)

According to the docs, that error gets returned if the target is not one of the available target enums. That said, GL_ARRAY_BUFFER is definitely listed as available.

Am I just doing something wrong here?

Upvotes: 1

Views: 1398

Answers (1)

Chris Vig
Chris Vig

Reputation: 8772

In case it helps anyone else, I had multiple issues:

  • glGetError() returns the first value from a queue of errors. The GL_INVALID_ENUM I thought I was getting was actually from a previous (unrelated) call.
  • Per this thread, glGenBuffers() allocates a new buffer name, but the buffer is not actually created until it gets bound to a context using glBindBuffer(). I was instead calling glNamedBufferStorage() immediately, which resulted in a GL_INVALID_OPERATION since the buffer didn't actually exist yet. So basically I should always use glCreate*() instead of glGen*() when using DSA.
  • I believe that glNamedBufferStorage() is for immutable buffers, and glNamedBufferData() is for buffers that you can modify, although I'm not entirely clear on that from the documentation. In any case, I'm using glNamedBufferData() now.

I'm now able to successfully map and write to my buffers after the following setup:

glCreateBuffers(1, &m_vertex_buffer);
glNamedBufferData(m_vertex_buffer, BUFFER_SIZE, NULL, GL_DYNAMIC_DRAW);
void* vertex_buffer_ptr = glMapNamedBuffer(m_vertex_buffer, GL_WRITE_ONLY);

Upvotes: 1

Related Questions