You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`WebGPUHostedSurfaceDemo` shows how to render ImageSharp.Drawing content into a UI object owned by an application framework. The sample uses a WinForms `Control`, but the API shape is intended for any externally-owned native drawable host.
4
+
5
+
It exists to demonstrate:
6
+
7
+
- creating a `WebGPUHostedSurface<Bgra32>` from a `WebGPUSurfaceHost`
8
+
- keeping the hosted surface synchronized with the host control's drawable framebuffer size
- drawing with the normal `DrawingCanvas<TPixel>` API
11
+
- presenting by disposing the acquired frame
12
+
13
+
## Running
14
+
15
+
```bash
16
+
dotnet run --project samples/WebGPUHostedSurfaceDemo -c Debug
17
+
```
18
+
19
+
Requirements:
20
+
21
+
- .NET 8.0 SDK or later
22
+
- Windows, because this sample is a WinForms app
23
+
- a WebGPU-capable desktop backend such as D3D12 or Vulkan
24
+
- adapter support for the storage-capable BGRA format required by `Bgra32`
25
+
26
+
When the sample starts you should see a WinForms window with two tabs:
27
+
28
+
-`Clock`: a continuously-rendered animated clock scene
29
+
-`Tiger`: an interactive SVG tiger viewer with pan and zoom
30
+
31
+
## Why This Sample Matters
32
+
33
+
`WebGPUWindow<TPixel>` owns a top-level native window. `WebGPUHostedSurface<TPixel>` is different: it attaches WebGPU rendering to something the application already owns, such as a control, view, widget, or native surface.
34
+
35
+
That makes it the integration path for UI frameworks. The host owns:
36
+
37
+
- the UI object and its lifecycle
38
+
- the platform handle
39
+
- layout and resize events
40
+
- input events
41
+
42
+
The hosted surface owns:
43
+
44
+
- the WebGPU surface created for that host
45
+
- swapchain configuration
46
+
- frame acquisition
47
+
- presentation
48
+
- per-frame texture and texture-view handles
49
+
50
+
## Code Tour
51
+
52
+
The reusable integration point is [WebGPURenderControl.cs](d:/GitHub/SixLabors/ImageSharp.Drawing/samples/WebGPUHostedSurfaceDemo/Controls/WebGPURenderControl.cs).
53
+
54
+
### Surface Creation
55
+
56
+
`WebGPURenderControl.OnHandleCreated(...)` creates the hosted surface from the WinForms control handle:
`WebGPUSurfaceHost` is a small public descriptor for externally-owned native handles. The host keeps ownership of those handles; `WebGPUHostedSurface<TPixel>` only uses them to create and manage the WebGPU rendering surface.
67
+
68
+
### Resize
69
+
70
+
`WebGPUHostedSurface<TPixel>.Resize(...)` expects the drawable framebuffer size in pixels:
The sample stores that size as `FramebufferSize` so scene code can draw in the same pixel coordinate space as the acquired frame.
78
+
79
+
### Frame Acquisition
80
+
81
+
`RenderOnce()` acquires a frame, invokes user drawing code, and disposes the frame:
82
+
83
+
```csharp
84
+
if (!this.surface.TryAcquireFrame(outWebGPUSurfaceFrame<Bgra32>? frame))
85
+
{
86
+
return;
87
+
}
88
+
89
+
using (frame)
90
+
{
91
+
this.PaintFrame?.Invoke(frame.Canvas, delta);
92
+
}
93
+
```
94
+
95
+
Disposing the frame flushes pending canvas work, presents the surface texture, and releases the per-frame WebGPU handles.
96
+
97
+
### Rendering Loop
98
+
99
+
The sample uses `Application.Idle` for continuous rendering. While the WinForms message queue is empty, the control renders frames. When input, resize, or other window messages arrive, the loop exits so WinForms can process them.
100
+
101
+
Frame pacing is delegated to the present mode. With the default `WebGPUPresentMode.Fifo`, frame acquisition naturally waits for display presentation instead of using a separate software timer.
102
+
103
+
## Scene Code
104
+
105
+
[MainForm.cs](d:/GitHub/SixLabors/ImageSharp.Drawing/samples/WebGPUHostedSurfaceDemo/MainForm.cs) creates two independent `WebGPURenderControl` instances, one per tab. Each control owns its own hosted surface.
-[TigerViewerScene.cs](d:/GitHub/SixLabors/ImageSharp.Drawing/samples/WebGPUHostedSurfaceDemo/Scenes/TigerViewerScene.cs): pan and zoom SVG tiger viewer
111
+
112
+
Each scene receives:
113
+
114
+
-`DrawingCanvas<Bgra32>` for the acquired frame
115
+
- the current framebuffer size
116
+
- elapsed time since the previous frame
117
+
118
+
## Files
119
+
120
+
-[WebGPURenderControl.cs](d:/GitHub/SixLabors/ImageSharp.Drawing/samples/WebGPUHostedSurfaceDemo/Controls/WebGPURenderControl.cs): reusable WinForms hosted-surface control
121
+
-[MainForm.cs](d:/GitHub/SixLabors/ImageSharp.Drawing/samples/WebGPUHostedSurfaceDemo/MainForm.cs): tabs and scene wiring
122
+
-[ClockScene.cs](d:/GitHub/SixLabors/ImageSharp.Drawing/samples/WebGPUHostedSurfaceDemo/Scenes/ClockScene.cs): clock scene
123
+
-[TigerViewerScene.cs](d:/GitHub/SixLabors/ImageSharp.Drawing/samples/WebGPUHostedSurfaceDemo/Scenes/TigerViewerScene.cs): tiger viewer scene
0 commit comments