I'm using SharpDX and trying to create a D3D11.Texture2D
that points to the same data as an existing D3D10.Texture2D
. I think that I need to get a DXGI.Surface
and use that to create the new texture, but I always get this unhelpful error:
HRESULT: [0x80070057], Module: [Unknown], ApiCode: [Unknown/Unknown], Message: The parameter is incorrect.
Here's my current code that compiles but doesn't work:
private D3D11.Texture2D CreateD3D11Texture2D(D3D10.Texture2D d3d10Texture2D)
{
var texDesc = new D3D11.Texture2DDescription
{
ArraySize = 1,
BindFlags = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.UnorderedAccess,
CpuAccessFlags = D3D11.CpuAccessFlags.None,
Format = DXGI.Format.B8G8R8A8_UNorm,
Height = d3d10Texture2D.Description.Height,
MipLevels = 1,
OptionFlags = D3D11.ResourceOptionFlags.None,
SampleDescription = new DXGI.SampleDescription(1, 0),
Usage = D3D11.ResourceUsage.Default,
Width = d3d10Texture2D.Description.Width
};
SharpDX.DataRectangle dataRectangle;
using (var surface = d3d10Texture2D.QueryInterface<DXGI.Surface>())
{
int pitch = d3d10Texture2D.Description.Width
* (int)DXGI.FormatHelper.SizeOfInBytes(d3d10Texture2D.Description.Format);
dataRectangle = new SharpDX.DataRectangle(surface.NativePointer, pitch);
}
return new D3D11.Texture2D(this.d3d11Device, texDesc, dataRectangle);
}
Update
I came across an answer that outlines how to share surfaces between different DirectX devices. Based on that, I believe the correct way to get a D3D11.Texture2D
from a D3D10.Texture2D
is to get a resource interface and call OpenSharedResource()
.
Doing that simplifies my function quite a bit:
private D3D11.Texture2D CreateD3D11Texture2D(D3D10.Texture2D d3d10Texture2D)
{
using (var resource = d3d10Texture2D.QueryInterface<DXGI.Resource>())
{
return this.d3d11Device.OpenSharedResource<D3D11.Texture2D>(resource.SharedHandle);
}
}
It seems to work. Is that the best way to do it?
To share data among multiple textures, get a DXGI.Resource
interface, and then call OpenSharedResource()
. Here's how do it with SharpDX:
private D3D11.Texture2D CreateD3D11Texture2D(D3D10.Texture2D d3d10Texture2D)
{
using (var resource = d3d10Texture2D.QueryInterface<DXGI.Resource>())
{
return this.d3d11Device.OpenSharedResource<D3D11.Texture2D>(resource.SharedHandle);
}
}
Here's an answer to another question with more general information about sharing surfaces between different DirectX devices.