Directx 12 : Sharing graphic memory between two processes
Asked Answered
W

1

5

I am trying to create two applications. One application will render a texture off-screen and the second application will read it from graphic memory and render/present it on window.

My doubt is is it possible to share graphic memory in directx 12.

My named shared memory approach is causing comptr addref error...

I am using a comptr for ID3D12Resource for texture data...

So how can we go on with such approach...

Wandawander answered 17/1, 2020 at 7:7 Comment(0)
K
6

Certainly possible to render to offscreen texture and display it in another process. To display, you can call CopyResource or CopyTextureRegion to copy shared resource into the Swapchain's backbuffer and then present it.

I'm not sure what you mean by your named shared memory approach, but to get interprocess memory sharing working you have to:

In process A:

In process B:

Sample sketched quickly (creates a buffer, but that shouldn't be any different for textures):

Microsoft::WRL::ComPtr<ID3D12Resource> ptr{};
if (isProcessA) {
    HANDLE handle{};
    throwIfFailed(device->CreateCommittedResource(
        &CD3DX12_HEAP_PROPERTIES{D3D12_HEAP_TYPE_DEFAULT},
        D3D12_HEAP_FLAG_SHARED,
        &CD3DX12_RESOURCE_DESC::Buffer(1024),
        D3D12_RESOURCE_STATE_COMMON,
        nullptr,
        IID_PPV_ARGS(&ptr)));
    throwIfFailed(device->CreateSharedHandle(ptr.Get(), nullptr, GENERIC_ALL, L"Name", &handle));
} else {
    HANDLE handle{};
    throwIfFailed(device->OpenSharedHandleByName(L"Name", GENERIC_ALL, &handle));
    throwIfFailed(device->OpenSharedHandle(handle, IID_PPV_ARGS(&ptr)));
}

Note, that you have to avoid collisions of Name values passed to the ID3D12Device::CreateCommittedResource.

For further reference, see MSDN.

Keon answered 19/1, 2020 at 23:57 Comment(1)
yes u r spot on... although i found the respective MSDN documentation for it btw...THANKS...Wandawander

© 2022 - 2024 — McMap. All rights reserved.