55using System . Runtime . CompilerServices ;
66using System . Runtime . InteropServices ;
77using Silk . NET . WebGPU ;
8+ using Silk . NET . WebGPU . Extensions . WGPU ;
89using SixLabors . ImageSharp . Memory ;
910using SixLabors . ImageSharp . PixelFormats ;
1011using 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
0 commit comments