Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialization context problem. #766

Closed
giordi91 opened this issue May 25, 2016 · 20 comments
Closed

initialization context problem. #766

giordi91 opened this issue May 25, 2016 · 20 comments
Assignees
Labels
support Windows Win32 specific (not Cygwin or WSL)
Milestone

Comments

@giordi91
Copy link

giordi91 commented May 25, 2016

Hi, I am currently trying to get shadow mapping working, and after hitting my head all over the place I cannot make it to work, so I started to compare 1:1 my code with all the tutorials online. But there is no way to get my shadow texture to render, it comes white, while following one of the tutorials I saw the guy setting major and minor context version, which I did not do, so I wondered if that could have been a problem and decided to set the version aswell, all my shader use 430 as version so I thought to set the same, here how i initialize the window:

    if (!glfwInit())
        exit(EXIT_FAILURE);

    //here we crate a pointer to our window
    GLFWwindow* window;
    //here we setup the callbacks
    glfwSetErrorCallback(error_callback);
    glfwWindowHint(GLFW_SAMPLES, 4);
    //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    //createing the window and check if it s all good
    window = glfwCreateWindow((int)settings->m_window_width, 
                              (int)settings->m_window_height, 
                              "Platfoorm 1.0", 
                              nullptr, 
                              nullptr);
    // get version info

    if (!window)
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    //setting up the context and swap interval
    glfwMakeContextCurrent(window);
    glewInit ();
    glfwSwapInterval((int)settings->m_vsync);

Now as soon as I try to set minor or major context, I start to get problem all over the place, where opengl call crash saying I am trying to write to 0x00000 etc. Basically this happens with the first function call to opengl. Also instead if I try to set opengl core profile. I get error saying that the profile works only from opengl 3.2 and up. My driver support up to opengl 4.5 and that s what I thought I was using, I have been using geometry shaders aswell which are core since 3.2 so I should at least be running a 3.2 context. I am really confused by that. Do you see anything wrong in my code? or what do you think might trigger the problem if i set the context version?
Is it there a way to query whats the version of the created context? The crashing in opengl might it be a problem with glewInit() not initializing properly if the context is too high?

Any help on the matter is much appreciated. I am running glfw 3.1.2

@giordi91
Copy link
Author

ps, I jumped back in the git revision to pretty much at the beginning of the project when I just had a triangle moving up and down, as soon as I touch GLFW_CONTEXT_VERSION nothing gets rendered at screen any-more, I don't get errors or crash just no render whatsoever.

@intrigus
Copy link

You can and should set the error callback before you initialize glfw.

@elmindreda
Copy link
Member

@giordi91 What is the output of glfwinfo -m4 -n3 -pcore -f?

@elmindreda elmindreda self-assigned this May 25, 2016
@giordi91
Copy link
Author

giordi91 commented May 25, 2016

Hi here is the output, i recompiled the latest 3.2 glfw to run glwinfo i hope that doesnt affect the result.

