Page 1 of 1

[2.2] Save depth texture to file

Posted: Thu May 16, 2019 8:57 pm
by xrgo
Hello! I need to take a screenshot and also save the depthmap for that capture in a separate file, what would be the easiest way to do that in 2.2? I can imagine some ways to do it alla 2.1, but I am guessing en 2.2 there is a more straightforward way to do it?

thanks in advance!!

Re: [2.2] Save depth texture to file

Posted: Sat May 18, 2019 11:01 pm
by xrgo
I see that its recommended to copy the depth buffer in to another texture, even in 2.2 (due to hw limitations/performance) that's the "2.1 way" I knew, so I implemented it and it works!

buuuuuuuuuuut!!! it onIy works when I use 8 bit pixel format... and I really need to store the image as 16 or 32 bit... I tried lots of combinations of pixel formats and save extensions and always get a 0 bit size file :(

which pixel format should I use and in what extensions should I save? os its not supported? should I use the library mantioned here (DirectXTex) viewtopic.php?f=25&t=94913? sounds like it wont work with opengl :P

Re: [2.2] Save depth texture to file

Posted: Sun May 19, 2019 1:19 am
by dark_sylinc
I can't try anything right now, I'm away.

Copying TextureGpu -> StagingTexture should work, but there may be minor GL caveats (I haven't tried).
Since all our download code uses (requires) StagingTexture, then copying to another texture shouldn't be needed. If copying to another texture works for you and workarounds those untested caveats, then great.

As for the pixel format thingy, that's a codec thing. I know OITD codec (our internal format) will work, but that is not readable by any useable tool.

FreeImageCodec2 only supports up to 16 bits per channel
DDSCodec2::encode hasn't been ported yet but should support it.
EXR codec hasn't been ported but perhaps it's easy to port to 2.2? I don't know if it ever supported encoding
KTX (ETCCodec) also supports float32 but we haven't written the encode() functionality

Re: [2.2] Save depth texture to file

Posted: Sun May 19, 2019 5:59 pm
by xrgo
dark_sylinc wrote:
Sun May 19, 2019 1:19 am
If copying to another texture works for you and workarounds those untested caveats, then great.
yes, for time sake I am going to leave it like this, I just need it for taking captures so I don't care it there's a better way
dark_sylinc wrote:
Sun May 19, 2019 1:19 am
FreeImageCodec2 only supports up to 16 bits per channel
that's enough for my needs!! do you mean this is implemented/integrated in to ogre? how do I use it?

Re: [2.2] Save depth texture to file

Posted: Sun May 19, 2019 8:09 pm
by xrgo
ok, turns out it wasnt that difficult to integrate DirectXtex library and its working!

this is how I save a PFG_R16_FLOAT texture:

Code: Select all

                Ogre::Image2 image;
                image.convertFromTexture( mScreenShotTextureDepth, 0, 0 );

                DirectX::Image dxImage;
                dxImage.width = mScreenShotTextureDepth->getWidth();
                dxImage.height = mScreenShotTextureDepth->getHeight();
                dxImage.format = DXGI_FORMAT_R16_FLOAT;
                dxImage.pixels = static_cast<uint8_t*>( image.getData( 0 ).atFromOffsettedOrigin( 0, 0, 0 ) );

                size_t rowPitch;
                size_t slicePitch;

                DirectX::ComputePitch( dxImage.format, dxImage.width, dxImage.height,
                                       rowPitch, slicePitch, DirectX::CP_FLAGS_NONE );

                dxImage.rowPitch = rowPitch;
                dxImage.slicePitch = slicePitch;

                std::string fileName = "./Captures/" + yUtils::getDateInStandardString() + "_Depth.png";

                wchar_t* wide_string = new wchar_t[ fileName.length() + 1 ];
                std::copy( fileName.begin(), fileName.end(), wide_string );
                wide_string[ fileName.length() ] = 0;

                DirectX::SaveToWICFile( dxImage, DirectX::WIC_FLAGS_NONE, DirectX::GetWICCodec(DirectX::WIC_CODEC_PNG), wide_string );
                delete [] wide_string;
still would be nicer to have ogre do it so I don' t have to include another dependency, but not a real problem

thanks!!