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

FrameBuffer::clearColor not working #16

Closed
Gerard097 opened this issue Mar 19, 2019 · 6 comments
Closed

FrameBuffer::clearColor not working #16

Gerard097 opened this issue Mar 19, 2019 · 6 comments

Comments

@Gerard097
Copy link

Gerard097 commented Mar 19, 2019

I've succesfully installed the base-qt bootstrap, however, I have a little problem using the function clearColor which in fact doesn't change the clear color from the default buffer.

with the given paintGL function:

void MyApplication::paintGL() {
    /* Reset state to avoid Qt affecting Magnum */
    GL::Context::current().resetState(GL::Context::State::ExitExternal);

    /* Using framebuffer provided by Qt as default framebuffer */
    auto qtDefaultFramebuffer = GL::Framebuffer::wrap(defaultFramebufferObject(), {{}, {width(), height()}});
    qtDefaultFramebuffer.clearColor(GL_COLOR_ATTACHMENT0, Math::Color4<Float>::red());
    qtDefaultFramebuffer.clear(GL::FramebufferClear::Color);
    /* TODO: Add your drawing code here */

    /* Clean up Magnum state and back to Qt */
    GL::Context::current().resetState(GL::Context::State::EnterExternal);
}

I added the clearColor call before clearing the buffer, but it doesn't work. So i tried using the native opengl function which actually worked as spected. I'm not sure if i'm doing something wrong or it's a bug.

void MyApplication::paintGL() {
    /* Reset state to avoid Qt affecting Magnum */
    GL::Context::current().resetState(GL::Context::State::ExitExternal);

    /* Using framebuffer provided by Qt as default framebuffer */
    auto qtDefaultFramebuffer = GL::Framebuffer::wrap(defaultFramebufferObject(), {{}, {width(), height()}});
    //qtDefaultFramebuffer.clearColor(GL_COLOR_ATTACHMENT0, Math::Color4<Float>::red());
    glClearColor(1.f, 0.f, 0.f, 1.f);
    qtDefaultFramebuffer.clear(GL::FramebufferClear::Color);
    /* TODO: Add your drawing code here */

    /* Clean up Magnum state and back to Qt */
    GL::Context::current().resetState(GL::Context::State::EnterExternal);
}

OS: Windows 10
IDE: Visual Studio 2017
Qt: 5.12
Both Magnum and Corrade where compiled from master branch.

@mosra
Copy link
Owner

mosra commented Mar 19, 2019

Hi, thanks for the detailed report!

Hmm. Actually I think I know what's happening here -- Framebuffer::clearColor() is not equivalent to glClearColor() but rather glClearBuffer(), a slightly different (and newer) API. So then, what the code in the first listing is doing is basically this:

  1. calling the clearColor() clears the framebuffer with a red color (instead of setting the clear color to red, as you assumed)
  2. then, calling the clear() clears the framebuffer again, but with the globally set clear color (so it basically clears back to black and so it looks like nothing was changed)

Setting the clear color (without clearing anything) is done using GL::Renderer::setClearColor() instead, so to fix the above either:

  • remove the second clear(), and call just clearColor(), or
  • call GL::Renderer::setClearColor(Color4::red()) instead of qtDefaultFramebuffer.clearColor()

What might be helpful for you -- the documentation has a table mapping from GL APIs to Magnum APIs, so if you know what you want to do in raw GL, you can use the table to see the equivalent Magnum APIs. It's also possible to use the search to search for e.g. glClearColor and it'll show what function it corresponds to. Every function documentation also lists GL APIs used underneath.

Hope this helps! :)


Originally I thought you're hitting a driver bug because very recently I was pushing some workarounds for Intel drivers on Windows where using clearColor() didn't do anything but clear() did. But since you're using master, you should be using the workaround already (if on an Intel GPU) and it should be listed in the engine startup log.

@Gerard097
Copy link
Author

Thanks a lot for the fast and detailed explanation. I was confused because I misinterpreted the documentation of GL::Renderer::setClearColor but now it is "clear" to me.

Now, I just tried both approaches:

void MyApplication::initializeGL() {
    _context.create();
  
    GL::Renderer::setClearColor(Color4::red());
    /* TODO: Add your initialization code here */
}

