SFML community forums

Help => Graphics => Topic started by: Haikarainen on June 06, 2012, 01:31:05 am

Title: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Haikarainen on June 06, 2012, 01:31:05 am
I don't know if the problem is related to SFML at all, but i'm posting it here either way!

A project of mine is using SFML to create a window as well as load imagefiles, and so far this has been going great! I'm currently working on drawing  planet earth using highresolution textures and shaders, currently I have 3 textures I want to use, all of wich are 8192x4096:

Regardless of the order I'm loading these in, the third one always fails with GL_OUT_OF_MEMORY after the methodcall gluBuild2DMipmaps. Remember I dont use sf::Texture or sf::Shader here, only sf::RenderWindow and sf::Image. The program still runs but the third texture seems to contain no pixeldata, making it transparent.

If I load the  night-texture last: https://legacy.sfmluploads.org/cache/pics/243_earthclouds.png (https://legacy.sfmluploads.org/cache/pics/243_earthclouds.png)
If I load the cloud-texture last: https://legacy.sfmluploads.org/cache/pics/244_earthnight.png (https://legacy.sfmluploads.org/cache/pics/244_earthnight.png)

I suspect it being a missing glcontext or something, therefore I wanted to use ensureGlContext to ensure it but noticed that method is protected :/ Is there anyone who knows what it might be?

The max texture size on my system is 16K, so that passes.

Full code to create a  texture:

de::Texture::Texture(de::Base* r, std::string name,std::string path){
        std::cout << "Loading texture from " << path.c_str() << "..";
        this->Root = r;
        this->Name = "__not_ready";
        this->Handle = 0;
        this->IsReady = false;
        sf::Image c;
        if(c.loadFromFile(path)){
                glGenTextures(1, &this->Handle);
                glBindTexture(GL_TEXTURE_2D, this->Handle);
                gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, c.getSize().x, c.getSize().y, GL_RGBA, GL_UNSIGNED_BYTE, c.getPixelsPtr());
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

                if(this->Root->GetGLSystem()->GetHardware()->Anisotrophy){
                        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, this->Root->GetGLSystem()->GetHardware()->Anisotrophy);
                }
                this->IsReady = true;
                this->Name = name;
                std::cout << "Created texture!" << std::endl;
        }else{
                std::cout << "Couldn't load texture from file." << std::endl;
                throw(de::Exception("Couldn't create a texture, image not valid!"));
        }
}

de::Texture::Handle is a GLuint
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Laurent on June 06, 2012, 08:25:19 am
How many MB of RAM does your graphics card have (512?)? GL_OUT_OF_MEMORY clearly states that it's full. A context error would trigger the GL_INVALID_OPERATION error.
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Haikarainen on June 06, 2012, 03:22:02 pm
How many MB of RAM does your graphics card have (512?)? GL_OUT_OF_MEMORY clearly states that it's full. A context error would trigger the GL_INVALID_OPERATION error.

I highly doubt this, I have 2 AMD HD6870 in Crossfire with 1GB each(Dont think the second gpu mem is used but still). I've also heard OUT_OF_MEMORY is triggered when a malloc() fails.

Anyway  I did some testing yesterday before I went to bed, If I loaded the first texture 10 times before loading the others, it seemed to work. Once. Then it failed again.

I have now also created my own glCheck function and wrapped all GL-calls in that, and I get no other errors than OUT_OF_MEMORY when generating mipmaps in loading textures.


EDIT: Something else I noted yesterday, apparently I dont have shader support unless I draw something to the window after creating it, and before loading resources.  That is (!GLEW_ARB_vertex_shader || !GLEW_ARB_fragment_shader) returns true. This made me suspect that sf::RenderWindow isn't that stable to use with pure OpenGL yet?
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Laurent on June 06, 2012, 03:31:18 pm
Apparently GL_OUT_OF_MEMORY can indeed be triggered if there's no context (that's weird...). Is there a chance that no context is active when you get this error?
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Haikarainen on June 06, 2012, 03:32:55 pm
Apparently GL_OUT_OF_MEMORY can indeed be triggered if there's no context (that's weird...). Is there a chance that no context is active when you get this error?

