CopyResource Texture Format compatibility

Started by
6 comments, last by MJP 11 years, 11 months ago
When Microsoft says:

[quote name='MSDN']
Must have compatible DXGI formats, which means the formats must be identical or at least from the same type group. For example, a DXGI_FORMAT_R32G32B32_FLOAT texture can be copied to an DXGI_FORMAT_R32G32B32_UINT texture since both of these formats are in the DXGI_FORMAT_R32G32B32_TYPELESS group.
[/quote]

how can I know which formats are compatible or in the same group?

I mean, since in the given example both formats are R32G32B32 but with different type, could also DXGI_FORMAT_D32_FLOAT and DXGI_FORMAT_R32_FLOAT be considered compatible or in the same group? (they are the same type, but one is D32 and the otherone R32).


if they are not, then how could I copy the contents of a DepthBuffer to a normal texture?

Thanks!
"lots of shoulddas, coulddas, woulddas in the air, thinking about things they shouldda couldda wouldda donne, however all those shoulddas coulddas woulddas ran away when they saw the little did to come"
Advertisement

When Microsoft says:

[quote name='MSDN']
Must have compatible DXGI formats, which means the formats must be identical or at least from the same type group. For example, a DXGI_FORMAT_R32G32B32_FLOAT texture can be copied to an DXGI_FORMAT_R32G32B32_UINT texture since both of these formats are in the DXGI_FORMAT_R32G32B32_TYPELESS group.


how can I know which formats are compatible or in the same group?

I mean, since in the given example both formats are R32G32B32 but with different type, could also DXGI_FORMAT_D32_FLOAT and DXGI_FORMAT_R32_FLOAT be considered compatible or in the same group? (they are the same type, but one is D32 and the otherone R32).


if they are not, then how could I copy the contents of a DepthBuffer to a normal texture?

Thanks!
[/quote]

What do you mean by copying depth buffer content to a "normal texture"?
A depth buffer is a texture2d with specific setup and a depthStencilView. (I assume you are using d3d10/11 from your post.) You can still have a shader resource view to sample the texture in your shaders. Or are you trying to do something different?
To answer your question though just have a look at the typeless formats in the dxgi_format list. The compatible format ones have the same format, only different interpretation of the data.
If ever you want to do something funkier, you can always copy the depth buffer in a shader by sampling it using it's shader resource view, and outputing to a rendertarget with your reinterpreted depth data. Can't think of many uses for that though.
I would just try it and find out. If it doesn't work you can always do the copy manually by writing a simple pixel shader that samples the depth buffer as a texture and writes the result to the copy texture.
Thanks ATEFred,


What do you mean by copying depth buffer content to a "normal texture"?
A depth buffer is a texture2d with specific setup and a depthStencilView. (I assume you are using d3d10/11 from your post.)


Yes, sorry... what I intended to say is to copy a depth buffer's contents into a non-depth-buffer.
(and yes, I'm using Dx11)


You can still have a shader resource view to sample the texture in your shaders. Or are you trying to do something different?


Basically, I want to read in the CPU the contents of the DepthBuffer, something like this:


// Create texture: DepthStencil | 2xMultiSampled | USAGE_DEFAULT
D3D11_TEXTURE2D_DESC descDepth;
descDepth.Width = 800;
descDepth.Height = 600;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D32_FLOAT;
descDepth.SampleDesc.Count = 2;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
ID3D11Texture2D* pDepthStencil = nullptr;
m_pDevice->CreateTexture2D( &descDepth, NULL, &pDepthStencil );

// Create texture: R32 | SingleSampled | USAGE_DEFAULT
descDepth.SampleDesc.Count = 1;
descDepth.BindFlags = D3D11_BIND_UNORDERED_ACCESS;
descDepth.Format = DXGI_FORMAT_R32_FLOAT;
ID3D11Texture2D* pTemp1 = nullptr;
m_pDevice->CreateTexture2D( &descDepth, NULL, &pTemp1 );

// Create texture: R32 | SingleSampled | USAGE_STAGING
descDepth.BindFlags = 0;
descDepth.Usage = D3D11_USAGE_STAGING;
descDepth.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
ID3D11Texture2D* pCPUReadable = nullptr;
m_pDevice->CreateTexture2D( &descDepth, NULL, &pCPUReadable );

// RENDER SOMETHING

// Src must be multisampled, Dst must be singlesampled and USAGE_DEFAULT
m_pDevice->ResolveSubresource( pTemp1, 0, pDepthStencil, 0, DXGI_FORMAT_R32_FLOAT );
// Both, Src & Dst, must be singlesampled or multisampled, must be compatible formats, have same Multisampling count and Qlty
m_pDevice->CopyResource( pCPUReadable, pTemp1 );

D3D11_MAPPED_SUBRESOURCE MappedResource;
m_pDevice->Map( pCPUReadable, 0, D3D11_MAP_READ, 0, &MappedResource );



However, no matter what MappedResource.pData is always 0000000000000000000000000000000000000000000000000000000000000000......



If ever you want to do something funkier, you can always copy the depth buffer in a shader by sampling it using it's shader resource view, and outputing to a rendertarget with your reinterpreted depth data.


I'm trying to keep away from copying it in a shader as much as possible, in theory this should be working, but if there is no way to do it without using the shader then yes, I think I will have to do it that way.


I would just try it and find out


Thanks MJP, I tryed but it doesn't send any error (the destination only keeps as 0's)

Any ideas of what could I be missing?

Thanks!
"lots of shoulddas, coulddas, woulddas in the air, thinking about things they shouldda couldda wouldda donne, however all those shoulddas coulddas woulddas ran away when they saw the little did to come"
in theory (acording to MSDN's documentation) ResolveSubresource() does not restrict the change between pDepthStencil's DXGI_FORMAT_D32_FLOAT to pTemp1's DXGI_FORMAT_R32_FLOAT.... so, by the time CopyResource() copies from pTemp1 to pCPUReadable (which is also DXGI_FORMAT_R32_FLOAT) there should be no problem (since CopyResource does restrict the copy to be some compatible format).

Unless.... in using ResolveSubresource() apply the same restrictions that on CopyResource().
"lots of shoulddas, coulddas, woulddas in the air, thinking about things they shouldda couldda wouldda donne, however all those shoulddas coulddas woulddas ran away when they saw the little did to come"
This page explicitly calls out the formats that support multisample resolve, and D32_FLOAT is not one of them.
Thanks MJP,

I can't read the table up to that column, but is good to know there is actually no way of doing it besides using a pixel shader.

Thanks!
"lots of shoulddas, coulddas, woulddas in the air, thinking about things they shouldda couldda wouldda donne, however all those shoulddas coulddas woulddas ran away when they saw the little did to come"
Oh yeah you're right, it's messed up for me too. I actually looked it up in the documentation that comes with the SDK, and then google'd for the MSDN link based on the title.

This topic is closed to new replies.

Advertisement