Wednesday, November 13, 2024
spot_img

c++ – The right way to use a GPU Buffer from UE5 as enter right into a CUDA kernel?


I’m attempting to make use of an UE5 GPU Buffer in a CUDA kernel (from an exterior library).

I’ve seemed into the one instance of the Unreal Sources situated in FRivermaxInputStream.cpp however could not reproduce their sucessful mapping, and into the CUDA Samples SimpleD3D12.cpp.

I constructed a minimal challenge the place I run a lambda on the Rendering Thread that creates an RHIBuffer and tries to map it in CUDA.
Unfortunatly, the CUDA system pointer I get from cuExternalMemoryGetMappedBuffer() throws an CUDA_ERROR_INVALID_VALUE (invalid argument) on utilization.

Here’s a hyperlink to the principle file (and minimal challenge) : Github Venture

I’m working out of concepts on what I’m lacking and the way I can debug it.

As soon as we get this to work, I plan on leaving this challenge public as a starter level for builders sooner or later.

Thanks to your assist.

EDIT :
Listed below are the important thing steps of my code to map the D3D12 Buffer to CUDA (I did not need to flood the publish with code, so I posted the total file on Github).

// Create the Buffer
const EBufferUsageFlags Flags = BUF_StructuredBuffer | BUF_Shared | BUF_ShaderResource | BUF_UnorderedAccess;
FRHIBuffer* const Buffer = RHICreateBuffer(Rely, Flags, Stride, ResourceState, CreateInfo);
// Get the D3D12 Useful resource
ID3D12Resource* const NativeD3D12Resource = GetID3D12DynamicRHI()->RHIGetResource(Buffer);

// Create the D3D12 SharedHandle
HANDLE D3D12SharedHandle;
NativeD3D12Device->CreateSharedHandle(NativeD3D12Resource, nullptr, GENERIC_ALL, nullptr, &D3D12SharedHandle);

// Import the D3D12 reminiscence in CUDA
CUDA_EXTERNAL_MEMORY_HANDLE_DESC Desc{};
Desc.sort = CUexternalMemoryHandleType::CU_EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_RESOURCE;
Desc.deal with.win32.deal with = D3D12SharedHandle;
Desc.measurement = GetID3D12DynamicRHI()->RHIGetResourceMemorySize(Buffer);
Desc.flags = CUDA_EXTERNAL_MEMORY_DEDICATED;
CUexternalMemory Deal with;
FCUDAModule::CUDA().cuImportExternalMemory(&Deal with, &Desc);

// Map the imported reminiscence
CUdeviceptr Ptr;
FCUDAModule::CUDA().cuExternalMemoryGetMappedBuffer(&Ptr, CudaExternalMemoryHandle, &Desc);

// Use the CUDA ptr
FCUDAModule::CUDA().cuMemcpyHtoD(BufferCudaPtr, SomeCpuArray.GetData(), Rely * Stride); // 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest Articles