Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.d/gpu-import-textures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Added

- Sample hardware decoders' frames in `gpu.View` without copying them, through `Device.ImportTexture`: Direct3D 11 shared textures on Windows and DMA-BUFs on Linux (#2296 by @oneo-me).
11 changes: 10 additions & 1 deletion docs/architecture/gpu.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,15 @@ return gpu.View{
2. `core` bumps the surface's device generation, marks all handles from the lost `Device` invalid (subsequent writes/submits become no-ops rather than panicking), and calls `View.OnDeviceLost`.
3. When the platform restores or recreates the device/context, it emits `EventGPUDeviceReady` with the new generation and `core` invokes `View.OnReady` with a fresh `*gpu.Device` so the app rebuilds its resources cleanly.

### Imported textures

`Device.ImportTexture` ([#2296](https://github.com/nsgo-dev/nsgo/issues/2296)) opens GPU memory another device owns, such as a hardware decoder's frame, as a sampled 2D texture without copying it; `Device.SupportsExternalTexture(kind)` (`Features.ImportD3D11Shared`, `Features.ImportDMABuf`) says whether a target can, and elsewhere it returns `ErrUnsupported`, so callers fall back to `WriteTexture`.

- **One plane per import**: an NV12 frame is two textures, luma `FormatR8Unorm` and chroma `FormatRG8Unorm` at half size, and P010's are the import-only `FormatR16Unorm` and `FormatRG16Unorm`, which `CreateTexture` rejects. Shaders keep a plain `texture_2d` and convert YUV themselves, so no backend needs `samplerExternalOES` or a YCbCr sampler.
- **Windows**: the backend opens the NT handle with `ID3D11Device1::OpenSharedResource1` once per handle within a `Submit`, so planes imported together share that resource (opening a handle twice can yield two resources, and the keyed mutex of one does not make the other's writes visible) while handle values closed and recycled by Windows in a later `Submit` open the new resource, and views the plane with `CreateShaderResourceView1`'s `PlaneSlice`. With `KeyedMutex`, each frame whose passes bind the texture acquires `AcquireKey` (100 ms at most, reporting a timeout on `OnError`) before the passes and releases `ReleaseKey` after them.
- **Linux**: the backend makes an EGLImage with `EGL_EXT_image_dma_buf_import` (naming the modifier where `_modifiers` is present) on the display `eglGetCurrentDisplay` gives inside the `GtkGLArea`'s context, and binds it with `glEGLImageTargetTexture2DOES`; a GLX context reports no support.
- **Lifetime**: the caller keeps the handle or file descriptors, open until the first frame submitted after the import completes. Imported textures are only sampled: writes, copies, readbacks and render passes on them are dropped or rejected, and they are not retained across device loss. Closing one releases what the backend opened after the frames already submitted, so a decoder's frame goes back to its pool once the `FrameFence` of the last frame that sampled it completes.

## Web backend: WebGPU with WebGL 2 fallback (Decision 2)

On the web (`internal/platform/web`, [#1108](https://github.com/nsgo-dev/nsgo/issues/1108)), a `gpu.View` mounts a `<canvas>` sized to physical pixels (`width * pixelRatio`, `height * pixelRatio`), reusing the canvas sizing and animation-frame lifecycle already established in `internal/platform/web/shader.go`:
Expand Down Expand Up @@ -450,7 +459,7 @@ Tracked as staged follow-up issues once Stage 1 (`#1105`–`#1110`) is green on
- Dynamic uniform buffer offsets, ring-buffered streaming updates, mipmap generation, and GPU timing/profiling queries.
- **Compute, interop, and Stage 2 backends ([#1234](https://github.com/nsgo-dev/nsgo/issues/1234)–[#1240](https://github.com/nsgo-dev/nsgo/issues/1240))**:
- Compute pipelines (`@compute @workgroup_size(X, Y, Z)`, `var<storage, read>`, `var<storage, read_write>`, `texture_storage_2d`, `Device.CreateComputePipeline`, `Encoder.BeginComputePass`, `Dispatch` / `DispatchIndirect`, `Device.ReadBuffer`) with `Device.Capabilities().Features.Compute` / `Device.SupportsCompute()` capability queries ([#1234](https://github.com/nsgo-dev/nsgo/issues/1234)). Compute shaders compile to MSL (`kernel void`), HLSL SM 5.0 (`[numthreads]` + `StructuredBuffer`/`RWStructuredBuffer`), WGSL, and GLSL 4.30 / ES 3.10 (`layout(local_size_*) in;` + `std430` SSBOs), and dispatch runs today on the headless software device (`gpu.NewSoftwareRenderer`, `nsgotest`). The Metal, Direct3D 11, WebGPU, WebGL 2, OpenGL and Android GLES backends still report `Features.Compute` false until each wires compute dispatch. Where `SupportsCompute()` is false, creating compute pipelines, read/write storage or storage texture bindings, dispatching, or `ReadBuffer` returns `gpu.ErrUnsupported`, and a compute pass records nothing; applications should fall back to CPU updates (`Device.WriteBuffer`) or ping-pong render-to-texture passes.
- Zero-copy / shared-texture interop with `core.Image`, `core.Video`, and pixel buffers;
- Zero-copy interop with `core.Image`, `core.Video`, and pixel buffers, and importing `CVPixelBuffer` / IOSurface frames on Metal ([#1105](https://github.com/nsgo-dev/nsgo/issues/1105)); [imported textures](#imported-textures) cover Direct3D 11 and DMA-BUF;
- Stage 2 **Android Vulkan** (`purego` once bionic `dlopen` and cross-process surface sharing land) and Stage 2 **HarmonyOS** GPU surface support.

### Completion criteria
Expand Down
2 changes: 1 addition & 1 deletion docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ The [games plan (#1047)](https://github.com/nsgo-dev/nsgo/issues/1047) targets m
| Audio scheduling and latency | [#1223](https://github.com/nsgo-dev/nsgo/issues/1223), [audio timing](architecture/audio.md#timing) | Windows schedules sounds against XAudio2's clock, which its backend does not yet do (`CanSchedule` is false there), and reads its output latency, which it reports as unknown; Android cannot schedule a start and answers the output-latency query at once as unknown, as `SoundPool` and `MediaPlayer` expose no route latency ([#1097](https://github.com/nsgo-dev/nsgo/issues/1097)); two scheduled starts 500 ms apart are measured against recorded output on Windows |
| Crash reports outside native apps | [#1226](https://github.com/nsgo-dev/nsgo/issues/1226), [crash reports](architecture/application.md#crash-reports) | Web, HarmonyOS and mini-program hosts have a persistent place for reports and return them on the next launch |
| HarmonyOS system edge gestures | [#1218](https://github.com/nsgo-dev/nsgo/issues/1218) | Establish whether ArkUI offers a supported way to defer navigation gestures over game controls, as iOS and Android do |
| GPU view for 3D | [#1059](https://github.com/nsgo-dev/nsgo/issues/1059), [GPU view](architecture/gpu.md) ([#1104](https://github.com/nsgo-dev/nsgo/issues/1104)), [#1105](https://github.com/nsgo-dev/nsgo/issues/1105)–[#1110](https://github.com/nsgo-dev/nsgo/issues/1110), [#1189](https://github.com/nsgo-dev/nsgo/issues/1189), [#1191](https://github.com/nsgo-dev/nsgo/issues/1191), [#1195](https://github.com/nsgo-dev/nsgo/issues/1195), [#1196](https://github.com/nsgo-dev/nsgo/issues/1196), [#1240](https://github.com/nsgo-dev/nsgo/issues/1240), [#1193](https://github.com/nsgo-dev/nsgo/issues/1193), [#1237](https://github.com/nsgo-dev/nsgo/issues/1237), [#1238](https://github.com/nsgo-dev/nsgo/issues/1238), [#1239](https://github.com/nsgo-dev/nsgo/issues/1239), [#1234](https://github.com/nsgo-dev/nsgo/issues/1234), [#1192](https://github.com/nsgo-dev/nsgo/issues/1192), [#1235](https://github.com/nsgo-dev/nsgo/issues/1235), [#1236](https://github.com/nsgo-dev/nsgo/issues/1236), [#1190](https://github.com/nsgo-dev/nsgo/issues/1190), [#1194](https://github.com/nsgo-dev/nsgo/issues/1194) | A depth-tested mesh draws with textures and lighting in `gpu.View` on macOS, iOS, Windows, Linux, Web (WebGPU with WebGL 2 fallback), Android (GLES 3.0; Vulkan in Stage 2) and headless `nsgotest`/CI with zero UI view-tree commits per frame; so far the portable `gpu` package (including offscreen color/depth render targets, depth-only shadow passes, readable `texture_depth_2d` textures, and `sampler_comparison` / `textureSampleCompare` / `textureSampleCompareLevel` shadow sampling, compressed BCn/ETC2/ASTC texture formats with capability queries and pure-Go software decoding, per-pass GPU timing queries, zero-cost-in-release debug labels/groups, and compute pipelines with read/write storage buffers, storage textures, `Device.SupportsCompute()` and `Device.ReadBuffer`, 4x MSAA render targets with pass-end and explicit `ResolveTexture` resolves, stencil and full pipeline state, up to four color targets (MRT), HDR float and sRGB formats, and cubemaps, 2D texture arrays, mip chain uploads and LOD sampling), `core`/`driver`/`engine` off-commit surface hook, vertex+fragment+compute WGSL/`shaderir` compilation and MSL/HLSL/GLSL (3.30/4.30/ES 3.00/ES 3.10)/WGSL emitters, instanced drawing (`VertexStepInstance`, `StepRate`, `@builtin(instance_index)`, `DrawInstanced`/`DrawIndexedInstanced`), per-frame `RingBuffer` / dynamic uniform offsets / `FramesInFlight` `FrameFence` pipelining, and headless `nsgotest` software 3D rasterizer and compute VM with pixel, depth and buffer readback exist, as do the Metal backend on macOS and iOS ([#1105](https://github.com/nsgo-dev/nsgo/issues/1105)) and the Web GPU backend ([#1108](https://github.com/nsgo-dev/nsgo/issues/1108), WebGPU primary via WGSL with automatic WebGL 2 fallback via GLSL ES 3.00, verified in headless Chrome via `nsgo test -target web ./internal/platform/web` with textured offscreen draw, readback, resize, context loss/restore, and clean teardown) and the Android OpenGL ES 3.0 backend ([#1109](https://github.com/nsgo-dev/nsgo/issues/1109)) and the Windows Direct3D 11 backend ([#1106](https://github.com/nsgo-dev/nsgo/issues/1106)) and the Linux OpenGL 3.3 / ES 3.0 backend on `GtkGLArea` ([#1107](https://github.com/nsgo-dev/nsgo/issues/1107)); cross-target pixel comparison is pending. Device and context loss recovery with app-suspension gating and opt-in retained resource restoration ([#1238](https://github.com/nsgo-dev/nsgo/issues/1238)) and a persistent JSON `PipelineCache` with asynchronous pipeline precompilation ([#1239](https://github.com/nsgo-dev/nsgo/issues/1239)) are in place. Compute dispatch, pipeline stencil testing, MRT beyond one target and the extended formats (`FormatRG11B10Float`, `FormatR32Float`, `FormatRGBA32Float`, `FormatBGRA8UnormSRGB`, `FormatStencil8`) currently run only on the software device; the native backends report `Features.Compute`, `Features.StencilTest` and `Features.ExtendedFormats` false and `Limits.MaxColorAttachments` 1. Device-side mipmap generation (`Features.MipmapGeneration`) and sampler LOD clamps are software-only as well; native backends sample mip chains uploaded with `WriteTextureMip`. |
| GPU view for 3D | [#1059](https://github.com/nsgo-dev/nsgo/issues/1059), [GPU view](architecture/gpu.md) ([#1104](https://github.com/nsgo-dev/nsgo/issues/1104)), [#1105](https://github.com/nsgo-dev/nsgo/issues/1105)–[#1110](https://github.com/nsgo-dev/nsgo/issues/1110), [#1189](https://github.com/nsgo-dev/nsgo/issues/1189), [#1191](https://github.com/nsgo-dev/nsgo/issues/1191), [#1195](https://github.com/nsgo-dev/nsgo/issues/1195), [#1196](https://github.com/nsgo-dev/nsgo/issues/1196), [#1240](https://github.com/nsgo-dev/nsgo/issues/1240), [#1193](https://github.com/nsgo-dev/nsgo/issues/1193), [#1237](https://github.com/nsgo-dev/nsgo/issues/1237), [#1238](https://github.com/nsgo-dev/nsgo/issues/1238), [#1239](https://github.com/nsgo-dev/nsgo/issues/1239), [#1234](https://github.com/nsgo-dev/nsgo/issues/1234), [#1192](https://github.com/nsgo-dev/nsgo/issues/1192), [#1235](https://github.com/nsgo-dev/nsgo/issues/1235), [#1236](https://github.com/nsgo-dev/nsgo/issues/1236), [#1190](https://github.com/nsgo-dev/nsgo/issues/1190), [#1194](https://github.com/nsgo-dev/nsgo/issues/1194) | A depth-tested mesh draws with textures and lighting in `gpu.View` on macOS, iOS, Windows, Linux, Web (WebGPU with WebGL 2 fallback), Android (GLES 3.0; Vulkan in Stage 2) and headless `nsgotest`/CI with zero UI view-tree commits per frame; so far the portable `gpu` package (including offscreen color/depth render targets, depth-only shadow passes, readable `texture_depth_2d` textures, and `sampler_comparison` / `textureSampleCompare` / `textureSampleCompareLevel` shadow sampling, compressed BCn/ETC2/ASTC texture formats with capability queries and pure-Go software decoding, per-pass GPU timing queries, zero-cost-in-release debug labels/groups, and compute pipelines with read/write storage buffers, storage textures, `Device.SupportsCompute()` and `Device.ReadBuffer`, 4x MSAA render targets with pass-end and explicit `ResolveTexture` resolves, stencil and full pipeline state, up to four color targets (MRT), HDR float and sRGB formats, and cubemaps, 2D texture arrays, mip chain uploads and LOD sampling), `core`/`driver`/`engine` off-commit surface hook, vertex+fragment+compute WGSL/`shaderir` compilation and MSL/HLSL/GLSL (3.30/4.30/ES 3.00/ES 3.10)/WGSL emitters, instanced drawing (`VertexStepInstance`, `StepRate`, `@builtin(instance_index)`, `DrawInstanced`/`DrawIndexedInstanced`), per-frame `RingBuffer` / dynamic uniform offsets / `FramesInFlight` `FrameFence` pipelining, and headless `nsgotest` software 3D rasterizer and compute VM with pixel, depth and buffer readback exist, as do the Metal backend on macOS and iOS ([#1105](https://github.com/nsgo-dev/nsgo/issues/1105)) and the Web GPU backend ([#1108](https://github.com/nsgo-dev/nsgo/issues/1108), WebGPU primary via WGSL with automatic WebGL 2 fallback via GLSL ES 3.00, verified in headless Chrome via `nsgo test -target web ./internal/platform/web` with textured offscreen draw, readback, resize, context loss/restore, and clean teardown) and the Android OpenGL ES 3.0 backend ([#1109](https://github.com/nsgo-dev/nsgo/issues/1109)) and the Windows Direct3D 11 backend ([#1106](https://github.com/nsgo-dev/nsgo/issues/1106)) and the Linux OpenGL 3.3 / ES 3.0 backend on `GtkGLArea` ([#1107](https://github.com/nsgo-dev/nsgo/issues/1107)); cross-target pixel comparison is pending. Device and context loss recovery with app-suspension gating and opt-in retained resource restoration ([#1238](https://github.com/nsgo-dev/nsgo/issues/1238)) and a persistent JSON `PipelineCache` with asynchronous pipeline precompilation ([#1239](https://github.com/nsgo-dev/nsgo/issues/1239)) are in place. Compute dispatch, pipeline stencil testing, MRT beyond one target and the extended formats (`FormatRG11B10Float`, `FormatR32Float`, `FormatRGBA32Float`, `FormatBGRA8UnormSRGB`, `FormatStencil8`) currently run only on the software device; the native backends report `Features.Compute`, `Features.StencilTest` and `Features.ExtendedFormats` false and `Limits.MaxColorAttachments` 1. Device-side mipmap generation (`Features.MipmapGeneration`) and sampler LOD clamps are software-only as well; native backends sample mip chains uploaded with `WriteTextureMip`. `Device.ImportTexture` samples hardware decoders' frames without copying on Windows (Direct3D 11 shared textures) and Linux (DMA-BUFs over EGL) ([#2296](https://github.com/nsgo-dev/nsgo/issues/2296)); Metal (`CVPixelBuffer` / IOSurface, [#1105](https://github.com/nsgo-dev/nsgo/issues/1105)), Web, Android and the software device report `SupportsExternalTexture` false. |
| Display refresh rates | [#1213](https://github.com/nsgo-dev/nsgo/issues/1213), [displays](architecture/services.md#displays-and-locale-settings) | Linux reports a panel's maximum rate where the compositor exposes its modes. watchOS and mini programs report the current display and its rate where the host exposes them |
| Games on HarmonyOS | [#1117](https://github.com/nsgo-dev/nsgo/issues/1117), [#1051](https://github.com/nsgo-dev/nsgo/issues/1051), [#1113](https://github.com/nsgo-dev/nsgo/issues/1113), [#1352](https://github.com/nsgo-dev/nsgo/issues/1352), [#1512](https://github.com/nsgo-dev/nsgo/issues/1512) | **Works today:** multi-touch `OnTouch` (presentation coordinates under bound transforms), presses and long presses, scroll/pinch-zoom, physical-keyboard shortcuts via `Scope.OnKey`; 2D `core.Canvas` on ArkUI `CanvasRenderingContext2D` (paths, fills, strokes, linear/radial gradients, text, encoded/resource images, animated slots; ~62–63 FPS on emulator) and encoded/URI `core.Image`; `RequestAnimationFrame` via ArkGraphics2D `displaySync` (`expected: 60`, `min: 30, max: 120`, passing `targetTimestamp`); `KeepAwake` via ArkUI power assertion; `RequestOrientation` via `window.setPreferredOrientation` ([#2227](https://github.com/nsgo-dev/nsgo/issues/2227)). **`Env` reports:** `OS` `android`, `Idiom` phone/tablet, `Simulated` false, `HasWebView` true, `CanTakePhoto` true, `CanPlayAudio`/`CanSpeak`/`CanRecognizeSpeech`/`CanScanCode`/`SupportsCustomCursor` false. **Missing:** `OnKeyDown`/`OnKeyUp`/`KeysDown`/`KeyHeld` (#1352), `OnPan`/`OnPinch`/`OnRotation` outside scroll views, gamepads (#1049), mouse/pointer lock (#1054), `ImageSource.Pixels` ArkUI `PixelMap` bridge (#1051), `core.OffscreenImage` and `KeepImage`, conic gradients, `core.Shader` and `gpu.View` 3D (Stage 2 via `XComponent` GLES 3.0/Vulkan or ArkWeb WebGL 2, [GPU view](architecture/gpu.md)), `audio.Sound`/`audio.Player` (#1050), and haptics (`ItemHaptic`) |
| Games in mini programs | [#1117](https://github.com/nsgo-dev/nsgo/issues/1117), [#1051](https://github.com/nsgo-dev/nsgo/issues/1051), [#1101](https://github.com/nsgo-dev/nsgo/issues/1101), [#1113](https://github.com/nsgo-dev/nsgo/issues/1113), [#1503](https://github.com/nsgo-dev/nsgo/issues/1503), [#1505](https://github.com/nsgo-dev/nsgo/issues/1505), [#1507](https://github.com/nsgo-dev/nsgo/issues/1507) | **Works today:** multi-touch `OnTouch`, presses, long presses, single-finger `OnPan`, two-finger `OnPinch` and `OnRotation`, with presentation touch coordinates under bound transforms, and back navigation on WeChat; 2D `core.Canvas` via `canvas2d` offscreen canvas serialized to PNG data URLs (with animated slots), `ImageSource.Pixels` in canvases on all hosts and in `core.Image` via a dedicated 2D `<canvas>` on WeChat (#1101); `RequestAnimationFrame` on a 16 ms `setTimeout` loop using the graph clock. **`Env` reports:** host device `OS` with `Simulated` true, `Idiom` phone/tablet, `AppleKeys` true on iOS/macOS, `CanTakePhoto` true on mobile hosts with `chooseMedia`/`chooseImage`, `CanPlayAudio`/`CanSpeak`/`CanRecognizeSpeech`/`CanScanCode`/`HasWebView`/`SupportsCustomCursor` false. **Missing:** physical keyboard (`OnKeyDown`/`OnKeyUp`/`KeysDown`/`OnKey`), mouse/pointer lock (#1054), gamepads (#1049), transformed pointer coordinates outside touches (#1502), pixel-buffer image views on QQ/Douyin/Alipay (#1051), `core.OffscreenImage`, `core.Shader` (`nsgo_noshader`, #1507) and `gpu.View` 3D (out of scope: 2 MB package cap and no logic-layer GPU surface, [GPU view](architecture/gpu.md)), `audio.Sound`/`audio.Player` (#1050, [audio](architecture/audio.md)), display-vsync frame timing (logic layer has none, #1113), haptics, `KeepAwake`, orientation, and fitting larger game binaries within the 2 MB package limit (#1505) |
Expand Down
Loading