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

Author Topic: reducing cpu useage?  (Read 4789 times)

0 Members and 1 Guest are viewing this topic.

h4tt3n

  • Newbie
  • *
  • Posts: 7
    • View Profile
reducing cpu useage?
« on: March 21, 2009, 09:35:44 am »
Hello everyone,

For some time I've been using glut and glfw for my opengl projects, and now I'm looking for something broader which still has ogl support.

What I like about the above two tools is their ability to automatically keep cpu useage at a minimum. When making - say - a small physics simulation project with opengl, it typically spends just 5-10% cpu power in glut and glfw. This happens automatically, and as a programmer I don't have to worry about adding any form of sleep routine to prevent ~100% cpu useage.

Now, when I ran the *very* small opengl example shipped with sfml 1.4 cpu useage went sky high, so I assume that the above mentioned functionality is not part of this library? Does sfml have a native, automatic way of keeping cpu useage at a minimum?

cheers,
Mike

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
reducing cpu useage?
« Reply #1 on: March 21, 2009, 09:45:49 am »
window.SetFramerateLimit
or
window.UseVerticalSync
Laurent Gomila - SFML developer

h4tt3n

  • Newbie
  • *
  • Posts: 7
    • View Profile
reducing cpu useage?
« Reply #2 on: March 21, 2009, 12:07:13 pm »
Quote from: "Laurent"
window.SetFramerateLimit
or
window.UseVerticalSync


Ok, I tried to add one of these after opening the window:

App.UseVerticalSync(true);

and

App.SetFramerateLimit(60);

In both cases cpu useage dropped considerably, but at the cost of jerky movement and artefacts. This should be impossible with vertical sync on, though !?!

Not sure what to do now...


Code: [Select]

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <iostream>


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
    // Create main window
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML OpenGL");
   
    App.PreserveOpenGLStates(true);
    //App.UseVerticalSync(true);
   // App.SetFramerateLimit(60);

    // Create a sprite for the background
    sf::Image BackgroundImage;
    if (!BackgroundImage.LoadFromFile("datas/opengl/background.jpg"))
        return EXIT_FAILURE;
    sf::Sprite Background(BackgroundImage);

    // Load an OpenGL texture.
    // We could directly use a sf::Image as an OpenGL texture (with its Bind() member function),
    // but here we want more control on it (generate mipmaps, ...) so we create a new one
    GLuint Texture = 0;
    {
        sf::Image Image;
        if (!Image.LoadFromFile("datas/opengl/texture.jpg"))
            return EXIT_FAILURE;
        glGenTextures(1, &Texture);
        glBindTexture(GL_TEXTURE_2D, Texture);
        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Image.GetWidth(), Image.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, Image.GetPixelsPtr());
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    }

    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);
    glClearDepth(1.f);

    // Setup a perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.f, 1.f, 1.f, 500.f);

    // Bind our texture
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, Texture);
    glColor4f(1.f, 1.f, 1.f, 1.f);

    // Create a clock for measuring the time elapsed
    sf::Clock Clock;

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

            // Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();

            // Adjust the viewport when the window is resized
            if (Event.Type == sf::Event::Resized)
                glViewport(0, 0, Event.Size.Width, Event.Size.Height);
         }

        // Draw background
        App.Draw(Background);

        // Clear depth buffer
        glClear(GL_DEPTH_BUFFER_BIT);

        // Apply some transformations
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.f, 0.f, -200.f);
        glRotatef(Clock.GetElapsedTime() * 30, 1.f, 0.f, 0.f);
        glRotatef(Clock.GetElapsedTime() * 60, 0.f, 1.f, 0.f);
        glRotatef(Clock.GetElapsedTime() * 90, 0.f, 0.f, 1.f);

        // Draw a cube
        glBegin(GL_QUADS);

            glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
            glTexCoord2f(0, 1); glVertex3f(-50.f,  50.f, -50.f);
            glTexCoord2f(1, 1); glVertex3f( 50.f,  50.f, -50.f);
            glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, -50.f);

            glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, 50.f);
            glTexCoord2f(0, 1); glVertex3f(-50.f,  50.f, 50.f);
            glTexCoord2f(1, 1); glVertex3f( 50.f,  50.f, 50.f);
            glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, 50.f);

            glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
            glTexCoord2f(0, 1); glVertex3f(-50.f,  50.f, -50.f);
            glTexCoord2f(1, 1); glVertex3f(-50.f,  50.f,  50.f);
            glTexCoord2f(1, 0); glVertex3f(-50.f, -50.f,  50.f);

            glTexCoord2f(0, 0); glVertex3f(50.f, -50.f, -50.f);
            glTexCoord2f(0, 1); glVertex3f(50.f,  50.f, -50.f);
            glTexCoord2f(1, 1); glVertex3f(50.f,  50.f,  50.f);
            glTexCoord2f(1, 0); glVertex3f(50.f, -50.f,  50.f);

            glTexCoord2f(0, 1); glVertex3f(-50.f, -50.f,  50.f);
            glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
            glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, -50.f);
            glTexCoord2f(1, 1); glVertex3f( 50.f, -50.f,  50.f);

            glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f,  50.f);
            glTexCoord2f(0, 0); glVertex3f(-50.f, 50.f, -50.f);
            glTexCoord2f(1, 0); glVertex3f( 50.f, 50.f, -50.f);
            glTexCoord2f(1, 1); glVertex3f( 50.f, 50.f,  50.f);

        glEnd();

        // Draw some text on top of our OpenGL object
        sf::String Text("This is not a rotating cube");
        Text.SetPosition(50.f, 50.f);
        Text.SetColor(sf::Color(255, 244, 32));
        App.Draw(Text);

        // Finally, display the rendered frame on screen
        App.Display();
    }

    // Don't forget to destroy our texture
    glDeleteTextures(1, &Texture);

    return EXIT_SUCCESS;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
