-1

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?

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
BartW
  • 29
  • 5
  • Try to remove the `dxgiResource->Release()` call – Simon Mourier Aug 04 '23 at 07:32
  • https://stackoverflow.com/questions/42527906/how-do-i-pass-a-handle-of-dxgi-shared-resource-to-another-process ? – Swift - Friday Pie Aug 04 '23 at 07:35
  • @SimonMourier - this unfortunately didn't change anything. @Swift-FridayPie - Thanks. I was hoping to use the named shared handle to avoid the overhead that the `DuplicateHandle` requires, especially as I don't necessarily know the other other process pid. I was under the impression that this was what the purpose of `OpenSharedResourceByName`, but maybe I'm mistaken. – BartW Aug 04 '23 at 08:33
  • Do you have a fully reproducing simple project https://stackoverflow.com/help/minimal-reproducible-example – Simon Mourier Aug 06 '23 at 07:47

1 Answers1

-1

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.

BartW
  • 29
  • 5
  • I don't understand why this got downvoted, when it actually turned out to be the answer to my original question - why I got the E_INVALIDARG when issuing that call. MS documentation does not specify that you can't share resources between adapters (or at least it's not in the documentation for sharing functions). You can only share them between different Devices only if they are created on the same adapter. – BartW Aug 15 '23 at 11:23