SharpDX:从D3D10.Texture2D创建D3D11.Texture2D?

时间:2013-03-21 19:33:05

标签: directx sharpdx

我正在使用SharpDX并尝试创建一个D3D11.Texture2D,指向与现有D3D10.Texture2D相同的数据。我认为我需要获得DXGI.Surface并使用它来创建新纹理,但我总是得到这个无用的错误:

  

HRESULT:[0x80070057],模块:[Unknown],ApiCode:[Unknown / Unknown],消息:参数不正确。

这是我当前编译但不起作用的代码:

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);
}

更新

我找到了一个概述how to share surfaces between different DirectX devices的答案。基于此,我认为从D3D11.Texture2D获取D3D10.Texture2D的正确方法是获取资源界面并调用OpenSharedResource()

这样做可以简化我的功能:

private D3D11.Texture2D CreateD3D11Texture2D(D3D10.Texture2D d3d10Texture2D)
{
    using (var resource = d3d10Texture2D.QueryInterface<DXGI.Resource>())
    {
        return this.d3d11Device.OpenSharedResource<D3D11.Texture2D>(resource.SharedHandle);
    }
}

似乎有效。这是最好的方法吗?

1 个答案:

答案 0 :(得分:4)

要在多个纹理之间共享数据,请获取DXGI.Resource接口,然后调用OpenSharedResource()。以下是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另一个问题,其中包含有关在不同DirectX设备之间共享曲面的更一般信息。