`Welcome to Git (version 1.9.5-preview20141217)

Run 'git help git' to display the help index.
Run 'git help ' to display help for specific commands.

$ ./glfwinfo.exe -m4 -n3 -pcore -f
GLFW header version: 3.2.0
GLFW library version: 3.2.0
GLFW library version string: "3.2.0 Win32 WGL EGL VisualC"
OpenGL context version string: "4.3.0 NVIDIA 365.19"
OpenGL context version parsed by GLFW: 4.3.0
OpenGL context flags (0x00000001): forward-compatible
OpenGL context flags parsed by GLFW: forward-compatible
OpenGL profile mask (0x00000001): core
OpenGL profile mask parsed by GLFW: core
OpenGL robustness strategy (0x00008261): none
OpenGL robustness strategy parsed by GLFW: none
OpenGL context renderer string: "GeForce GTX 960/PCIe/SSE2"
OpenGL context vendor string: "NVIDIA Corporation"
OpenGL context shading language version: "4.30 NVIDIA via Cg compiler"
OpenGL framebuffer:
 red: 8 green: 8 blue: 8 alpha: 8 depth: 24 stencil: 8
 samples: 0 sample buffers: 0
Vulkan loader: missing

The driver should support up to 4.5 no problem, same goes for the card.

@giordi91
Copy link
Author

@intrigus thanks for the tip, I moved the error calback before the init but did not change anything , and no extra output was seen at the console, aka I think the init works fine.

@elmindreda
Copy link
Member

Versioned context creation seems to work, then. This does not appear to be a bug in GLFW.

@giordi91
Copy link
Author

if the context is not specified, what will glfw create? is it possible to
create afterwards from the context?

On Fri, May 27, 2016 at 2:24 PM, Camilla Berglund [email protected]
wrote:

Versioned context creation seems to work, then. This does not appear to be
a bug in GLFW.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#766 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/ACaKZYTXwPXhDROIopRGQi50TUNOMaiOks5qFvCMgaJpZM4ImXel
.

Marco Giordano
Software Developer at Frontier Developmet
+447462721890
London(UK)
linkedin http://be.linkedin.com/in/marcogiordano3d/

@elmindreda
Copy link
Member

Sorry, I don't understand what that means.

@giordi91
Copy link
Author

if I create a glfw window without asking for a specific context version ,
like 4.3 ,3.2 core etc. what context glfw creates for me? basically what is
the default?

On Fri, May 27, 2016 at 2:44 PM, Camilla Berglund [email protected]
wrote:

Sorry, I don't understand what that means.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#766 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/ACaKZTxmMpgdm6l9avT3_lsAxSmfvUXYks5qFvVQgaJpZM4ImXel
.

Marco Giordano
Software Developer at Frontier Developmet
+447462721890
London(UK)
linkedin http://be.linkedin.com/in/marcogiordano3d/

@elmindreda
Copy link
Member

See the documentation for those hints:

GLFW_CONTEXT_VERSION_MAJOR and GLFW_CONTEXT_VERSION_MINOR are not hard constraints, but creation will fail if the OpenGL version of the created context is less than the one requested. It is therefore perfectly safe to use the default of version 1.0 for legacy code and you may still get backwards-compatible contexts of version 3.0 and above when available.

While there is no way to ask the driver for a context of the highest supported version, GLFW will attempt to provide this when you ask for a version 1.0 context, which is the default for these hints.

@elmindreda
Copy link
Member

Also note that if you request version 3.2 or greater, the context may by default have any profile.

@giordi91
Copy link
Author

Yes that s what I mean, I am sorry if I am not explaining myself correctly.

that s is how I am currently initalizing the window:
` GLFWwindow* window;
//here we setup the callbacks

window = glfwCreateWindow((int)settings->m_window_width, 
                          (int)settings->m_window_height, 
                          "Platfoorm 1.0", 
                          nullptr, 
                          nullptr);`

I am not setting any hint to glfw of the version of the context I want. In that case what kind of context will glfw try to initialize for me ? I don't think it is a 1.0 because i am using shaders and geometry shaders etc. So what I would like to know is what context version is glfw trying to initialize if not asked explicitly? If not asked explicitly will be glfw making the decision or the driver?

Either way if not explicitly set, is there a way to query with what version the context has been created? I hope it makes sense.

@giordi91
Copy link
Author

I was finally able to debug the problem, thanks to gDebugger, what i noticed was the following, when I set GLFW_CONTEXT the current context get destroyed and recreated, as you can see here:
Here in the first image i set the context and you can see deletion and creation of the context
context2

In the second image where those lines have been commented you don't get the problem.
context1

I fixed that by moving the lines:
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

before glfwInit().

After doing this all worked nicely no problem.

I just find this a bit misleading sinze i am setting a window hint, so i expect this to be related to the window and not to the library initialization. I don't think changing func names might be an option but maybe is it possible to have some runtime checks to prevent this? also any idea why the context is lost? what i mean is, looks like the current context get lost somehow when i set the context after glfwInit(), and that s why the opengl calls after that fails, although i used glfwMakeContextCurrent.
Any idea? here the full code of my initalization:

`int major, minor,rev;
glfwGetVersion(&major,&minor,&rev);
std::cout<<"Initializing glfw version: "<<major <<"."<<minor<<"."<<rev<<std::endl;