void MyApplication::paintGL() {
    /* Reset state to avoid Qt affecting Magnum */
    GL::Context::current().resetState(GL::Context::State::ExitExternal);

    /* Using framebuffer provided by Qt as default framebuffer */
    auto qtDefaultFramebuffer = GL::Framebuffer::wrap(defaultFramebufferObject(), {{}, {width(), height()}});
    qtDefaultFramebuffer.clear(GL::FramebufferClear::Color);
    /* TODO: Add your drawing code here */

    /* Clean up Magnum state and back to Qt */
    GL::Context::current().resetState(GL::Context::State::EnterExternal);
}

This one worked as spected cleaning the buffer with red color.

void MyApplication::initializeGL() {
    _context.create();
    /* TODO: Add your initialization code here */
}

void MyApplication::paintGL() {
    /* Reset state to avoid Qt affecting Magnum */
    GL::Context::current().resetState(GL::Context::State::ExitExternal);

    /* Using framebuffer provided by Qt as default framebuffer */
    auto qtDefaultFramebuffer = GL::Framebuffer::wrap(defaultFramebufferObject(), {{}, {width(), height()}});
    qtDefaultFramebuffer.clearColor(GL_COLOR_ATTACHMENT0, Color4::green());
    /* TODO: Add your drawing code here */

    /* Clean up Magnum state and back to Qt */
    GL::Context::current().resetState(GL::Context::State::EnterExternal);
}

Unfortunately this one didn't work, leaving the frame buffer with a gray-black color instead of green. Is GL_COLOR_ATTACHMENT0 the right attachment to clear?

This is the startup info:

Renderer: GeForce GTX 1050/PCIe/SSE2 by NVIDIA Corporation
OpenGL version: 4.6.0 NVIDIA 399.07
Using optional features:
GL_ARB_ES2_compatibility
GL_ARB_ES3_1_compatibility
GL_ARB_direct_state_access
GL_ARB_get_texture_sub_image
GL_ARB_invalidate_subdata
GL_ARB_multi_bind
GL_ARB_robustness
GL_ARB_separate_shader_objects
GL_ARB_texture_filter_anisotropic
GL_ARB_texture_storage
GL_ARB_texture_storage_multisample
GL_ARB_vertex_array_object
GL_KHR_debug
Using driver workarounds:
no-layout-qualifiers-on-old-glsl
nv-zero-context-profile-mask
nv-implementation-color-read-format-dsa-broken
nv-windows-dangling-transform-feedback-varying-names
nv-cubemap-inconsistent-compressed-image-size
nv-cubemap-broken-full-compressed-image-query
nv-compressed-block-size-in-bits

@mosra
Copy link
Owner

mosra commented Mar 19, 2019

Haha, right, the GL_COLOR_ATTACHMENT0 looked suspicious to me. Now I know why :) The index passed to clearColor() is interpreted as GL_COLOR_ATTACHMENT0 + index, so in this case you're clearing atttachment number 36064 ;)

Try this instead:

qtDefaultFramebuffer.clearColor(0, Color4::green());

Also, the code you have above should throw some GL error. You can either check GL errors manually (which is tedious), or you can run the app with --magnum-gpu-validation on passed on the command line (or, equivalently, setting the MAGNUM_GPU_VALIDATION=ON environment variable since I'm not sure how Qt propagates the command-line arguments). With this enabled, the driver should print a detailed info about every GL error that happens, NVidia drivers are exceptionally good at showing what's wrong. A bit of caution though -- it might happen that Qt itself would be spamming the output with its own GL errors in which case it would be hard to know what's your error and what Qt's. I'm being optimistic and trust Qt to not cause any GL error, but I never verified that myself so I can't be sure :)

In general, none of the Magnum APIs expect raw GL enums, it's always either a numeric value or a C++11 scoped enum wrapping the raw GL values in a more typesafe way.

@Gerard097
Copy link
Author

Alright that was it :). I added the command in the build configuration and the console was displaying some clear errors.

Thanks for the tips, I'll take them in consideration.

@mosra
Copy link
Owner

mosra commented Mar 19, 2019

Great :)

Anything else or can this be closed as resolved? :) For general questions you can join the Gitter channel (simply log in with your GitHub account), usually there's always someone around who's happy to help.

@Gerard097
Copy link
Author

Yes, that's all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants