Skip to content

Commit fdb5ef8

Browse files
Extract DeviceSharedState to WebGPURuntime
1 parent 108bf55 commit fdb5ef8

16 files changed

Lines changed: 714 additions & 908 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public bool TryReadRegion<TPixel>(
6868
Device* device = (Device*)capability.Device;
6969

7070
if (requiredFeature != FeatureName.Undefined
71-
&& !WebGPUFlushContext.GetOrCreateDeviceState(api, device).HasFeature(requiredFeature))
71+
&& !WebGPURuntime.GetOrCreateDeviceState(api, device).HasFeature(requiredFeature))
7272
{
7373
return false;
7474
}

src/ImageSharp.Drawing.WebGPU/WebGPUDrawingBackend.cs

Lines changed: 11 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Collections.Generic;
45
using System.Runtime.CompilerServices;
56
using Silk.NET.WebGPU;
67
using SixLabors.ImageSharp.Memory;
@@ -34,84 +35,19 @@ public WebGPUDrawingBackend()
3435
=> this.fallbackBackend = DefaultDrawingBackend.Instance;
3536

3637
/// <summary>
37-
/// Gets the testing-only diagnostic counter for total coverage preparation requests.
38+
/// Gets a value indicating whether the last flush completed on the staged path.
3839
/// </summary>
39-
internal int TestingPrepareCoverageCallCount { get; private set; }
40-
41-
/// <summary>
42-
/// Gets the testing-only diagnostic counter for coverage preparations executed on the GPU.
43-
/// </summary>
44-
internal int TestingGPUPrepareCoverageCallCount { get; private set; }
45-
46-
/// <summary>
47-
/// Gets the testing-only diagnostic counter for coverage preparations delegated to the fallback backend.
48-
/// </summary>
49-
internal int TestingFallbackPrepareCoverageCallCount { get; private set; }
50-
51-
/// <summary>
52-
/// Gets the testing-only diagnostic counter for total composition requests.
53-
/// </summary>
54-
internal int TestingCompositeCoverageCallCount { get; private set; }
55-
56-
/// <summary>
57-
/// Gets the testing-only diagnostic counter for compositions executed on the GPU.
58-
/// </summary>
59-
internal int TestingGPUCompositeCoverageCallCount { get; private set; }
60-
61-
/// <summary>
62-
/// Gets the testing-only diagnostic counter for compositions delegated to the fallback backend.
63-
/// </summary>
64-
internal int TestingFallbackCompositeCoverageCallCount { get; private set; }
65-
66-
/// <summary>
67-
/// Gets the testing-only diagnostic counter for completed prepared-coverage uses.
68-
/// </summary>
69-
internal int TestingReleaseCoverageCallCount { get; private set; }
70-
71-
/// <summary>
72-
/// Gets a value indicating whether the testing-only diagnostic indicates the backend completed GPU initialization.
73-
/// </summary>
74-
internal bool TestingIsGPUReady { get; private set; }
75-
76-
/// <summary>
77-
/// Gets a value indicating whether the testing-only diagnostic indicates GPU initialization has been attempted.
78-
/// </summary>
79-
internal bool TestingGPUInitializationAttempted { get; private set; }
40+
internal bool TestingLastFlushUsedGPU { get; private set; }
8041

8142
/// <summary>
8243
/// Gets the testing-only diagnostic containing the last GPU initialization failure reason, if any.
8344
/// </summary>
8445
internal string? TestingLastGPUInitializationFailure { get; private set; }
8546

8647
/// <summary>
87-
/// Gets the testing-only diagnostic counter for live prepared coverage handles currently in use.
88-
/// </summary>
89-
internal int TestingLiveCoverageCount { get; private set; }
90-
91-
/// <summary>
92-
/// Gets the cumulative number of visible commands executed by the compute composition path.
93-
/// </summary>
94-
internal int TestingComputePathVisibleCommandCount { get; private set; }
95-
96-
/// <summary>
97-
/// Gets the cumulative number of unique coverage definitions executed by the compute composition path.
98-
/// </summary>
99-
internal int TestingComputePathDefinitionCount { get; private set; }
100-
101-
/// <summary>
102-
/// Gets the cumulative number of tile-bin command references consumed by the compute composition path.
103-
/// </summary>
104-
internal int TestingComputePathTileBinEntryCount { get; private set; }
105-
106-
/// <summary>
107-
/// Gets the cumulative number of composition commands executed on the GPU.
48+
/// Gets a value indicating whether the last flush completed on the staged path.
10849
/// </summary>
109-
public int DiagnosticGpuCompositeCount => this.TestingGPUCompositeCoverageCallCount;
110-
111-
/// <summary>
112-
/// Gets the cumulative number of composition commands that fell back to the CPU backend.
113-
/// </summary>
114-
public int DiagnosticFallbackCompositeCount => this.TestingFallbackCompositeCoverageCallCount;
50+
public bool DiagnosticLastFlushUsedGPU => this.TestingLastFlushUsedGPU;
11551

