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

Author Topic: When does a Window delete its Opengl Context?  (Read 9489 times)

0 Members and 1 Guest are viewing this topic.

golgoth

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
When does a Window delete its Opengl Context?
« on: January 25, 2013, 06:20:37 pm »
Hello SFML Wizards,

Quick question, once you've created a GL Context in a Window, is there a time where this context is being delete/replace before the window is finally destroy? What about resizing the Window, is the Context kept?

THX

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: When does a Window delete its Opengl Context?
« Reply #1 on: January 25, 2013, 06:32:29 pm »
The context is destroyed when the window is destroyed, and it is recreated when you call the create function. It is kept when you resize the window.
Laurent Gomila - SFML developer

golgoth

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: When does a Window delete its Opengl Context?
« Reply #2 on: January 25, 2013, 06:52:04 pm »
Marvellous, merci Laurent for your input!

cheers,

golgoth

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: When does a Window delete its Opengl Context?
« Reply #3 on: January 25, 2013, 07:19:01 pm »
hum, would it be possible to create a Window without SFML dealing with the creation of the OpenGL Context and especially, loading the OpenGL libraries?

I'm asking because I don't need SFML doing any OpenGL related stuff as I would like to handle this on my own, like creating the Context and swapping buffers?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: When does a Window delete its Opengl Context?
« Reply #4 on: January 25, 2013, 08:17:03 pm »
But then why do you need SFML? And why do you want to handle contexts, the most annoying and platform-specific part of the code?
Laurent Gomila - SFML developer

golgoth

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: When does a Window delete its Opengl Context?
« Reply #5 on: January 25, 2013, 09:01:45 pm »
Hi Laurent,

To answer your question, I use SFML mainly for the window creation and user inputs, threading and networking. I’m very happy with this but my problem is that SFML seems to takes over my graphic pipeline and I believe it might be causing very hard to find bugs that I’m trying to solve.

The reason behind my last question is that gDEBugger GL is telling me that 3 GL contexts are being created and it spits out memory leaks as some VBOs are being deleted on a context that did not created them. Why do I end up whit 3 GL Contexts is beyond my understanding right now and I’m trying to investigate why is this happening.

My guess is that SFML Window is creating its own proc addresses like this one:

        PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = reinterpret_cast<PFNWGLCREATECONTEXTATTRIBSARBPROC>(wglGetProcAddress("wglCreateContextAttribsARB"));

…and it might not play well as I also have to do it before SFML does.

I also noticed in WindowImplWin32.cpp:

#include <SFML/Window/glext/wglext.h>
#include <SFML/Window/glext/glext.h>

I might be missing something, but this is not needed over there.

The bottom line is, I’m trying to strip down any OpenGL calls from SFML Window and make sure there is no mumbo jumbo name mangling issues of some sort.

regards,

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: When does a Window delete its Opengl Context?
« Reply #6 on: January 25, 2013, 09:38:32 pm »
Quote
Why do I end up whit 3 GL Contexts is beyond my understanding right now and I’m trying to investigate why is this happening
The internal context handling in SFML is complicated. Hidden contexts are created. You shouldn't care about that, it's implementation details.

Quote
it spits out memory leaks as some VBOs are being deleted on a context that did not created them
All SFML contexts share their resources, so this shouldn't be a problem. Are you sure that the leask are caused by that? Usually, OpenGL memory leaks are caused by the graphics driver.

Quote
My guess is that SFML Window is creating its own proc addresses like this one:

        PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = reinterpret_cast<PFNWGLCREATECONTEXTATTRIBSARBPROC>(wglGetProcAddress("wglCreateContextAttribsARB"));

…and it might not play well as I also have to do it before SFML does.
It really doesn't matter. I don't "create" anything, I just get the function pointer. Yours will be the same.

Quote
I might be missing something, but this is not needed over there.
I removed them, thanks.

There shouldn't be any problem between SFML and your own code. Before rewriting parts of it you should explain your problem here.
Laurent Gomila - SFML developer

golgoth

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: When does a Window delete its Opengl Context?
« Reply #7 on: January 25, 2013, 10:19:46 pm »
Very well thank you for your support Laurent,

