Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Failed to activate OpenGL context  (Read 2791 times)

0 Members and 1 Guest are viewing this topic.

santiagolardiez

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Failed to activate OpenGL context
« 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3383
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Failed to activate OpenGL context
« Reply #1 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 (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.

Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11038
    • View Profile
    • development blog
    • Email
Re: Failed to activate OpenGL context
« Reply #2 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything