Crash using setTextureCreationFlag(irr::video::ETCF_ALWAYS_)

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
floriang
Posts: 13
Joined: Mon Jul 14, 2014 12:03 pm

Crash using setTextureCreationFlag(irr::video::ETCF_ALWAYS_)

Post by floriang »

Hello,

I got an access violation exception when using irr::video::ETCF_ALWAYS_16_BIT for texture loading from PNG file.
Actually it works fine for most of texture, but it crashes when trying to load a big (2301x2308) texture atlas.

Below is my helper code for loading a texture. When loading icons i would like to force 16bit format(with alpha) to save memory:

Code: Select all

 
    static irr::video::ITexture* LoadIconTextureFromFile(
        irr::IrrlichtDevice* dev, const std::string& path, bool use16bit = false ) 
    {
        if( !dev ) {
            return NULL;
        }
 
        irr::video::ITexture* ret = NULL;
        irr::video::IVideoDriver* driver = dev->getVideoDriver();
        bool flg_mip = driver->getTextureCreationFlag(irr::video::ETCF_CREATE_MIP_MAPS);
        bool flg_pow2 = driver->getTextureCreationFlag(irr::video::ETCF_ALLOW_NON_POWER_2);
        bool flg_optimized_quality = driver->getTextureCreationFlag(irr::video::ETCF_OPTIMIZED_FOR_QUALITY);
        bool flg_optimized_speed = driver->getTextureCreationFlag(irr::video::ETCF_OPTIMIZED_FOR_SPEED);
        bool flg_32bit = driver->getTextureCreationFlag(irr::video::ETCF_ALWAYS_32_BIT);
        bool flg_16bit = driver->getTextureCreationFlag(irr::video::ETCF_ALWAYS_16_BIT);
 
        driver->setTextureCreationFlag(irr::video::ETCF_CREATE_MIP_MAPS, false);
        driver->setTextureCreationFlag(irr::video::ETCF_ALLOW_NON_POWER_2, true);
        if( use16bit ) {
            driver->setTextureCreationFlag(irr::video::ETCF_ALWAYS_16_BIT, true);
            driver->setTextureCreationFlag(irr::video::ETCF_ALWAYS_32_BIT, false);
            driver->setTextureCreationFlag(irr::video::ETCF_OPTIMIZED_FOR_QUALITY, false);
            driver->setTextureCreationFlag(irr::video::ETCF_OPTIMIZED_FOR_SPEED, false);
        }
        if( FileSystem::FileExists(path) ) {
            ret = driver->getTexture(path.c_str());
        } else {
            LOG_ERROR "texture file missing: %s\n", path.c_str() LOG_END;
        }
        driver->setTextureCreationFlag(irr::video::ETCF_CREATE_MIP_MAPS, flg_mip);
        driver->setTextureCreationFlag(irr::video::ETCF_ALLOW_NON_POWER_2, flg_pow2);
        if( use16bit ) {
            driver->setTextureCreationFlag(irr::video::ETCF_ALWAYS_16_BIT, flg_16bit);
            driver->setTextureCreationFlag(irr::video::ETCF_ALWAYS_32_BIT, flg_32bit);
            driver->setTextureCreationFlag(irr::video::ETCF_OPTIMIZED_FOR_QUALITY, flg_optimized_speed);
            driver->setTextureCreationFlag(irr::video::ETCF_OPTIMIZED_FOR_SPEED, flg_optimized_quality);
        }
        return ret;
    }
 
For the background I need to run my code on a device running opengl ES with hard memory limitations, and my 2301x2308 texture takes currently about 20MB in memory (and I may have several to load), so if I could half this it would be great.
I work both on windows and android, and I get a similar crash on both systems.
They have enough video memory to load the texture (they can load it in 32BIT mode).

My environments are:
Windows
Irrlicht version 1.8.0
Visual Studio 2010 premium
Using OpenGL driver

Android
Irrlicht 1.9.0
Eclipse ADT (using android NDK r10b, ADT Version: 23.0.6.1720515)
Using OpenGL ES 2.0 driver

Any ideas?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Crash using setTextureCreationFlag(irr::video::ETCF_ALWA

Post by mongoose7 »

If you work on Windows, get a back trace of the fault.
floriang
Posts: 13
Joined: Mon Jul 14, 2014 12:03 pm

Re: Crash using setTextureCreationFlag(irr::video::ETCF_ALWA

Post by floriang »

Actually I got this stack trace on android (didn't retry on windows yet):

Code: Select all

01-01 12:55:02.370: I/DEBUG(2159):     #04  pc 00574008  /data/app-lib/com.mylib.mysampleappli-2/libMyLib.so (irr::video::COGLES2Texture::uploadTexture(bool, unsigned int, bool, void*, unsigned int)+1604)
01-01 12:55:02.370: I/DEBUG(2159):     #05  pc 00571604  /data/app-lib/com.mylib.mysampleappli-2/libMyLib.so (irr::video::COGLES2Texture::COGLES2Texture(irr::video::IImage*, irr::core::string<char, irr::core::irrAllocator<char> > const&, void*, irr::video::COGLES2Driver*)+1128)
01-01 12:55:02.370: I/DEBUG(2159):     #06  pc 005604f0  /data/app-lib/com.mylib.mysampleappli-2/libMyLib.so (irr::video::COGLES2Driver::createDeviceDependentTexture(irr::video::IImage*, irr::core::string<char, irr::core::irrAllocator<char> > const&, void*)+192)
01-01 12:55:02.370: I/DEBUG(2159):     #07  pc 0052c110  /data/app-lib/com.mylib.mysampleappli-2/libMyLib.so (irr::video::CNullDriver::loadTextureFromFile(irr::io::IReadFile*, irr::core::string<char, irr::core::irrAllocator<char> > const&)+164)
01-01 12:55:02.370: I/DEBUG(2159):     #08  pc 0052be30  /data/app-lib/com.mylib.mysampleappli-2/libMyLib.so (irr::video::CNullDriver::getTexture(irr::core::string<char, irr::core::irrAllocator<char> > const&)+472)
PS: I'm starting to debug from there...
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: Crash using setTextureCreationFlag(irr::video::ETCF_ALWA

Post by thanhle »

I'm not sure, but could be 2^n texture?
Also, Width = Height?

Regards
floriang
Posts: 13
Joined: Mon Jul 14, 2014 12:03 pm

Re: Crash using setTextureCreationFlag(irr::video::ETCF_ALWA

Post by floriang »

So now I found that for opengl ES 2.0 driver, the crash is occuring in COGLES2Texture::uploadTexture when calling glTexImage2D with the following arguments

This is in 16BIT mode (crash)
glTexImage2D(3553,0,6408,2301,2308,0,6408,32820,7AE88008)
Actually meaning

Code: Select all

 
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,2301,2308,0,GL_RGBA,GL_UNSIGNED_SHORT_5_5_5_1,7AE88008)
 
This is in 32BIT mode (no crash)
glTexImage2D(3553,0,32993,2301,2308,0,32993,5121,7A467008)
Actually meaning

Code: Select all

 
glTexImage2D(GL_TEXTURE_2D,0,GL_BGRA_EXT,2301,2308,0,GL_BGRA_EXT,GL_UNSIGNED_BYTE,7A467008)
 
I don't see anything really wrong here.
Maybe the usage of the GL_BGRA_EXT instead of GL_RGBA is suspicious, but I don't see why it would work for the small textures and not the big one.

Below the the extract of COGLES2Texture::uploadTexture where the issue happens:

Code: Select all

 
COGLES2Texture::uploadTexture (...)
{
// ... some code above ...
    if (newTexture)
    {
        MYLOG_TRACE "IsCompressed=%d", (int)IsCompressed MYLOG_END;
        if (IsCompressed)
        {
            glCompressedTexImage2D(tmpTextureType, 0, InternalFormat, image->getDimension().Width,
                image->getDimension().Height, 0, compressedImageSize, source);
        }
        else
        {
            MYLOG_TRACE "image=%08X", (unsigned int)image MYLOG_END;
            MYLOG_TRACE
                "glTexImage2D(%d,%d,%d,%d,%d,0,%d,%d,%08X)",
                (int)tmpTextureType, (int)level, (int)InternalFormat, (int)image->getDimension().Width,
                (int)image->getDimension().Height,(int)PixelFormat, (int)PixelType, (unsigned int)source
            MYLOG_END;
            glTexImage2D(tmpTextureType, level, InternalFormat, image->getDimension().Width,
                image->getDimension().Height, 0, PixelFormat, PixelType, source);
            MYLOG_TRACE "glTexImage2D call end" MYLOG_END;
        }
    }
 // ... some code below ...
}
 
floriang
Posts: 13
Joined: Mon Jul 14, 2014 12:03 pm

Re: Crash using setTextureCreationFlag(irr::video::ETCF_ALWA

Post by floriang »

thanhle wrote:I'm not sure, but could be 2^n texture?
Also, Width = Height?

Regards
I already tried to force power of 2 textures but no luck.
And my small textures are not power of 2 anyway, and they don't have issues getting loaded even in 16BIT.
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Re: Crash using setTextureCreationFlag(irr::video::ETCF_ALWA

Post by Nadro »

Please also check ETCF_ALLOW_MEMORY_COPY flag. This flag is disabled by default.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
Post Reply