Quote
The internal context handling in SFML is complicated. Hidden contexts are created. You shouldn't care about that, it's implementation details.

Once the first Window is created, SFMF has created 3 different contexts. I understand resources are being shared between contexts but regarding to GL gDEBugger, it does not like it.

From:
http://www.opengl.org/sdk/docs/man/xhtml/glDeleteShader.xml

Quote
If a shader object to be deleted is attached to a program object, it will be flagged for deletion, but it will not be deleted until it is no longer attached to any program object, for any rendering context (i.e., it must be detached from wherever it was attached before it will be deleted)

I try to "set Active Window" before creation and before deletion but I’m having the same issue, any ideas?

golgoth

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: When does a Window delete its Opengl Context?
« Reply #8 on: January 26, 2013, 01:47:39 am »
I finally found the leaks, quite a catch and you were right, sharing context seems to work fine.

golgoth

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: When does a Window delete its Opengl Context?
« Reply #9 on: January 26, 2013, 04:41:26 am »
There is definitely something fishy with the contexts, It seems like there's a swap of context after the Window is created that might cause this issue. And it would be very handy to know what triggers the swap before we make GL calls.

gDEBugger returns:

Debug String: Detected error: The debugged process asked for an extension function pointer (glCreateProgram) from one render context, but called this function pointer in another render context (context #3)
Debug String: Detected error: The debugged process asked for an extension function pointer (glBindAttribLocation) from one render context, but called this function pointer in another render context (context #3)
Debug String: Detected error: The debugged process asked for an extension function pointer (glCreateShader) from one render context, but called this function pointer in another render context (context #3)
Debug String: Detected error: The debugged process asked for an extension function pointer (glShaderSource) from one render context, but called this function pointer in another render context (context #3)

SNIP...

OpenGL Render Context 3 Deleted
Debug String: Detected error: The debugged process asked for an extension function pointer (glDeleteBuffers) from one render context, but called this function pointer in another render context (context #2)
Debug String: Detected error: The debugged process asked for an extension function pointer (glUseProgram) from one render context, but called this function pointer in another render context (context #2)
Debug String: Detected error: The debugged process asked for an extension function pointer (glDetachShader) from one render context, but called this function pointer in another render context (context #2)
Debug String: Detected error: The debugged process asked for an extension function pointer (glDeleteShader) from one render context, but called this function pointer in another render context (context #2)
Debug String: Detected error: The debugged process asked for an extension function pointer (glDeleteProgram) from one render context, but called this function pointer in another render context (context #2)

It would help if:
- The dummy contexts could be destroyed leaving only the main one... or
- We could track which context is the main one (the one that uses glswapbuffers) and/or...
- A way to set the main context active before making gl calls. (I'm guessing that is "set active window" but it does not seems to do any good here).


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: When does a Window delete its Opengl Context?
« Reply #10 on: January 26, 2013, 10:05:54 am »
Dummy contexts are used only if you no "regular" context is active in the current thread. If you have a window, and you didn't call setActive(false) on it, the window's context will always be used.

Why don't you post a complete and minimal code that reproduces the problem, so that we can work on real stuff rather than on your own conclusions?
Laurent Gomila - SFML developer

golgoth

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: When does a Window delete its Opengl Context?
« Reply #11 on: January 26, 2013, 04:27:57 pm »
Yes of course,

int main()
{
        sf::Window window(sf::VideoMode(800, 600), "SFML window", sf::Style::Default);

        glewInit();

        UINT l_id = glCreateProgram();

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

        glDeleteProgram(l_id);

        return EXIT_SUCCESS;
}

Run this with:
http://www.gremedy.com/download.php

and you get:
Debug String: Detected error: The debugged process asked for an extension function pointer (glCreateProgram) from one render context, but called this function pointer in another render context (context #3)
SNIP...
Debug String: Detected error: The debugged process asked for an extension function pointer (glDeleteProgram) from one render context, but called this function pointer in another render context (context #2)
« Last Edit: January 26, 2013, 04:54:23 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: When does a Window delete its Opengl Context?
« Reply #12 on: January 26, 2013, 04:56:27 pm »
Strange... I'd have to investigate this further, but I don't have much time.
Laurent Gomila - SFML developer