ofFloatPixels readto pixels crash


#1

On OSX the following runs fine:

ofFloatPixels checkPixels;
fboX.readToPixels(checkPixels);

fboX is an ofFbo previously allocated as GL_RGBA32F

Problem is, I am trying to compile and run the same on Visual Studio. When I do that the readToPixels line causes a crash (bad allocation).

It’s a bit frustrating, am I missing something?

Sam


#2

Ok, so I’ve tried a load of debugging. The crash seems to be being caused when the readToPixels() function calls the TARGET_OPENGLES option:

#ifndef TARGET_OPENGLES
	getTexture(attachmentPoint).readToPixels(pixels);
#else

If I replace the readToPixels call with the following (i.e. the #else option), the code runs fine:

    ofFloatPixels checkPixels;
	int hSafe = fboX.getTextureReference().getHeight();
	int wSafe = fboX.getTextureReference().getWidth();

	checkPixels.allocate(wSafe, hSafe, ofGetImageTypeFromGLType(GL_RGBA32F));
	fboX.bind();
	int format = ofGetGLFormatFromInternal(GL_RGBA32F);
	glReadPixels(0, 0, wSafe, hSafe, format, GL_FLOAT, checkPixels.getData());
	fboX.unbind();

So, this is a workaround, but can someone explain why? I am calling some openCL kernels elsewhere in the code, would that cause a clash? The error message is a bad_alloc, so might be out of memory…?

S


#3

Aaaargggg. Looks like the above is not a fix, after all. I am still getting the crash on the PC compile.

I’ve tried unpacking the methods but it isn’t getting me very far. The below is what I have at present. This is crashing on PC in release mode, but doesn’t seem to in debug mode. On Mac it runs fine.

    ofFloatPixels checkPixels;
	int hSafe = fboX.getTextureReference().getHeight();
	int wSafe = fboX.getTextureReference().getWidth();
	ofTexture texturePass = fboX.getTexture();

	checkPixels.allocate(wSafe, hSafe, ofGetImageTypeFromGLType(GL_RGBA32F));
#ifndef TARGET_OPENGLES
	ofSetPixelStoreiAlignment(GL_PACK_ALIGNMENT, checkPixels.getWidth(), checkPixels.getBytesPerChannel(), checkPixels.getNumChannels());
	glBindTexture(texturePass.texData.textureTarget, texturePass.texData.textureID);
	glGetTexImage(texturePass.texData.textureTarget, 0, ofGetGlFormat(checkPixels), GL_FLOAT, checkPixels.getData());
	glBindTexture(texturePass.texData.textureTarget, 0);
#else
	fboX.bind();
	int format = ofGetGLFormatFromInternal(GL_RGBA32F);
	glReadPixels(0, 0, wSafe, hSafe, format, GL_FLOAT, checkPixels.getData());
	fboX.unbind();
#endif

It also seems as though when I just call fboX.readtoPixels() to ofPixels, not ofFloatPixels, the crash on PC doesn’t occur either.

As I mentioned before, the error message is a ‘std::bad_alloc at memory location…’ one, which if I am guessing correctly, may be to do with the pixel allocation. But I may just be wrong.

Can anyone help? It is driving me a bit mad.

In hope
Sam

[update] I’ve been running cout<< checks, from which it looks like the PC version consistently crashes on the checkPixels.allocate() line, the second time it calls it.


#4

Well, I decided that there must be a memory overload issue here. So I tested out reading an ‘empty’ fbo to the pixels before and after each cycle to ‘purge’ them.

Still not 100% sure why the crash was happening, as it definitely doesn’t with normal ofPixels rather than ofFloatPixels, but anyway, the following works, so far:

    ofFloatPixels exposurePixels;
    ofFbo purger;

    purger.allocate(1, 1, GL_RGBA32F);
    purger.clear();
    purger.readToPixels(exposurePixels);
    
    int hSafe = fboX.getTextureReference().getHeight();
    int wSafe = fboX.getTextureReference().getWidth();
    fboX.readToPixels(exposurePixels);

//do stuff with exposurePixels, get etc.....

     purger.readToPixels(exposurePixels);

Any explanations or advice would be super welcome
S


#5

sigh.

The above only seems to reduce how often the crash happens, rather than fix it.
I have no idea, I’ve tried everything I can think of and am still getting the same occasional bad_alloc.

S


#6

On windows only anything between the
#ifndef and #else will be called as it is not OpenGL ES

#ifndef TARGET_OPENGLES
//Windows stuff here
#else
#endif

Maybe try allocating your pixels with the correct format, and do this test in a new app that does not do anything else to isolate the problem. You are allocating the pixel object with an OF_IMAGE_TYPE not a pixel type, which should be ok, but had given me problems on windows.

ofGetImageTypeFromGLType(GL_RGBA32F));
	fboX.bind();

Try;
OF_PIXELS_RGBA in the allocation.