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

Author Topic: Assistance requested concerning render threads.  (Read 5430 times)

0 Members and 1 Guest are viewing this topic.

cuddlyogre

  • Newbie
  • *
  • Posts: 10
    • View Profile
Assistance requested concerning render threads.
« on: November 04, 2013, 05:26:14 pm »
I am attempting to separate out my rendering from my logic.

As it is, when I render inside of my update thread, everything is incredibly smoothly. But when I move the draw operations to their own thread, everything gets incredibly choppy.

I've done a lot of researching and apparently this is a common issue with just about any framework in existence, but I can't seem to find any real answers on how to remedy the situation, besides just not separating them in the first place.

If that's what I have to do, then I accept that, but it would be nice to be able to do this, if even just to know how. If the answer is in the documentation, or on the forums somewhere, please direct me to it. I would love to read it.

CODE IS AS FOLLOWS

#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include <functional>

void startGameClass();
void startMinimalExample();
void render(sf::Sprite *sprite);
sf::RenderWindow window;

int main()
{
        startMinimalExample();
        return 0;
}

void startMinimalExample()
{
        window.create(sf::VideoMode(640, 480),"SFML Window");
        window.setActive(false);

        sf::Sprite sprite;
        sf::Texture texture;
        texture.loadFromFile(".\\resources\\test.png");
        sprite.setTexture(texture);

        sf::Thread t(std::bind(&render, &sprite));

        //UNCOMMENT t.launch() TO LAUNCH RENDER THREAD
        //COMMENT OUT WINDOW CALLS BELOW AS WELL
        //t.launch();

        sf::Clock updateClock;
        float fps = 1000000.00f / 60.00f;
        while (window.isOpen())
        {
                if (updateClock.getElapsedTime().asMicroseconds() >= fps)
                {
                        sf::Time elapsed = updateClock.restart();

                        sf::Event event;
                        while (window.pollEvent(event))
                        {
                                if (event.type == sf::Event::Closed)
                                {
                                        window.close();
                                }
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                        {
                                sprite.move(.25f * elapsed.asMilliseconds(), 0);
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                        {
                                sprite.move(-.25f * elapsed.asMilliseconds(), 0);
                        }

                        //COMMENT NEXT THREE LINES WHEN USING RENDER THREAD
                        window.clear();
                        window.draw(sprite);
                        window.display();      
                }
        }
}

void render(sf::Sprite *sprite)
{
        sf::Clock renderclock;
        float fps = 1000000.00f / 60.00f;
        while (window.isOpen())
        {
                if (renderclock.getElapsedTime().asMicroseconds() >= fps)
                {
                        renderclock.restart();
                        window.clear(sf::Color::Black);
                        window.draw(*sprite);
                        window.display();
                }
        }
}
 
« Last Edit: November 04, 2013, 05:51:40 pm by cuddlyogre »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Assistance requested concerning render threads.
« Reply #1 on: November 04, 2013, 05:50:05 pm »
Given that you're not using any kind of synchronization and SFML is not thread safe on its own, you're lucky if the application doesn't crash, because the state where thread1 reads while at same time thread2 writes (e.g. Sprite position) is undefined.
Simply separating update and rendering will never give you a lot of advantages. If it's not just for learning it's not really worth implementing multicore support as long as you don't have performance issues. That's mainly because parallel programming is a rather complex topic, due to all the synchronization issues one has to handle and actual performance gain is only small for many cases. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

cuddlyogre

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Assistance requested concerning render threads.
« Reply #2 on: November 04, 2013, 06:01:56 pm »
That's pretty much the answer I was expecting, since most of my searches also said it's probably not worth it. Do you know of any good resources on thread synchronization I could read up on? I seem to be coming up quite empty with my searches.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Assistance requested concerning render threads.
« Reply #3 on: November 04, 2013, 09:10:08 pm »
Even when you're experienced, multithreading will always be more complex and potentially more error-prone. Why use it if it's not necessary? It's of course good to know, for situations where it can actually improve performances or response time.

By the way, don't use global variables, and especially avoid SFML globals. You can train your search skills by finding out why ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

cuddlyogre

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Assistance requested concerning render threads.
« Reply #4 on: November 04, 2013, 09:58:08 pm »
Thank you for the feedback. I'm liking this community more by the minute.

Quote
By the way, don't use global variables, and especially avoid SFML globals. You can train your search skills by finding out why

Oh, most certainly. I've learned from past mistakes how bad globals can really darken someone's day.
« Last Edit: November 04, 2013, 09:59:48 pm by cuddlyogre »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Assistance requested concerning render threads.
« Reply #5 on: November 04, 2013, 10:21:09 pm »
As for a book about parallel programming... I haven't read any book about it, but from the C++ book list, you might want to take a look at "C++ Concurrency in Action: Practical Multithreading" no idea if it's good or bad though.

I've learned most of my theoretical knowledge from the university and my professor seemed to be be quite a fan of "The Art of Multiprocessor Programming" by Maurice Herlihy and Nir Shavit. I think it contains some valuable information, but keep in mind the book teaches the theory and not how you use it with C++. ;)

From a priority stand point though, you might want to think about whether concurrency is really the most important thing to look into at the moment. It's very interesting, complex and will get important more and more
« Last Edit: November 04, 2013, 10:23:30 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

cuddlyogre

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Assistance requested concerning render threads.
« Reply #6 on: November 04, 2013, 10:22:53 pm »
I've brought up the topic to another forum I'm part of and the answer is increasingly no, I do not. At least not for this task.

 

anything