Skip to content

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

@cqgis

Description

@cqgis

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposal 💡A proposal for a new featureuntriaged 🧰A new issue that needs initial triage

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions