microsoft / D3D11On12

The Direct3D11-On-12 mapping layer
MIT License
274 stars 33 forks source link

`ID3D11Texture2D::QueryInterface<IDXGISurface>` 0x80004002 : 'No such interface supported' #49

Closed tom-huntington closed 5 months ago

tom-huntington commented 5 months ago

I'd like to render to a D3D12 resource from D2D.

However, when I am wrapping a resource created with ID3D12Device::CreateCommittedResource (rather than a swapchain buffer) I cant obtain a IDXGISurface needed for D2D interop.

Could I get some guidance?

auto desc = d3dx12::CD3DX12_RESOURCE_DESC::Tex2D(
    DXGI_FORMAT_R8G8B8A8_UNORM, text_metrics.width, 53);
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;

winrt::com_ptr<ID3D12Resource> d3d12Resource;
auto heapProperties = d3dx12::CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
winrt::check_hresult(resources.D3D12Device().CreateCommittedResource(
    &heapProperties,
    D3D12_HEAP_FLAG_NONE,
    &desc,
    D3D12_RESOURCE_STATE_COMMON,
    nullptr,
    IID_PPV_ARGS(d3d12Resource.put())));

D3D11_RESOURCE_FLAGS d3d11Flags = { D3D11_BIND_RENDER_TARGET };
winrt::com_ptr<ID3D11Resource> d3d11Resource;
winrt::check_hresult(
m_d3d11On12Device->CreateWrappedResource(
    d3d12Resource.get(),
    &d3d11Flags,
    D3D12_RESOURCE_STATE_RENDER_TARGET,
    D3D12_RESOURCE_STATE_RENDER_TARGET,
    IID_PPV_ARGS(d3d11Resource.put())
));

auto tex2d = d3d11Resource.as<ID3D11Texture2D>(); // good!
auto surface2 = tex2d.as<IDXGISurface>(); //0x80004002 : 'No such interface supported'.
auto surface = d3d11Resource.as<IDXGISurface>(); //0x80004002 : 'No such interface supported'.
tom-huntington commented 5 months ago

Woops I was using MipLevels 0. It will work with 1.

MipLevels may be 0, or 1 to the maximum mip levels supported by the Width, Height , and DepthOrArraySize dimensions. When 0 is used, the API will automatically calculate the maximum mip levels supported and use that. But, some resource and heap properties preclude mip levels, so the app must specify the value as 1.