ComputeSharp icon indicating copy to clipboard operation
ComputeSharp copied to clipboard

Is there a way to directly copy data from D3D9 surfaces to ComputeSharp's ReadOnlyTexture2D without CPU staging?

Open cqgis opened this issue 7 months ago • 1 comments

I'm using Media Foundation to decode video frames (D3D9 GPU decoding, BGRA32 format) and ComputeSharp for pixel processing. Currently, I'm forced to stage the data through the CPU when copying from the decoded D3D9 buffer to ReadOnlyTexture2D, which introduces significant overhead. The workflow looks like this:
IMF2DBuffer (GPU) → UploadTexture2D (CPU) → ReadOnlyTexture2D (GPU)

Here's the current code snippet:

using var buffer = result.ConvertToContiguousBuffer();
using var buffer2d = buffer.QueryInterface<IMF2DBuffer>();
buffer2d.Lock2D(out var scanline0, out var strideInBytes);
int length = height * strideInBytes;

unsafe
{
    // Staging through CPU-visible texture
    using var stagingTexture = GraphicsDevice.GetDefault().AllocateUploadTexture2D<Bgra32>(width, height);
    var pointer = stagingTexture.View.DangerousGetAddressAndByteStride(out _);
    Buffer.MemoryCopy(scanline0.ToPointer(), pointer, length, length);

    // GPU texture creation and copy
    var originTexture = GraphicsDevice.GetDefault().AllocateReadOnlyTexture2D<Bgra32, float4>(width, height);
    originTexture.CopyFrom(stagingTexture);
    
    // ... processing ...
    originTexture.Dispose();
}

buffer2d.Unlock2D();

Key Questions:

  1. Is there a way to directly copy from the D3D9-decoded surface to ComputeSharp's ReadOnlyTexture2D without CPU staging?
  2. Alternatively, can ComputeSharp/Direct3D interop allow direct access to the D3D9 buffer in compute shaders (e.g., via resource sharing or shared handles)?
  • The D3D9 surface comes from Media Foundation's IMF2DBuffer (docs)
  • Current CPU staging (UploadTexture2D) is a bottleneck for real-time processing

cqgis avatar May 21 '25 05:05 cqgis

ComputeSharp is using D3D12, so I'd expect this article to at least explain if this is possible in principle: https://learn.microsoft.com/en-us/windows/win32/direct3darticles/surface-sharing-between-windows-graphics-apis

rickbrew avatar May 21 '25 15:05 rickbrew