Search code examples
ipcdirectx-11dxgi

Sharing ID3D11Texture2D between processes yields invalid argument error


I'm attempting to share a ID3D11Texture2D between processes using named shared handle. My tests work fine between threads, but when I do the same between two running applications, I get E_INVALIDARG back, as if the texture was not properly created or something else was not allowing it to share (I get the same error on handle creation when texture doesn't have the flags set correctly for example).

Handle creation:

    D3D11_TEXTURE2D_DESC texDesc;
    _tex->GetDesc( &texDesc );

    // check if can be shared
    if ( !( texDesc.MiscFlags & D3D11_RESOURCE_MISC_SHARED ) 
         || !( texDesc.MiscFlags & D3D11_RESOURCE_MISC_SHARED_NTHANDLE ) ) {
         return -1;
    }

    IDXGIResource1 *dxgiResource = nullptr;
    auto result = _tex->QueryInterface( IID_PPV_ARGS( &dxgiResource ) );
    if ( FAILED( result ) ) {
        return -1;
    }
    // convert name to wide string
    auto wHandleName = ToWString( handleName );
    result = dxgiResource->CreateSharedHandle(
            NULL, 
            DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, 
            wHandleName.c_str(), 
            &_sharedHandle );
    }

    dxgiResource->Release();

    if ( FAILED( result ) ) {
        return -1;
    }

Handle opening:

    ID3D11Texture2D *sharedTexture;
    auto result = d3d11Device1->OpenSharedResourceByName( ToWString( handleName ).c_str(),
                                                          DXGI_SHARED_RESOURCE_READ,
                                                          IID_PPV_ARGS( &sharedTexture ) );

    // this is where the error happens
    if ( FAILED( result ) ) {
        return nullptr;
    }
    DEFER( { sharedTexture->Release(); } );

I'm storing the texture and handle until they are captured and freed by another process, the tests that spawn threads and use this code between threads are working fine.

Documentation says that it's not necessary to use DuplicateHandle, using the name should be enough.

What am I missing?


Solution

  • It turned out that I was trying to open the resource that was shared on a different adapter (GPU). Sharing on the same adapter works fine.