Q
Q
quad692022-04-10 16:01:16
C++ / C#
quad69, 2022-04-10 16:01:16

How to build a vertex buffer in a separate thread?

I'm learning C++, I decided to write a game (OpenGL + GLFW). As the game progresses, you constantly have to rebuild some parts of the levels, now this happens in the main thread and very often the game freezes at these moments.

And so the question is, how can you put all this into a separate thread? The problem is that gl functions refuse to work in another thread, glGetError returns code 1282 (invalid operation) even before any actions with the buffer itself. Now in a new thread I build the buffer itself (it's an array of floats) and call glBindVertexArray, glBindBuffer, glBufferData ... and the game crashes at this point.

Alternatively, you can collect an array of points in a separate thread, and when it is ready to call glBufferData already in the main, but is this correct? In general, I would like to hear from knowledgeable people the correct algorithm.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2022-04-10
@quad69

First you should think about having the GPU build the buffer.
If this is stressful, you have the option to use DMA (glMapBuffer), (I would do this option), use two contexts, or do whatever you want - call glBufferData on a signal from the child thread
Useful to read

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question