//let s initialize glfw, if that doesn't work let's kill the 
//program

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
if (!glfwInit())
    exit(EXIT_FAILURE);
glfwSetErrorCallback(error_callback);

//here we crate a pointer to our window
GLFWwindow* window;

//createing the window and check if it s all good
window = glfwCreateWindow((int)settings->m_window_width, 
                          (int)settings->m_window_height, 
                          "Platfoorm 1.0", 
                          nullptr, 
                          nullptr);

if (!window)
{
    glfwTerminate();
    exit(EXIT_FAILURE);
}

//setting up the context and swap interval
glfwMakeContextCurrent(window);
auto r = glewInit () == GLEW_OK;

glfwSwapInterval((int)settings->m_vsync);`

@elmindreda
Copy link
Member

The context is re-created because that is the correct and only way to access modern extensions to the WGL API, such as versioned context creation. One needs to create a legacy context to gain access to the newer context creation functions.

By putting the calls to glfwWindowHint before glfwInit you cause them to be ignored and emit errors, which in turn go unreported as you only set the error callback after calling glfwInit. Thus you are requesting a default 1.0 context, which doesn't require versioned context creation and so doesn't trigger context re-creation.

A tool like gDebugger should be able to cope with context re-creation, as it is a very commonly used technique.

@giordi91
Copy link
Author

Interesting thanks for clarification, oh gdebugger has not a problem with context, my application fails flat out on simple opengl commands like :

glBindBuffer(GL_ARRAY_BUFFER, m_buffer_index);
Where it returns error 1280 which should stand for invalid enum, where it works perfectly fine without setting the context version.

You mentioned that should default to opengl 1.0 context correct? If so how come I can access shaders , frame buffer etc which should be stuff only used in newer opengl version?

PS mine are not complains but genuine questions trying to figure out why app fails if context is set to any version

M.

@elmindreda
Copy link
Member

elmindreda commented May 31, 2016

You mentioned that should default to opengl 1.0 context correct? If so how come I can access shaders , frame buffer etc which should be stuff only used in newer opengl version?

This is all described in the documentation, which I linked to and quoted earlier as well.

Also, if I recall, running any but the most recent version of GLEW on a core profile context will cause GL_INVALID_ENUM, as it only recently added support for the core profile.

@giordi91
Copy link
Author

Thank you for your patience and help, I really appreciate it, mmmm I am going to double check the latest version of glew and try with this. Thank you so much.

@elmindreda elmindreda added question Please use the support label instead and removed waiting labels May 31, 2016
@elmindreda elmindreda added this to the milestone May 31, 2016
elmindreda added a commit that referenced this issue May 31, 2016
Related to #766.
@giordi91 giordi91 reopened this May 31, 2016
@giordi91
Copy link
Author

giordi91 commented May 31, 2016

Sorry to re-open the issue, but actually found what the problem was it is written here , under the the GLEW bug:

http://www.opengl-tutorial.org/miscellaneous/faq/

It is a glew bug which leads to problem, I started putting glGetError() everywhere , and got up all the way to glewInit(), if the context is set right after glewInit() i get error 1280, although glew does not return any error.

In the end i solved it adding:
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);

This fixed the problem, I also read that using glewExperimental = true; might help aswell in combination with the above line of code.

Now my last question is do I have any way to make sure that GLFW create successifully a 4.3 context and did not backfault to 3.0?

Again thanks for all the help

Also found related bug on glew repository which looks like has been sitting there for more than two years:

nigels-com/glew#3

Might be time to leave glew once and for all.

@elmindreda
Copy link
Member

Thank you for the detailed follow-up!

Now my last question is do I have any way to make sure that GLFW create successifully a 4.3 context and did not backfault to 3.0?

Just request 4.3 via the GLFW_CONTEXT_VERSION_* hints in addition to the profile hint above. If 4.3 compatibility profile (or later) isn't supported, creation will fail.

@giordi91
Copy link
Author

That s all I needed to know! Thanks a lot

M.

@elmindreda elmindreda added Windows Win32 specific (not Cygwin or WSL) and removed question Please use the support label instead labels Oct 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Windows Win32 specific (not Cygwin or WSL)
Projects
None yet
Development

No branches or pull requests

3 participants