Actually read my edit, I think it might have something to do with it.
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Laurent on June 06, 2012, 03:43:35 pm
Quote
That is (!GLEW_ARB_vertex_shader || !GLEW_ARB_fragment_shader) returns true. This made me suspect that sf::RenderWindow isn't that stable to use with pure OpenGL yet?
This is not pure OpenGL, this is GLEW. Is GLEW properly initialized when you run this code?
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Haikarainen on June 06, 2012, 03:54:32 pm
Quote
That is (!GLEW_ARB_vertex_shader || !GLEW_ARB_fragment_shader) returns true. This made me suspect that sf::RenderWindow isn't that stable to use with pure OpenGL yet?
This is not pure OpenGL, this is GLEW. Is GLEW properly initialized when you run this code?

Didn't realize GLEW had to be initialized. It is initializing now and the shader-bug disappeared, anyhow still getting out of memory on texture loading :/
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Laurent on June 06, 2012, 04:14:47 pm
Ok, so let's go back to the previous question:
Quote
Is there a chance that no context is active when you get this error?
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Haikarainen on June 06, 2012, 04:28:42 pm
Im not quite sure, I've tried to use setActive on my renderwindow after creation and before creating the textures. I guess SFML is  creating this context, but all of those functions are private/protected. How can I be sure?
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Laurent on June 06, 2012, 04:37:50 pm
The design of your engine must ensure that there is always an active OpenGL context in the current thread when you call an OpenGL function. This is the most important thing to consider when writing OpenGL code.

If you need an OpenGL context but have no window, you can instanciate a sf::Context.
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Haikarainen on June 06, 2012, 04:39:53 pm
Allright, well I create the sf::RenderWindow before loading my textures, is there any way for me to make sure that its glContext is active?
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Laurent on June 06, 2012, 04:49:59 pm
window.setActive(true);

By the way, do you use threads?
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Haikarainen on June 06, 2012, 04:54:56 pm
window.setActive(true);

By the way, do you use threads?

I do not use any threading.  the setActive(true) made no difference :/ I appreciate your help though, this problem probably has little to do with sfml
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Laurent on June 06, 2012, 05:01:18 pm
If you can write a complete and minimal code that reproduces the problem, I can try to have a look at it.
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Haikarainen on June 06, 2012, 05:26:17 pm
If you can write a complete and minimal code that reproduces the problem, I can try to have a look at it.

As complete and minimal as I possible could make it and still catch errors properly:
http://fredrik.haikarainen.se/test.rar (http://fredrik.haikarainen.se/test.rar)
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Laurent on June 06, 2012, 05:51:22 pm
A 50 MB archive? That doesn't look very minimal, sorry.

Here is what I mean:
http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Haikarainen on June 06, 2012, 06:05:00 pm
A 50 MB archive? That doesn't look very minimal, sorry.

Here is what I mean:
http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368

It is minimal, i just packed the  3 textures as well. It is 1 main.cpp file that just loads the textures, opens a window and quits.

EDIT: Only code here: https://legacy.sfmluploads.org/code/123
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Laurent on June 06, 2012, 06:25:34 pm
Ah, then a simple
image.create(width, height);
has the same effect, and doesn't require an additional file.
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Haikarainen on June 06, 2012, 06:37:37 pm
Ah, then a simple
image.create(width, height);
has the same effect, and doesn't require an additional file.

That is weird, changed it to creating a texture of the same size and that seem to work. Also when I experimented yesterday and loaded like 15 different textures (most small) I got a sfml error that said something like "could not load image: out of memory". Is there any destructor or delete I am missing? Might it be that libpng or the implementation of it handles large files badly, maybe creating a faulty pixelsptr?
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Laurent on June 06, 2012, 06:48:16 pm
Hmm interesting. I have no idea what happens inside stb_image (the lib I use to load images). If you have some time and motivation, you can try debugging inside SFML/stb_image code.
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Haikarainen on June 06, 2012, 07:03:21 pm
Hmm interesting. I have no idea what happens inside stb_image (the lib I use to load images). If you have some time and motivation, you can try debugging inside SFML/stb_image code.

I just did, apparently stb takes a shortcut when dealing with pngs, and does not split the png streams as its supposed to, making  large pictures unreliable (not as complicated for programmer, speed might also increate, but memory allocation is way larger when loading the image).

I changed the files to jpegs and now it loads perfectly!
Title: Re: OpenGL Problems: GL_OUT_OF_MEMORY, ensureGlContext?
Post by: Laurent on June 06, 2012, 08:12:17 pm
Nice. Thank you for your investigation :)