Skip to content

Commit 36f2203

Browse files
Replace Lease-based runtime with shared API
1 parent 24af798 commit 36f2203

File tree

10 files changed

+118
-205
lines changed

10 files changed

+118
-205
lines changed

src/ImageSharp.Drawing.WebGPU/WebGPUDeviceContext{TPixel}.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public sealed class WebGPUDeviceContext<TPixel> : IDisposable
1717
{
1818
private readonly nint deviceHandle;
1919
private readonly nint queueHandle;
20-
private readonly WebGPURuntime.Lease? runtimeLease;
2120
private bool isDisposed;
2221

2322
/// <summary>
@@ -37,7 +36,6 @@ public unsafe WebGPUDeviceContext(Configuration configuration)
3736
Guard.NotNull(configuration, nameof(configuration));
3837
EnsurePixelTypeSupported();
3938

40-
WebGPURuntime.Lease lease = WebGPURuntime.Acquire();
4139
this.Backend = new WebGPUDrawingBackend();
4240

4341
try
@@ -49,13 +47,11 @@ public unsafe WebGPUDeviceContext(Configuration configuration)
4947

5048
this.deviceHandle = (nint)device;
5149
this.queueHandle = (nint)queue;
52-
this.runtimeLease = lease;
5350
this.Configuration = configuration;
5451
}
5552
catch
5653
{
5754
this.Backend.Dispose();
58-
lease.Dispose();
5955
throw;
6056
}
6157
}
@@ -390,7 +386,6 @@ public void Dispose()
390386
}
391387

392388
this.Backend.Dispose();
393-
this.runtimeLease?.Dispose();
394389
this.isDisposed = true;
395390
}
396391

src/ImageSharp.Drawing.WebGPU/WebGPUDrawingBackend.Readback.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ public bool TryReadRegion<TPixel>(
8989
return false;
9090
}
9191

92-
using WebGPURuntime.Lease lease = WebGPURuntime.Acquire();
93-
WebGPU api = lease.Api;
92+
WebGPU api = WebGPURuntime.GetApi();
93+
Wgpu wgpuExtension = WebGPURuntime.GetWgpuExtension();
9494
Device* device = (Device*)capability.Device;
9595

9696
if (requiredFeature != FeatureName.Undefined
@@ -185,7 +185,7 @@ void Callback(BufferMapAsyncStatus status, void* userData)
185185

186186
using PfnBufferMapCallback callback = PfnBufferMapCallback.From(Callback);
187187
api.BufferMapAsync(readbackBuffer, MapMode.Read, 0, (nuint)readbackByteCount, callback, null);
188-
if (!WaitForMapSignal(lease.WgpuExtension, device, mapReady) || mapStatus != BufferMapAsyncStatus.Success)
188+
if (!WaitForMapSignal(wgpuExtension, device, mapReady) || mapStatus != BufferMapAsyncStatus.Success)
189189
{
190190
error = $"WebGPU readback map failed with status '{mapStatus}'.";
191191
return false;

src/ImageSharp.Drawing.WebGPU/WebGPUDrawingBackend.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private void FlushCompositionsFallback<TPixel>(
257257

258258
this.fallbackBackend.FlushCompositions(configuration, stagingFrame, compositionScene);
259259

260-
using WebGPURuntime.Lease lease = WebGPURuntime.Acquire();
260+
WebGPU api = WebGPURuntime.GetApi();
261261
Buffer2DRegion<TPixel> uploadRegion = compositionBounds is Rectangle cb && cb.Width > 0 && cb.Height > 0
262262
? stagingRegion.GetSubRegion(cb)
263263
: stagingRegion;
@@ -266,7 +266,7 @@ private void FlushCompositionsFallback<TPixel>(
266266
uint destY = compositionBounds is Rectangle cby ? (uint)cby.Y : 0;
267267

268268
WebGPUFlushContext.UploadTextureFromRegion(
269-
lease.Api,
269+
api,
270270
(Queue*)capability!.Queue,
271271
(Texture*)capability.TargetTexture,
272272
uploadRegion,

src/ImageSharp.Drawing.WebGPU/WebGPUFlushContext.cs

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Runtime.CompilerServices;
66
using System.Runtime.InteropServices;
77
using Silk.NET.WebGPU;
8+
using Silk.NET.WebGPU.Extensions.WGPU;
89
using SixLabors.ImageSharp.Memory;
910
using SixLabors.ImageSharp.PixelFormats;
1011
using WgpuBuffer = Silk.NET.WebGPU.Buffer;
@@ -41,16 +42,17 @@ internal sealed unsafe class WebGPUFlushContext : IDisposable
4142
private readonly Dictionary<Image, nint> cachedSourceTextureViews = new(ReferenceEqualityComparer.Instance);
4243

4344
private WebGPUFlushContext(
44-
WebGPURuntime.Lease runtimeLease,
45+
WebGPU api,
46+
Wgpu wgpuExtension,
4547
Device* device,
4648
Queue* queue,
4749
in Rectangle targetBounds,
4850
TextureFormat textureFormat,
4951
MemoryAllocator memoryAllocator,
5052
WebGPURuntime.DeviceSharedState deviceState)
5153
{
52-
this.RuntimeLease = runtimeLease;
53-
this.Api = runtimeLease.Api;
54+
this.Api = api;
55+
this.WgpuExtension = wgpuExtension;
5456
this.Device = device;
5557
this.Queue = queue;
5658
this.TargetBounds = targetBounds;
@@ -60,9 +62,9 @@ private WebGPUFlushContext(
6062
}
6163

6264
/// <summary>
63-
/// Gets the runtime lease that keeps the process-level WebGPU API alive.
65+
/// Gets the wgpu-native extension used to poll asynchronous callbacks.
6466
/// </summary>
65-
public WebGPURuntime.Lease RuntimeLease { get; }
67+
public Wgpu WgpuExtension { get; }
6668

6769
/// <summary>
6870
/// Gets the WebGPU API facade for this flush.
@@ -165,40 +167,38 @@ private WebGPUFlushContext(
165167
return null;
166168
}
167169

168-
WebGPURuntime.Lease lease = WebGPURuntime.Acquire();
169-
try
170-
{
171-
Device* device = (Device*)nativeCapability.Device;
172-
Queue* queue = (Queue*)nativeCapability.Queue;
173-
TextureFormat textureFormat = WebGPUTextureFormatMapper.ToSilk(nativeCapability.TargetFormat);
174-
Rectangle bounds = frame.Bounds;
175-
Rectangle nativeBounds = new(0, 0, nativeCapability.Width, nativeCapability.Height);
176-
WebGPURuntime.DeviceSharedState deviceState = WebGPURuntime.GetOrCreateDeviceState(lease.Api, device);
177-
178-
if (requiredFeature != FeatureName.Undefined && !deviceState.HasFeature(requiredFeature))
179-
{
180-
lease.Dispose();
181-
return null;
182-
}
183-
184-
// Region frames expose bounds relative to their parent target. The flush context must preserve
185-
// that absolute slice so later scene encoding, dispatch planning, and texture copies address
186-
// the correct sub-rectangle of the native surface instead of silently expanding back to full-frame.
187-
if (!nativeBounds.Contains(bounds))
188-
{
189-
lease.Dispose();
190-
return null;
191-
}
170+
WebGPU api = WebGPURuntime.GetApi();
171+
Device* device = (Device*)nativeCapability.Device;
172+
Queue* queue = (Queue*)nativeCapability.Queue;
173+
TextureFormat textureFormat = WebGPUTextureFormatMapper.ToSilk(nativeCapability.TargetFormat);
174+
Rectangle bounds = frame.Bounds;
175+
Rectangle nativeBounds = new(0, 0, nativeCapability.Width, nativeCapability.Height);
176+
WebGPURuntime.DeviceSharedState deviceState = WebGPURuntime.GetOrCreateDeviceState(api, device);
192177

193-
WebGPUFlushContext context = new(lease, device, queue, in bounds, textureFormat, memoryAllocator, deviceState);
194-
context.InitializeNativeTarget(nativeCapability);
195-
return context;
178+
if (requiredFeature != FeatureName.Undefined && !deviceState.HasFeature(requiredFeature))
179+
{
180+
return null;
196181
}
197-
catch
182+
183+
// Region frames expose bounds relative to their parent target. The flush context must preserve
184+
// that absolute slice so later scene encoding, dispatch planning, and texture copies address
185+
// the correct sub-rectangle of the native surface instead of silently expanding back to full-frame.
186+
if (!nativeBounds.Contains(bounds))
198187
{
199-
lease.Dispose();
200-
throw;
188+
return null;
201189
}
190+
191+
WebGPUFlushContext context = new(
192+
api,
193+
WebGPURuntime.GetWgpuExtension(),
194+
device,
195+
queue,
196+
in bounds,
197+
textureFormat,
198+
memoryAllocator,
199+
deviceState);
200+
context.InitializeNativeTarget(nativeCapability);
201+
return context;
202202
}
203203

204204
/// <summary>
@@ -487,7 +487,6 @@ public void Dispose()
487487
this.ownsTargetView = false;
488488
this.ownsTargetTexture = false;
489489

490-
this.RuntimeLease.Dispose();
491490
this.disposed = true;
492491
}
493492

src/ImageSharp.Drawing.WebGPU/WebGPURenderTarget{TPixel}.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Six Labors Split License.
33

44
using System.Diagnostics.CodeAnalysis;
5+
using Silk.NET.WebGPU;
56
using SixLabors.ImageSharp.Memory;
67
using SixLabors.ImageSharp.PixelFormats;
78

@@ -121,9 +122,9 @@ private static OwnedTarget AllocateOwnedTarget(Configuration configuration, int
121122
WebGPUDeviceContext<TPixel> graphics = new(configuration);
122123
try
123124
{
124-
using WebGPURuntime.Lease lease = WebGPURuntime.Acquire();
125+
WebGPU api = WebGPURuntime.GetApi();
125126
if (!WebGPURenderTargetAllocation.TryCreateRenderTarget<TPixel>(
126-
lease.Api,
127+
api,
127128
graphics.DeviceHandle,
128129
graphics.QueueHandle,
129130
width,
@@ -403,9 +404,9 @@ private static WebGPURenderTarget<TPixel> CreateCore(
403404
{
404405
graphics.ThrowIfDisposed();
405406

406-
using WebGPURuntime.Lease lease = WebGPURuntime.Acquire();
407+
WebGPU api = WebGPURuntime.GetApi();
407408
if (!WebGPURenderTargetAllocation.TryCreateRenderTarget<TPixel>(
408-
lease.Api,
409+
api,
409410
graphics.DeviceHandle,
410411
graphics.QueueHandle,
411412
width,

0 commit comments

Comments
 (0)