LWJGL 3 - Immense loading time for opening a second window

Started by h1, August 28, 2015, 16:02:24

Previous topic - Next topic

h1

Hello there,

most time (not always) when my application opens a second window, it takes significant amount of time until opengl begins to draw. The window always appears imadiatly. i have no clou waht causes this problem.

here is my code which initialises a new window:
        // Setup an error callback. The default implementation
        // will print the error message in System.err.
        glfwSetErrorCallback(errorCallback = errorCallbackPrint(System.err));

        // Configure our window
        glfwDefaultWindowHints(); // optional, the current window hints are already the default
        glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation
        glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // the window will be resizable

        // Create the window
        window = glfwCreateWindow(width, height, "Hello World!", NULL, NULL);
        if ( window == NULL )
            throw new RuntimeException("Failed to create the GLFW window");

        // Get the resolution of the primary monitor
        ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());

        // Make the OpenGL context current
        glfwMakeContextCurrent(window);

        // Enable v-sync
        glfwSwapInterval(1); // 60 fps

        // Make the window visible
        glfwShowWindow(window);


        // Setup several callbacks...
        [...]

        // This line is critical for LWJGL's interoperation with GLFW's
        // OpenGL context, or any context that is managed externally.
        // LWJGL detects the context that is current in the current thread,
        // creates the ContextCapabilities instance and makes the OpenGL
        // bindings available for use.
        GLContext.createFromCurrent();

        // Set the clear color
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        // init Gl
        glViewport(0, 0, width, height);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, width, 0, height, 1, -1);


can somebody help me please :)

quew8

Well creating a window and context and things of the like will take time but if you move the "glfwShowWindow" to the end of code snippet then the window will start drawing as soon as it is visible at least, but it will take longer to be visible.

h1

my current work around is, whenever it takes longer i open an addidtional window. this additional window then immadiatly shows up and start rendering with almost no delay. and the loading window from before is then also ready. I can't make head or tail of it.

without this hack the loading time could rise up to 20 seconds !!!!?! this couldn't be normal..

spasi

Hey h1,

Could you try with the latest 3.0.0b build? If it still happens, could you time your code and let us know which function is taking so long to execute?

h1

Changing to latest stable release was my first attempt but this problem couldnt get solved by doing so.

I altered the code from above somewhat for timing:
        Timer timer = new Timer();
        timer.setResolution(Timer.MILLISECONDS);
        timer.reset();

        // Setup an error callback. The default implementation
        // will print the error message in System.err.
        glfwSetErrorCallback(errorCallback = errorCallbackPrint(System.err));

        // Configure our window
        glfwDefaultWindowHints(); // optional, the current window hints are already the default
        glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation
        glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // the window will be resizable

        // Create the window
        window = glfwCreateWindow(width, height, "Hello World!", NULL, NULL);
        if ( window == NULL )
            throw new RuntimeException("Failed to create the GLFW window");

        // Get the resolution of the primary monitor
        ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());

        // Make the OpenGL context current
        glfwMakeContextCurrent(window);

        // Enable v-sync
        //glfwSwapInterval(0); // max fps
        glfwSwapInterval(1); // 60 fps
        //glfwSwapInterval(2); // 30 fps


        timer.advance();
        System.out.println("before show " + timer.getDeltaTime());


        // Make the window visible
        glfwShowWindow(window);

        
        timer.advance();
        System.out.println("after show " + timer.getDeltaTime());


        // Setup several callbacks...
        [...]

        
        timer.advance();
        System.out.println("after callback " + timer.getDeltaTime());


        // This line is critical for LWJGL's interoperation with GLFW's
        // OpenGL context, or any context that is managed externally.
        // LWJGL detects the context that is current in the current thread,
        // creates the ContextCapabilities instance and makes the OpenGL
        // bindings available for use.
        GLContext.createFromCurrent();

        
        timer.advance();
        System.out.println("after context " + timer.getDeltaTime());

        
        // Set the clear color
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        // init Gl
        glViewport(0, 0, width, height);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, width, 0, height, 1, -1);
        
        
        timer.advance();
        System.out.println("after remains " + timer.getDeltaTime());


Here is a normal case:

before show 88.39144
after show 9.680505
after callback 0.214421
after context 164.06546
after remains 0.142049


And here are the results in those cases where the loadingtime gets abnormal.




befor show 164.75491
after show 332.10287
after callback 0.142434
after context 29549.367
after remains 0.135119

before show 331.3022
after show 241.0179
after callback 0.158602
after context 8035.149
after remains 0.21981

before show 182.13263
after show 299.50977
after callback 0.156292
after context 7514.9365
after remains 0.251762

before show 255.975
after show 208.50523
after callback 1.454751
after context 13989.93
after remains 4.016637

before show 347.0369
after show 301.42838
after callback 0.222505
after context 12058.213
after remains 0.18093

before show 235.8818
after show 364.6444
after callback 0.159757
after context 27760.44
after remains 0.199023

As you can see, the call of the function GLContext.createFromCurrent() seems to be the critical one.

spasi

And are you sure this only happens when trying to open a second window? It never happens on the first one? Could you share some more details about your system? (OS, GPU, driver version, etc)

h1

Scince now i only recognized this phänomen while opening a second window. Never for the first or the subsequent ones. If this changes I will tell.

OS: Windows 7 Professional with Service Pack 1
GPU: NVIDIA NVS 3100M
Diver Version: 9.18.13.4144

Dunno what else details to give.

spasi

It could be GPU/driver related. Capabilities creation is a lot of code, but it doesn't do anything that expensive. Timings on my machine: 193ms interpreted, 4.6ms JIT compiled. Since this is called usually once per context, the interpreted number is the relevant one, but that's still too low.

Since I have no way to reproduce this, it would be extremely helpful if you could add some timings to the LWJGL code directly. I cannot offer a better solution unless we find exactly where the slowdown occurs.