Skip to content

Commit 6e0b36c

Browse files
Add WebGPU environment options and preference
1 parent d5a87e2 commit 6e0b36c

5 files changed

Lines changed: 71 additions & 1 deletion

File tree

src/ImageSharp.Drawing.WebGPU/WEBGPU_BACKEND.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ The public WebGPU surface area around this backend is small and target-first.
2727

2828
`WebGPUDeviceContext` is internal infrastructure used by targets and surfaces. It is not part of the public WebGPU entry-point model.
2929

30+
`WebGPUEnvironment.Options` configures the library-managed WebGPU environment before first use. Set it before
31+
constructing WebGPU objects or calling support probes; the shared runtime reads the current options during first
32+
initialization and does not reconfigure an existing device after that point.
33+
3034
Those types all exist to get a `DrawingCanvas` over a native WebGPU target. Once the canvas flushes, `WebGPUDrawingBackend` becomes the execution boundary.
3135

3236
The support probes also live outside the backend:

src/ImageSharp.Drawing.WebGPU/WebGPUEnvironment.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ namespace SixLabors.ImageSharp.Drawing.Processing.Backends;
99
/// </summary>
1010
public static class WebGPUEnvironment
1111
{
12+
/// <summary>
13+
/// Gets or sets the options used when the library-managed WebGPU environment is initialized.
14+
/// </summary>
15+
/// <remarks>
16+
/// Assign this before constructing WebGPU objects or calling support probes. The shared WebGPU runtime reads the
17+
/// current options during first initialization; changing them later does not reconfigure an existing device.
18+
/// </remarks>
19+
public static WebGPUEnvironmentOptions Options { get; set; } = WebGPUEnvironmentOptions.Default;
20+
1221
/// <summary>
1322
/// Gets or sets the callback invoked when the native WebGPU runtime reports an uncaptured device error.
1423
/// </summary>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
namespace SixLabors.ImageSharp.Drawing.Processing.Backends;
5+
6+
/// <summary>
7+
/// Options for the library-managed WebGPU environment.
8+
/// </summary>
9+
/// <remarks>
10+
/// Assign these options before constructing WebGPU objects or calling support probes. The shared WebGPU runtime reads
11+
/// the current options during first initialization; changing them later does not reconfigure an existing device.
12+
/// </remarks>
13+
public sealed class WebGPUEnvironmentOptions
14+
{
15+
/// <summary>
16+
/// Gets the default WebGPU environment options.
17+
/// </summary>
18+
public static WebGPUEnvironmentOptions Default { get; } = new();
19+
20+
/// <summary>
21+
/// Gets or sets the adapter power preference requested for the library-managed device.
22+
/// </summary>
23+
public WebGPUPowerPreference PowerPreference { get; set; } = WebGPUPowerPreference.HighPerformance;
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
namespace SixLabors.ImageSharp.Drawing.Processing.Backends;
5+
6+
/// <summary>
7+
/// Describes the adapter power preference requested for the library-managed WebGPU device.
8+
/// </summary>
9+
public enum WebGPUPowerPreference
10+
{
11+
/// <summary>
12+
/// Uses the native WebGPU runtime default.
13+
/// </summary>
14+
Default,
15+
16+
/// <summary>
17+
/// Prefers a lower-power adapter when the platform provides one.
18+
/// </summary>
19+
LowPower,
20+
21+
/// <summary>
22+
/// Prefers a high-performance adapter when the platform provides one.
23+
/// </summary>
24+
HighPerformance
25+
}

src/ImageSharp.Drawing.WebGPU/WebGPURuntime.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,17 @@ void Callback(RequestAdapterStatus status, Adapter* adapterPtr, byte* message, v
533533
}
534534

535535
using PfnRequestAdapterCallback callbackPtr = PfnRequestAdapterCallback.From(Callback);
536+
WebGPUEnvironmentOptions environmentOptions = WebGPUEnvironment.Options;
537+
536538
RequestAdapterOptions options = new()
537539
{
538-
PowerPreference = PowerPreference.HighPerformance
540+
PowerPreference = environmentOptions.PowerPreference switch
541+
{
542+
WebGPUPowerPreference.Default => PowerPreference.Undefined,
543+
WebGPUPowerPreference.LowPower => PowerPreference.LowPower,
544+
WebGPUPowerPreference.HighPerformance => PowerPreference.HighPerformance,
545+
_ => throw new InvalidOperationException("The WebGPU power preference mapping is incomplete.")
546+
}
539547
};
540548

541549
api.InstanceRequestAdapter(instance, in options, callbackPtr, null);

0 commit comments

Comments
 (0)