11652
/// <summary>
11753
/// Gets the last staged-scene creation or dispatch failure that forced CPU fallback.
@@ -278,35 +214,21 @@ public void FlushCompositions<TPixel>(
278214
return;
279215
}
280216

281-
int commandCount = compositionScene.Commands.Count;
282-
this.TestingCompositeCoverageCallCount += commandCount;
283-
this.TestingGPUInitializationAttempted = true;
284-
this.TestingIsGPUReady = false;
217+
this.TestingLastFlushUsedGPU = false;
285218
this.TestingLastGPUInitializationFailure = null;
286-
this.TestingLiveCoverageCount = 0;
287-
this.TestingComputePathVisibleCommandCount = 0;
288-
this.TestingComputePathDefinitionCount = 0;
289-
this.TestingComputePathTileBinEntryCount = 0;
290219

291220
if (!WebGPUSceneDispatch.TryCreateStagedScene(configuration, target, compositionScene.Commands, out bool exceedsBindingLimit, out WebGPUStagedScene stagedScene, out string? error))
292221
{
293222
this.TestingLastGPUInitializationFailure = exceedsBindingLimit
294223
? error ?? "The staged WebGPU scene exceeded the current binding limits."
295224
: error ?? "Failed to create the staged WebGPU scene.";
296-
this.TestingPrepareCoverageCallCount += commandCount;
297-
this.TestingFallbackPrepareCoverageCallCount += commandCount;
298-
this.TestingFallbackCompositeCoverageCallCount += commandCount;
299-
this.TestingReleaseCoverageCallCount += commandCount;
300225
this.FlushCompositionsFallback(configuration, target, compositionScene, compositionBounds: null);
301226
return;
302227
}
303228

304229
try
305230
{
306-
this.TestingIsGPUReady = true;
307-
this.TestingComputePathVisibleCommandCount += stagedScene.EncodedScene.FillCount;
308-
this.TestingComputePathDefinitionCount += stagedScene.EncodedScene.UniqueDefinitionCount;
309-
this.TestingComputePathTileBinEntryCount += stagedScene.EncodedScene.TotalTileMembershipCount;
231+
this.TestingLastFlushUsedGPU = true;
310232

311233
if (stagedScene.EncodedScene.FillCount == 0)
312234
{
@@ -315,25 +237,17 @@ public void FlushCompositions<TPixel>(
315237

316238
if (WebGPUSceneDispatch.TryRenderStagedScene(ref stagedScene, out error))
317239
{
318-
this.TestingPrepareCoverageCallCount += stagedScene.EncodedScene.FillCount;
319-
this.TestingGPUPrepareCoverageCallCount += stagedScene.EncodedScene.FillCount;
320-
this.TestingGPUCompositeCoverageCallCount += stagedScene.EncodedScene.FillCount;
321-
this.TestingReleaseCoverageCallCount += stagedScene.EncodedScene.FillCount;
322240
return;
323241
}
324242

325-
this.TestingIsGPUReady = false;
243+
this.TestingLastFlushUsedGPU = false;
326244
this.TestingLastGPUInitializationFailure = error ?? "The staged WebGPU scene dispatch failed.";
327245
}
328246
finally
329247
{
330248
stagedScene.Dispose();
331249
}
332250

333-
this.TestingPrepareCoverageCallCount += commandCount;
334-
this.TestingFallbackPrepareCoverageCallCount += commandCount;
335-
this.TestingFallbackCompositeCoverageCallCount += commandCount;
336-
this.TestingReleaseCoverageCallCount += commandCount;
337251
this.FlushCompositionsFallback(configuration, target, compositionScene, compositionBounds: null);
338252
}
339253

@@ -355,7 +269,7 @@ public ICanvasFrame<TPixel> CreateLayerFrame<TPixel>(
355269
WebGPU api = lease.Api;
356270
Device* device = (Device*)parentCapability!.Device;
357271

358-
WebGPUFlushContext.DeviceSharedState deviceState = WebGPUFlushContext.GetOrCreateDeviceState(api, device);
272+
WebGPURuntime.DeviceSharedState deviceState = WebGPURuntime.GetOrCreateDeviceState(api, device);
359273
if (requiredFeature == FeatureName.Undefined || deviceState.HasFeature(requiredFeature))
360274
{
361275
TextureFormat textureFormat = WebGPUTextureFormatMapper.ToSilk(formatId);
@@ -699,11 +613,8 @@ public void Dispose()
699613
return;
700614
}
701615

702-
WebGPUFlushContext.ClearDeviceStateCache();
703-
704-
this.TestingLiveCoverageCount = 0;
705-
this.TestingIsGPUReady = false;
706-
this.TestingGPUInitializationAttempted = false;
616+
this.TestingLastFlushUsedGPU = false;
617+
this.TestingLastGPUInitializationFailure = null;
707618
this.isDisposed = true;
708619
}
709620

0 commit comments

Comments
 (0)