reducing cpu useage?
« Reply #3 on: March 21, 2009, 01:19:33 pm »
You shouldn't use both at the same time.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
reducing cpu useage?
« Reply #4 on: March 21, 2009, 02:12:04 pm »
Besides, you could use sf::Sleep to handle your framerate manually.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
reducing cpu useage?
« Reply #5 on: March 21, 2009, 02:23:31 pm »
Quote from: "Nexus"
Besides, you could use sf::Sleep to handle your framerate manually.

What for ? Isn't SetFramerateLimit() satisfying ?
Want to play movies in your SFML application? Check out sfeMovie!

h4tt3n

  • Newbie
  • *
  • Posts: 7
    • View Profile
reducing cpu useage?
« Reply #6 on: March 21, 2009, 03:25:38 pm »
Quote from: "Laurent"
You shouldn't use both at the same time.


I tried using the first, the last, and then both just for fun. All variations had pretty much the same result.

How come there are obvious graphical glitches whith vsync on (and setframelimit off) ? Are you sure this is not a bug?

Cheers,
Mike

chbrules

  • Newbie
  • *
  • Posts: 16
    • AOL Instant Messenger - Conrad1986
    • View Profile
reducing cpu useage?
« Reply #7 on: April 02, 2009, 06:10:16 am »
When I create just a window and run a loop to check for a window close event I get 100% CPU usage (50/50 on my dual core system). Why would it be doing this?

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
reducing cpu useage?
« Reply #8 on: April 02, 2009, 01:59:37 pm »
Why wouldn't it ? It goes on looping without any pause.
Want to play movies in your SFML application? Check out sfeMovie!

chbrules

  • Newbie
  • *
  • Posts: 16
    • AOL Instant Messenger - Conrad1986
    • View Profile
reducing cpu useage?
« Reply #9 on: April 02, 2009, 08:46:22 pm »
Working with SDL lI didn't have this issue. Setting up a similar program that initializes a window and just runs a gameloop would yeild very low (single digit) processor usage.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
reducing cpu useage?
« Reply #10 on: April 02, 2009, 09:50:36 pm »
Your program is basically just an endless while loop without any pause, that's why it eats as much CPU as it can. This is just a normal behaviour, there's no problem with that. However it doesn't mean that other programs won't be able to get the CPU as they need.
This will be the same with any library or API.

Having a low CPU usage is just a matter of inserting a pause somewhere. It can be done with:
- enabling vertical synchronization, either in your GPU driver or in your app
- limiting framerate
- calling a sleep function yourself
- most libraries will do it for you automatically
Laurent Gomila - SFML developer

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
reducing cpu useage?
« Reply #11 on: April 03, 2009, 12:03:03 am »
Quote from: "chbrules"
Working with SDL lI didn't have this issue.

Certainly because you were using the SDL_WaitEvent() function that blocks your program until it gets an event. SFML only has a function similar to SDL_PollEvent().
Want to play movies in your SFML application? Check out sfeMovie!

chbrules

  • Newbie
  • *
  • Posts: 16
    • AOL Instant Messenger - Conrad1986
    • View Profile
reducing cpu useage?
« Reply #12 on: April 03, 2009, 09:12:54 am »
Quote from: "Ceylo"
Quote from: "chbrules"
Working with SDL lI didn't have this issue.

Certainly because you were using the SDL_WaitEvent() function that blocks your program until it gets an event. SFML only has a function similar to SDL_PollEvent().


Ah, that would probably explain it then. Good to know.

Thanks guys!

 

anything