Sign in to follow this  
xlope01

ID3D11ShaderResourceView save to file

Recommended Posts

Have you taken a look at the DirectXTK library?  I believe this method is already implemented for you, or at least it can provide you with a reference implementation to start from...

This is exactly what I need from the DirectXTK library:

HRESULT SaveDDSTextureToFile( _In_ ID3D11DeviceContext* pContext,
                                  _In_ ID3D11Resource* pSource,
                                  _In_z_ LPCWSTR fileName );

however this library need Windows 8 SDK(that needs w8, and I don't have). It looks like only for this method I only missing <DirectXMath.h> and <wrl.h>, any suggestion ?

 

edit: I currently use DirectX SDK (June 2010)

Edited by xlope01

Share this post


Link to post
Share on other sites

The Windows 8 SDK can be installed on Win 7. Haven't tried myself, but one can even use it with VS 2010 as it seems. (Edit, corrected link).
 
Since you're using the June 2010 DX SDK, you can take a look at the D3DX legacy library, in particular D3DX11SaveTextureToFile.

 

"D3DX11SaveTextureToFile" was the first thing I did before trying anything else, but it didn't work, it just writes an empty file(16mb size).

ID3D11ShaderResourceView* my_ID3D11ShaderResourceView ;
ID3D11Resource* tempResource;
my_ID3D11ShaderResourceView->GetResource(&tempResource);
D3DX11SaveTextureToFile(m_D3D->GetDeviceContext(), tempResource, D3DX11_IFF_DDS, L"file.dds");
Edited by xlope01

Share this post


Link to post
Share on other sites
I'm currently not at home to test this, but maybe you need a temporary staging texture still, like Zaoshi Kaba suggests. Things to try:
 
- What HRESULT do you get from that call ?
- Compile with the debug library (d3dx11d.lib)
 
Also, don't forget to [tt]tempResource->Release()[/tt] when you're done.

Share this post


Link to post
Share on other sites

I'm currently not at home to test this, but maybe you need a temporary staging texture still, like Zaoshi Kaba suggests. Things to try:
 
- What HRESULT do you get from that call ?
- Compile with the debug library (d3dx11d.lib)
 
Also, don't forget to [tt]tempResource->Release()[/tt] when you're done.

HRESULT gives  S_OK

I have try everything i can with the "D3DX11SaveTextureToFile" but still cant get it working, and i don't want/need to migrate from June 2010 DX SDK to Windows 8 SDK to use the "SaveDDSTextureToFile".

Share this post


Link to post
Share on other sites
Strange. That function should work fine even for non-staging textures (IIRC it will create a temporary automatically). Fire up PIX and see what's going on.

Edit: How do you actually populate the texture in the first place ? Load it ? Fill it manually with Map/Update or at creation ? Is it also a render target ? Edited by unbird

Share this post


Link to post
Share on other sites

Strange. That function should work fine even for non-staging textures (IIRC it will create a temporary automatically). Fire up PIX and see what's going on.

Edit: How do you actually populate the texture in the first place ? Load it ? Fill it manually with Map/Update or at creation ? Is it also a render target ?

 

It is a render target(2048x2048).

As for PIX I can't get it to work for some reason.

Share this post


Link to post
Share on other sites
Read here and especially here. Use at your own risk, but uninstalling that platform update worked fine for me.

Edit: Ah yeah, what did you mean by "empty file" ? All zeroes (so no valid dds file at all) ? Black texture ? How did you check that ? Edited by unbird

Share this post


Link to post
Share on other sites

if i open the dds file with a text editor the first line is this

DDS |             0                                                            DX10                                          
 

and after that it is filled with spaces until the end of the file. (size16MB for a 2048x2048 render target)

of course I can not open the file in any photo/paint editor, it says error etc

Share this post


Link to post
Share on other sites

Strange. I tried something similar before suggesting: I saved (using SlimDX, which uses the above function under the hood) the backbuffer texture 1920x1080 with four channel half-floats. DxTex - both the 32 and 64 bit - load them fine. I don't see such a DX10 marker with a raw/hex viewer though. Is there some trigger (say, size) which "makes" it a DX10 DDS only?

 

Anyway: xlope01, could you upload this file somewhere, please ?

 

Also: you can "test" it with the corresponding loading function D3DX11CreateTextureFromFile and render it. Maybe first try a non-DDS format.

 

Edit: Update: Could go up to 4096^2 and again both DxTex load them fine. @xlope: Something is still off at your side I think. A 2048^2 texture should be 32 MB in size (2048*2048*4*2 = 33'554'432). And another [b]Edit[/b]: Wait, maybe I'm silly now, I don't know if DDS does support compression. But a 2048^2 texture saves to approximately 32 MB on my side.

Edited by unbird

Share this post


Link to post
Share on other sites

IT is actually working if I load with D3DX11CreateShaderResourceViewFromFile from the dds file that I make with D3DX11SaveTextureToFile (not sure why i can not open it with any photo/paint editor or  DxTex)

2048^2 is 32MB with R16G16B16A16, and with R11G11B10 is 16MB

Share this post


Link to post
Share on other sites
Aaaah, there's the problem. R11G11B10_FLOAT is a newer format. Tried it, suddenly I've got this DX10 marker. Jep, DxTex doesn't cope with that.
 
Pity that. I think you have to live with it, unfortunately. Neither can I name you a tool which supports it, sorry.
 
Just as an aside: Make sure to get PIX working. Here's a screenshot from the image save call.
82a105274537842.gif

One can see clearly what's happening, namely the exact sequence Zaoshi Kaba described (the last couple of events in the list). The temporary texture also gets a nice debug name.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this