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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - homar

Pages: [1]
1
General / Re: Loading thread using a second OpenGL context
« on: January 08, 2014, 12:16:45 am »
This is essentially what I am doing.
Please find below a small example program that reproduces the crash:

#include <iostream>
#include <thread>

#include <GL/glew.h>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#include <X11/Xlib.h>

class ResourceLoader
{
public:
        void Run()
        {
                sf::Context loadingContext;
                loadingContext.setActive(true);

                // Some test data.
                float* testData = new float[3000];
                for (unsigned int i = 0; i < 3000; ++i)
                {
                        testData[i] = 0.0f;
                }

                // Create lots of VBOs containing our
                // test data.
                for (unsigned int i = 0; i < 1000; ++i)
                {
                        // Create VBO.
                        GLuint testVBO = 0;
                        glGenBuffers(1, &testVBO);
                        std::cout << "Buffer ID: " << testVBO << std::endl;

                        // Bind VBO.
                        glBindBuffer(GL_ARRAY_BUFFER, testVBO);

                        // Crashes on this call!
                        glBufferData(
                                GL_ARRAY_BUFFER,
                                sizeof(float) * 3000,
                                &testData[0],
                                GL_STATIC_DRAW
                        );

                        // Unbind VBO.
                        glBindBuffer(GL_ARRAY_BUFFER, 0);

                        // Sleep for a bit.
                        std::this_thread::sleep_for(std::chrono::milliseconds(10));
                }

                delete[] testData;
        }
};

int main()
{
        XInitThreads();

        // Create the main window.
        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window", sf::Style::Default, sf::ContextSettings(32));
        window.setVerticalSyncEnabled(true);

        // Make it the active window for OpenGL calls.
        window.setActive();

        // Configure the OpenGL viewport to be the same size as the window.
        glViewport(0, 0, window.getSize().x, window.getSize().y);

        // Initialize GLEW.
        glewExperimental = GL_TRUE; // OSX fix.
        if (glewInit() != GLEW_OK)
        {
                window.close();
                exit(1); // failure
        }

        // Enable Z-buffer reading and writing.
        glEnable(GL_DEPTH_TEST);
        glDepthMask(GL_TRUE);

        // Create the resource loader.
        ResourceLoader loader;

        // Run the resource loader in a separate thread.
        std::thread loaderThread(&ResourceLoader::Run, &loader);

        // Detach the loading thread, allowing it to run
        // in the background.
        loaderThread.detach();

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

                // Clear scren.
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                // Switch to SFML's OpenGL state.
                window.pushGLStates();
                {
                        // Perform SFML drawing here...
                        sf::RectangleShape rect(sf::Vector2f(100.0f, 100.0f));
                        rect.setPosition(100.0f, 100.0f);
                        rect.setFillColor(sf::Color(255, 255, 255));
                        window.draw(rect);
                }
                // Switch back to our game rendering OpenGL state.
                window.popGLStates();

                // Perform OpenGL drawing here...


                // Display the rendered frame.
                window.display();
        }

        return 0;
}
 

2
General / Loading thread using a second OpenGL context
« on: January 07, 2014, 07:32:22 pm »
My plan was to create a loading thread inside of which I load resources such as 3D models, shaders, textures, etc. On the main thread I perform all the game logic and rendering. Then, on my loading thread, I create a sf::Context which is used only for loading.

This is working for loading shaders. However, xserver crashes when attempting to load models. I have narrowed the crash down to the glBufferData() call.

Is it possible call glBufferData() from a second thread using a second OpenGL context? If not, why is it possible to load shaders in the second context?

3
General / Re: Rendering thread and game logic
« on: January 02, 2014, 10:56:20 pm »
It just specifies that I should use multiple threads with message passing for communication between threads. There is no requirement for how I should separate the game into multiple threads.

4
General / Re: Rendering thread and game logic
« on: January 02, 2014, 05:40:38 pm »
Unfortunately, as this is for a university project, the project brief states that I am required to use multiple threads.

5
General / Rendering thread and game logic
« on: January 02, 2014, 04:45:53 pm »
I am trying to wrap my head around working with multiple threads. In one of the tutorials (http://www.sfml-dev.org/tutorials/2.0/window-opengl.php) it states that a typical configuration is to handle the window and its events in the main thread with the rendering is performed in another thread. In this configuration, where would the game logic go?

If the game logic is in the rendering thread then I can't see there being much of a performance benefit over having a single thread. The main thread would be doing very little.

On the other hand, if the game logic is in the main thread then the game entities would need to be shared across both threads so that they can be updated in the main thread and rendered in the rendering thread. This is susceptible to race conditions and so each game entity would need a mutex.

6
General / Re: Fixed time-step game loop and rendering
« on: October 26, 2013, 10:57:55 pm »
Thanks FRex!
I guess that I'll have to check for window.isOpen() before calling Render().

7
General / Fixed time-step game loop and rendering
« on: October 26, 2013, 10:44:28 pm »
Hello,

All of the examples of a fixed time-step game loop I have seen follow this logic:

sf::Clock clock;
const sf::Time timePerFrame = sf::seconds(1.0f / 60.0f);
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (window.isOpen())
{
        sf::Time dt = clock.restart();
        timeSinceLastUpdate += dt;

        while (timeSinceLastUpdate > timePerFrame)
        {
                timeSinceLastUpdate -= timePerFrame;
                ProcessEvents();
                Update(timePerFrame);
        }

        Render();
}
 

Is there not a problem with this code? The ProcessEvents() function generally checks for a sf::Event::Closed event and can close the window at this point. The problem is that the Render() function could then try to render to a closed window.  The result is that my program crashes with a segmentation fault if I have raw OpenGL calls in my Render() function.

If I use SFML's graphics module to draw 2D elements, then the program doesn't crash.  Does SFML check whether the window is still open (or the OpenGL context is still valid) when I call draw() on the window?

Pages: [1]