Skip to content

feat(gpu): import hardware decoders' frames as textures - #2297

Merged
oneo-me merged 2 commits into
mainfrom
agiox/implement-issue-2296
Nov 24, 2026
Merged

oneo-me merged 2 commits into
mainfrom
agiox/implement-issue-2296

Conversation

@oneo-me

@oneo-me oneo-me commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #2296.

Adds gpu.Device.ImportTexture and Device.SupportsExternalTexture, which sample a hardware decoder's frames in gpu.View without copying them to memory.

  • One plane per import: an NV12 frame is two textures, luma FormatR8Unorm and chroma FormatRG8Unorm. P010 uses the new import-only FormatR16Unorm and FormatRG16Unorm. Imported textures can only be sampled.
  • Windows: opens D3D11 NT handles with OpenSharedResource1, once per handle, and views each plane with CreateShaderResourceView1's PlaneSlice. With KeyedMutex, it holds IDXGIKeyedMutex (AcquireKey / ReleaseKey) around frames that bind the texture.
  • Linux: imports DMA-BUFs as EGLImages (EGL_EXT_image_dma_buf_import(_modifiers)) on GTK's EGL context and binds them with glEGLImageTargetTexture2DOES. GLX contexts report no support.
  • Other targets: report it unsupported and return ErrUnsupported. Closing an imported texture releases what the backend opened.

Tests:

  • Unit tests cover validation in gpu, the D3D11 controller (keyed mutex, release), and DMA-BUF attributes and EGLImage release.
  • Native, Windows (RTX 5080): an NV12 shared texture written on a second D3D11 device is imported. Its render matches the same data uploaded with WriteTexture, pixel for pixel.
  • Native, Linux: GBM DMA-BUFs in a gpu.View match the same way on Wayland and X11.
  • go tool nsgo check -target windows,linux,macos and go tool nsgo test pass.

🤖 Claude Opus 5.5

🤖 Generated with Claude Code

@oneo-me oneo-me self-assigned this Nov 24, 2026
@github-actions github-actions Bot added size/XS enhancement New feature or request labels Nov 24, 2026
@oneo-me
oneo-me force-pushed the agiox/implement-issue-2296 branch from b0018b5 to ac67f9a Compare November 24, 2026 11:11
@oneo-me oneo-me changed the title feat(gpu): import hardware decoder frames as textures feat(gpu): import hardware decoders' frames as textures Nov 24, 2026
@github-actions github-actions Bot added size/XL documentation Improvements or additions to documentation Windows Linux core and removed size/XS labels Nov 24, 2026
@oneo-me
oneo-me marked this pull request as ready for review November 24, 2026 11:11
@oneo-me
oneo-me enabled auto-merge (rebase) November 24, 2026 11:11
@houko

houko commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

One problem on Windows before this can go in: d3d11NativeOps.shared caches opened resources by NT handle value for as long as any texture imported from it is open, but the docs tell the caller to close the handle once the first frame after the import completes. After that, Windows is free to give the same handle value to a different shared texture, and openShared will hand back the old resource for it, so the import silently samples the wrong frame.

This is likely in the per-frame pattern the docs describe: the app closes frame N's textures and its handle, then CreateSharedHandle for frame N+1 often returns the value just freed. In d3dGPUController.Submit, imports run in phase 1 while the old texture's GPUOpDestroyTexture is deferred to phase 5 of the same submit, so the stale entry still has refs when the new import looks it up. Nothing checks that the cached resource is the object behind the new handle.

Keying by the handle is only safe while the caller keeps the handle open. One way to fix it is to share a resource only among imports applied in the same submit, which covers the two planes of one NV12/P010 frame: drop the map entry, but not the textures' references, at the end of each Submit. Comparing kernel objects with CompareObjectHandles against a duplicated handle would also work. A controller test that imports, closes and re-imports a recycled handle value in one frame would cover it.

A smaller note on KeyedMutex: each submitted frame that binds the texture acquires AcquireKey and releases ReleaseKey. With the usual producer/consumer keys (acquire 1, release 0), a second frame that samples the same decoded frame, such as a 30 fps video on a 60 Hz view or a redraw on resize, waits the full 100 ms on the UI thread and reports a timeout. ExternalTextureDesc.KeyedMutex's doc should say that each frame that samples the texture takes the key again, so the keys must allow that (for example equal keys) or the app must sample each import in one frame only.

The rest looks right to me: the vtable indexes (OpenSharedResource1 48, CreateShaderResourceView1 57, GetDesc 10, AcquireSync/ReleaseSync 8/9), the DXGI and DRM fourcc and EGL attribute values, plane sizes, and the EGLImage lifetime.

@oneo-me
oneo-me force-pushed the agiox/implement-issue-2296 branch from ac67f9a to da2a1ae Compare November 24, 2026 12:12
@oneo-me

oneo-me commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for catching both issues! Fixed in da2a1aed6:

  • Added d3dNativeOps.EndImportBatch() (deferred from d3dGPUController.Submit) to clear d3d11NativeOps.shared at the end of each Submit without releasing open textures' references, so planes imported in the same Submit still share one opened resource while a recycled NT handle value in a later Submit always opens the new resource.
  • Scoped acquireExternal's keyed-mutex deduplication by (importBatch, handle) so distinct import batches reusing the same handle value do not alias.
  • Added TestD3D11ImportedTextureRecycledHandleOpensFreshResource in internal/platform/windows/gpu_d3d11_test.go and updated TestD3D11ImportedNV12MatchesUpload.
  • Documented in ExternalTextureDesc.KeyedMutex and docs/architecture/gpu.md that each submitted frame binding the texture acquires AcquireKey and releases ReleaseKey again, so keys must allow repeated reads (e.g. equal keys) or each import must be sampled in one frame only.

Rebased onto main as well.

🤖 Gemini 4 Neon

@oneo-me
oneo-me force-pushed the agiox/implement-issue-2296 branch from da2a1ae to 716c7cb Compare November 24, 2026 15:10
@oneo-me
oneo-me merged commit a34e7e7 into main Nov 24, 2026
36 of 37 checks passed
@oneo-me
oneo-me deleted the agiox/implement-issue-2296 branch November 24, 2026 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core documentation Improvements or additions to documentation enhancement New feature or request Linux size/XL Windows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gpu: import a hardware decoder's frames as textures (Direct3D 11 on Windows, DMA-BUF on Linux)

2 participants