Question regarding pixel formats

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Post Reply
pjpetitprez
Gnoblar
Posts: 9
Joined: Wed Nov 23, 2016 5:28 pm

Question regarding pixel formats

Post by pjpetitprez »

Hello,

I am new to Ogre and I am trying to create a texture manually to use it in a shader.
I absolutely need it to have a RGBA pixel format (because I receive data from another part which is in RGBA and cannot be easily converted), but it looks like Ogre does not take my choice into account and always gives me what seems to be a BGRA texture.
I create my texture like this :

Code: Select all

    _texture= Ogre::TextureManager::getSingleton().createManual(
        "TestTexture",
        Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
        Ogre::TEX_TYPE_2D,
        256, 256,
        0,
        Ogre::PF_BYTE_RGBA,
        Ogre::TU_DYNAMIC
        );
and for testing purpose I fill it like this, and I would expect to get a red texture :

Code: Select all

    Ogre::HardwarePixelBufferSharedPtr pixelBuffer = _texture->getBuffer();
    pixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
    const Ogre::PixelBox& pixelBox = pixelBuffer->getCurrentLock();
    unsigned char* pDest = static_cast<unsigned char*>(pixelBox.data);
    for (size_t j = 0; j < 256; j++)
    {
        for (size_t i = 0; i < 256; i++)
        {
            *pDest++ = 255; 
            *pDest++ = 0; 
            *pDest++ = 0; 
            *pDest++ = 255; 
        }
    }
But when applied to a model in my shader, I get a blue texture. And of course if I fill it like this :

Code: Select all

            *pDest++ = 0; 
            *pDest++ = 0; 
            *pDest++ = 255; 
            *pDest++ = 255; 
I would expect a blue texture, but I get a red texture.
I tried to use PF_BYTE_BGRA (in hope that both were inverted) but the result is exactly the same.
Moreover, when I use

Code: Select all

_texture->getFormat()
to check which format is used, it gives me PF_A8R8G8B8, which corresponds neither to the one I set, and neither to the one I seem to get in the actual texture (alpha is the fourth component, that's for sure).

Could someone help me understanding the usage of pixel formats, or tell me what I am doing wrong ?
By the way I'm using Ogre 1.9 and OpenGL/GLSL.

Thank you very much.
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5296
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: Question regarding pixel formats

Post by dark_sylinc »

When creating an Ogre::Image (SW backed texture), you get exactly what you ask for; and conversions may be done automatically (ie. when uploading an Image in RGBA to a BGRA texture, etc)

But when creating an Ogre::Texture (HW based texture), you'll get the closest format the GPU supports that can satisfy what you asked for; thus you can't assume you will get exactly what you requested.
pjpetitprez
Gnoblar
Posts: 9
Joined: Wed Nov 23, 2016 5:28 pm

Re: Question regarding pixel formats

Post by pjpetitprez »

Thank you for the explanation.
I assume that the best for my usage (creating a texture which should be updated regularly and sent to a shader) is to use HW textures and not SW images, but can I use a Ogre::Image and send it to a shader?
Also, I have tested in OpenGL using glTexImage2D and I get a RGBA texture, does this mean that my hardware is able to manage RGBA but for some reason Ogre doesn't know it?

Thank you.
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5296
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: Question regarding pixel formats

Post by dark_sylinc »

pjpetitprez wrote:Thank you for the explanation.
I assume that the best for my usage (creating a texture which should be updated regularly and sent to a shader) is to use HW textures and not SW images, but can I use a Ogre::Image and send it to a shader?
If you send the Image to the Texture for conversion, then yes. But you can't use the Image directly because it's not in the GPU.
pjpetitprez wrote:Also, I have tested in OpenGL using glTexImage2D and I get a RGBA texture, does this mean that my hardware is able to manage RGBA but for some reason Ogre doesn't know it?
Yes, there are some oddities in the OpenGL RenderSystem from really old code that force one layout over the other; however it's never been addressed because an RGBA texture is equivalent to a BGRA texture in terms of quality and performance; and there are other oddities to consider (e.g. GLES).
pjpetitprez
Gnoblar
Posts: 9
Joined: Wed Nov 23, 2016 5:28 pm

Re: Question regarding pixel formats

Post by pjpetitprez »

Ok I have looked at the ogre.log file and on the line

Code: Select all

[GL] : Valid FBO targets
there's no PF_R8G8B8A8 written (but plenty of others like PF_B8G8R8A8 and so on)
But, I have tested Ogre 2.1 samples on the same computer and the Ogre.log says there is a PF_R8G8B8A8 available.
Does this mean that porting my app to Ogre 2 would solve my issue?
In all cases my app will be deployed on different systems, and I'm afraid to have different results on different hardware or OS.
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5296
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: Question regarding pixel formats

Post by dark_sylinc »

I fail to understand why is this an issue.

If the texture source comes from CPU side, the flip can be done during the transfer. If the texture source comes from another GPU surface, it can be done by swapping the shader swizzle.
pjpetitprez
Gnoblar
Posts: 9
Joined: Wed Nov 23, 2016 5:28 pm

Re: Question regarding pixel formats

Post by pjpetitprez »

That's true that I can use a workaround by swizzling in the shaders (since I have no idea how the texture data is created CPU-side - it comes from another part of the software) but then, I can't be sure that it will run exactly the same on different computers. That's the main problem for me, I need to be sure that the software can run on different computers without changes.
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5296
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: Question regarding pixel formats

Post by dark_sylinc »

Oh I see. I'm 99% you'll get the same behavior for RGBA8888 (or was it BGRA8888? anyway, the one that is working for you) in every computer because this is mostly an Ogre quirk where one of the formats is forced over the other behind the scenes.
What I meant is that it's not exactly a bug because you don't always get what you request. But in this case 8-bit formats are supported in all GPUs so it's safe to expect you will get always the same on all PCs.

If you're too paranoid, then you can check the value of texture->getFormat() after creating the texture and swap at runtime:

Code: Select all

const PixelFormat pf = texture->getFormat();
const bool bSwap = texture->getFormat() == PF_R8G8B8A8;
for( ... )
{
    if( bSwap )
    {
    }
    else
    {
    }
}
Cheers
Matias
pjpetitprez
Gnoblar
Posts: 9
Joined: Wed Nov 23, 2016 5:28 pm

Re: Question regarding pixel formats

Post by pjpetitprez »

Thank you for all your explanations. I feel I tried to "over-complicate" the thing...
I guess I'll stick with the working BGRA8888 format and use the paranoid check :mrgreen:
Thanks a lot for your time.
Cheers
Post Reply