SFML community forums

Help => System => Topic started by: santiagolardiez on January 12, 2024, 11:48:21 pm

Title: Failed to activate OpenGL context
Post by: santiagolardiez on January 12, 2024, 11:48:21 pm
I am making a terrain generation program, and I want to get it to run relatively quickly (currently it takes 5 seconds to render a small 600, 600 size screen).

Right now, I am threading each row of pixels to draw each colored sprite every row simultaneously. I am getting stuck with a runtime error that says,

Failed to activate OpenGL context: The resource is in use.

I was wondering it this is a problem of thread syncing or maybe there is another better way to do this, because I know that drawing thousands of sprites is inefficient but I don't know of a better way on SFML.
Title: Re: Failed to activate OpenGL context
Post by: Hapax on January 13, 2024, 10:36:03 pm
It sounds like you're drawing a sprite per pixel but I could be wrong. If you are, though, you should more likely use a vertex array of points.
With that said, converting from thousands of sprites to a vertex array will also help improve performance.

You could use a ready and automatic batcher such as this one (https://en.sfml-dev.org/forums/index.php?topic=29286.0) (easiest option), write a custom batcher or create the vertex array from the ground up (best but most complicated option).

If the issue is during preparation, you could likely perform the generation on values in memory (off the GPU) and only transfer to GPU when it's ready (such as by drawing a vertex array or other option).

"Other option": You could create an image or texture that contains what you are drawing and then draw just that as a quad (or multiple textures and quads if required). This is a pre-render.

A mid-way option is to do the same thing but to a render texture and that allows you to possibly more easily update it (or parts of it) if things change.

Title: Re: Failed to activate OpenGL context
Post by: eXpl0it3r on January 15, 2024, 02:04:44 pm
Split from: https://en.sfml-dev.org/forums/index.php?topic=15430.0
Maybe don't revive 10 year old threads, when you're asking different questions. ;)

You really shouldn't be doing OpenGL calls (i.e. anything with SFML Graphics objects will do OpenGL calls) in multiple threads.
OpenGL isn't multi-thread capable, so all you do is write to a queue from two threads and you have to make sure the right context is active at the right moment, and each context activate is quite costly.

Unless you know how mutexes work, what you need to protect shared memory, what race conditions are, etc. etc. I highly recommend to not do any multi-threading.

If you draw pixels anyways, then use an sf::Image and build everything up in RAM as Hapax said. Then once the image is complete, you can transfer it to the GPU.