[2.2] PixelFormat to PixelFormatGpu Conversion Issues

Discussion area about developing with Ogre-Next (2.1, 2.2 and beyond)


Post Reply
crancran
Greenskin
Posts: 138
Joined: Wed May 05, 2010 3:36 pm
x 6

[2.2] PixelFormat to PixelFormatGpu Conversion Issues

Post by crancran »

In my prior code working under OGRE 2.1, we would load our texture splatting blend masks as follows:

Code: Select all

Ogre::Image image;
image.loadDynamicImage( alphaBlendMaskBuffer, 64, 64, 1, Ogre::PF_L8, true );
  
auto alphaTexLoc = hlmsTextureManager->createOrRetrieveTexture( 
  alphaName, alphaName, TEXTURE_TYPE_NON_COLOR_DATA, &image );
  
datablock->setTexture( textureSlot, alphaTexLoc.xIdx, alphaTexLoc.texture, &alphaSampler );
datablock->setTextureUvSource( textureSlot, alphaUvSource );
If I sample just this texture in my shader, I get transitions from black to white, I would have expected.

Now porting this to 2.2, I have come up with the following code:

Code: Select all

// Continue to load the texture data into an image
Ogre::Image image;
image.loadDynamicImage( alphaBlendMaskBuffer, 64, 64, 1, Ogre::PF_L8, true );

// Create the texture, which at this point is just a descriptor
auto alphaTex = textureGpuManager->createOrRetrieveTexture(
  alphaName, GpuPageOutStrategy::AlwaysKeepSystemRamCopy, TextureFlags::ManualTexture, 
  TextureTypes::Type2D, "Terrain", 0u );
  
alphaTex->setPixelFormat( PixelFormatGpu::PFG_A8_UNORM );
alphaTex->setTextureType( TextureTypes::Type2D );
alphaTex->setResolution( 64, 64, 1 );
alphaTex->setNumMipmaps( 1u );

// Schedule texture to become resident
alphaTex->_transitionTo( GpuResidency::Resident, image.getData() );
alphaTex->_setNextResidencyStatus( GpuResidency::Resident );

// Setup staging texture
auto alphaStage = textureGpuManager->getStagingTexture( 64, 64, 1, 1, PixelFormatGpu::PFG_A8_UNORM );

// Map region and upload data specified by region
alphaStage->startMapRegion();
auto mapTexBox = alphaStage->mapRegion( 64, 64, 1, 1, PixelFormatGpu::PFG_A8_UNORM );
mapTexBox.copyFrom( image.getData(), 64, 64, 64 );
alphaStage->stopMapRegion();
alphaStage->upload( mapTexBox, alphaTex, 0, 0, true );

// Destroy staging texture
textureGpuManager->removeStagingTexture( alphaStage );
alphaStage = 0;

// Notify callbacks the data has been loaded
alphaTex->notifyDataIsReady();

datablock->setTexture( textureSlot, alphaTex, &alphaSampler );
datablock->setTextureUvSource( textureSlot, alphaUvSource );
The ironic thing is that instead of the sample being transitions between black and white like in 2.1, I am getting transitions between red and black. This leads to the shader output being blended incorrectly. I assume this has something to do with the PixelFormatGpu selected; however I am not sure what exactly I should select or if the value is right, what I have done mistakenly to account for my use case.

Ideas?
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: [2.2] PixelFormat to PixelFormatGpu Conversion Issues

Post by dark_sylinc »

Hi!
  1. Prefer PFG_R8_UNORM over PFG_A8_UNORM
  2. PF_L8 would return the same value in RGBA in GL3+, but in D3D11 it would return the value only in the R channel, while returning zero in G and B, and 1 in A. PFG_R8_UNORM behaves like the former in both APIs. Use HlmsUnlitDatablock::setTextureSwizzle to control how the channels are routed to your liking.
Cheers
crancran
Greenskin
Posts: 138
Joined: Wed May 05, 2010 3:36 pm
x 6

Re: [2.2] PixelFormat to PixelFormatGpu Conversion Issues

Post by crancran »

That certainly helped clear up the red color hue issue.

Now I changed the alpha mask code to use Ogre::PF_R8 rather than Ogre::PF_L8 when loading the buffer and storing it in an Ogre::Image. I also changed it to use Ogre::PFG_R8_SNORM as you suggested along with using R_MASK to control the channel mapping for the texture's swizzle. But what I am noticing is that the alpha texture (when sampled) appears very different between 2.1 and 2.2 for me.

The picture on the left is 2.1, the right is 2.2
Image Image

Any ideas what I could have missed or am not doing in porting this to 2.2?
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: [2.2] PixelFormat to PixelFormatGpu Conversion Issues

Post by dark_sylinc »

crancran wrote: Thu Mar 22, 2018 10:57 pm I also changed it to use Ogre::PFG_R8_SNORM as you suggested along with using R_MASK to control the channel mapping for the texture's swizzle.

Any ideas what I could have missed or am not doing in porting this to 2.2?
Probably because I suggested PFG_R8_UNORM and you're using PFG_R8_SNORM? :lol:

The UNORM variant maps [0;255] to [0;1] while the SNORM variant maps [-128; 127] to [-1; 1], thus half of the values (the brightest ones) will appear black to you.
crancran
Greenskin
Posts: 138
Joined: Wed May 05, 2010 3:36 pm
x 6

Re: [2.2] PixelFormat to PixelFormatGpu Conversion Issues

Post by crancran »

dark_sylinc wrote: Thu Mar 22, 2018 11:03 pm Probably because I suggested PFG_R8_UNORM and you're using PFG_R8_SNORM?
Oh gosh; that didn't even register. That fixed the rendering issues all together, thanks!
crancran
Greenskin
Posts: 138
Joined: Wed May 05, 2010 3:36 pm
x 6

Re: [2.2] PixelFormat to PixelFormatGpu Conversion Issues

Post by crancran »

@dark_sylinc, is it accurate that PixelFormat::PF_B8G8R8 translates to PixelFormatGpu::PFG_RGBA8_UNORM? If so, it would seem I'll need to add some logic to handle converting a 24-bit pixel to a 32-bit pixel format, right?
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: [2.2] PixelFormat to PixelFormatGpu Conversion Issues

Post by dark_sylinc »

Correct on both accounts.
Post Reply