Skip to content

Commit 0386d47

Browse files
Use explicit native mappings instead of casts
1 parent 506b059 commit 0386d47

8 files changed

Lines changed: 73 additions & 22 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ internal static bool TryGetCompositeTextureFormat<TPixel>(out WebGPUTextureForma
6262
return false;
6363
}
6464

65-
formatId = WebGPUTextureFormatMapper.FromSilk(r.TextureFormat);
65+
formatId = WebGPUTextureFormatMapper.FromNative(r.TextureFormat);
6666
requiredFeature = r.RequiredFeature;
6767
return true;
6868
}
@@ -79,7 +79,7 @@ internal static void GetCompositeTextureFormatInfo(
7979
out TextureFormat textureFormat,
8080
out FeatureName requiredFeature)
8181
{
82-
textureFormat = WebGPUTextureFormatMapper.ToSilk(format);
82+
textureFormat = WebGPUTextureFormatMapper.ToNative(format);
8383
requiredFeature = Find(textureFormat).RequiredFeature;
8484
}
8585

src/ImageSharp.Drawing.WebGPU/WebGPUDrawingBackend.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void RenderScene<TPixel>(
121121
// so staging does not repeat the generic frame capability checks.
122122
_ = nativeTarget.TryGetNativeSurface(out NativeSurface? nativeSurface);
123123
WebGPUNativeTarget webGPUTarget = nativeSurface!.GetNativeTarget<WebGPUNativeTarget>();
124-
TextureFormat textureFormat = WebGPUTextureFormatMapper.ToSilk(webGPUTarget.TargetFormat);
124+
TextureFormat textureFormat = WebGPUTextureFormatMapper.ToNative(webGPUTarget.TargetFormat);
125125

126126
if (webGPUTarget.TargetFormat != formatId)
127127
{

src/ImageSharp.Drawing.WebGPU/WebGPUFlushContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public static WebGPUFlushContext Create<TPixel>(
175175
_ = frame.TryGetNativeSurface(out NativeSurface? nativeSurface);
176176
WebGPUNativeTarget nativeTarget = nativeSurface!.GetNativeTarget<WebGPUNativeTarget>();
177177
WebGPU api = WebGPURuntime.GetApi();
178-
TextureFormat textureFormat = WebGPUTextureFormatMapper.ToSilk(nativeTarget.TargetFormat);
178+
TextureFormat textureFormat = WebGPUTextureFormatMapper.ToNative(nativeTarget.TargetFormat);
179179
Rectangle bounds = frame.Bounds;
180180
Rectangle nativeBounds = new(0, 0, nativeTarget.Width, nativeTarget.Height);
181181
WebGPURuntime.DeviceSharedState deviceState = WebGPURuntime.GetOrCreateDeviceState(api, nativeTarget.DeviceHandle);

src/ImageSharp.Drawing.WebGPU/WebGPUSurfaceResources.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ private void ConfigureSurfaceCore(WebGPUPresentMode presentMode, Size framebuffe
236236
SurfaceConfiguration surfaceConfiguration = new()
237237
{
238238
Usage = TextureUsage.RenderAttachment | TextureUsage.CopySrc | TextureUsage.CopyDst | TextureUsage.TextureBinding,
239-
Format = WebGPUTextureFormatMapper.ToSilk(this.Format),
240-
PresentMode = (SilkPresentMode)(int)presentMode,
239+
Format = WebGPUTextureFormatMapper.ToNative(this.Format),
240+
PresentMode = ToNative(presentMode),
241241
Width = (uint)framebufferSize.Width,
242242
Height = (uint)framebufferSize.Height,
243243
};
@@ -248,6 +248,15 @@ private void ConfigureSurfaceCore(WebGPUPresentMode presentMode, Size framebuffe
248248
this.Api.SurfaceConfigure((Surface*)surfaceReference.Handle, ref surfaceConfiguration);
249249
}
250250

251+
private static SilkPresentMode ToNative(WebGPUPresentMode presentMode)
252+
=> presentMode switch
253+
{
254+
WebGPUPresentMode.Fifo => SilkPresentMode.Fifo,
255+
WebGPUPresentMode.Immediate => SilkPresentMode.Immediate,
256+
WebGPUPresentMode.Mailbox => SilkPresentMode.Mailbox,
257+
_ => throw new InvalidOperationException("The WebGPU present mode mapping is incomplete.")
258+
};
259+
251260
/// <summary>
252261
/// Acquires the next presentable frame from <see cref="SurfaceHandle"/> and wraps it as a
253262
/// <see cref="WebGPUSurfaceFrame"/> with a ready-to-use <see cref="DrawingCanvas"/>.

src/ImageSharp.Drawing.WebGPU/WebGPUTextureFormat.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace SixLabors.ImageSharp.Drawing.Processing.Backends;
1010
/// </summary>
1111
/// <remarks>
1212
/// Only formats with storage texture binding support are included.
13-
/// Numeric values match the WebGPU texture-format constants.
1413
/// </remarks>
1514
public enum WebGPUTextureFormat
1615
{

src/ImageSharp.Drawing.WebGPU/WebGPUTextureFormatMapper.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,28 @@ internal static class WebGPUTextureFormatMapper
1515
/// </summary>
1616
/// <param name="formatId">The public texture format identifier.</param>
1717
/// <returns>The matching <see cref="TextureFormat"/> value.</returns>
18-
public static TextureFormat ToSilk(WebGPUTextureFormat formatId)
19-
=> (TextureFormat)(int)formatId;
18+
public static TextureFormat ToNative(WebGPUTextureFormat formatId)
19+
=> formatId switch
20+
{
21+
WebGPUTextureFormat.Rgba8Unorm => TextureFormat.Rgba8Unorm,
22+
WebGPUTextureFormat.Rgba8Snorm => TextureFormat.Rgba8Snorm,
23+
WebGPUTextureFormat.Bgra8Unorm => TextureFormat.Bgra8Unorm,
24+
WebGPUTextureFormat.Rgba16Float => TextureFormat.Rgba16float,
25+
_ => throw new InvalidOperationException("The WebGPU texture format mapping is incomplete.")
26+
};
2027

2128
/// <summary>
2229
/// Converts a native texture format to the corresponding public WebGPU texture format identifier.
2330
/// </summary>
2431
/// <param name="textureFormat">The native texture format.</param>
2532
/// <returns>The matching <see cref="WebGPUTextureFormat"/> value.</returns>
26-
public static WebGPUTextureFormat FromSilk(TextureFormat textureFormat)
27-
=> (WebGPUTextureFormat)(int)textureFormat;
33+
public static WebGPUTextureFormat FromNative(TextureFormat textureFormat)
34+
=> textureFormat switch
35+
{
36+
TextureFormat.Rgba8Unorm => WebGPUTextureFormat.Rgba8Unorm,
37+
TextureFormat.Rgba8Snorm => WebGPUTextureFormat.Rgba8Snorm,
38+
TextureFormat.Bgra8Unorm => WebGPUTextureFormat.Bgra8Unorm,
39+
TextureFormat.Rgba16float => WebGPUTextureFormat.Rgba16Float,
40+
_ => throw new InvalidOperationException("The native texture format mapping is incomplete.")
41+
};
2842
}

src/ImageSharp.Drawing.WebGPU/WebGPUWindow.cs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,41 @@ private static WindowOptions CreateSilkOptions(WebGPUWindowOptions options)
533533

534534
private static Point ToPoint(Vector2D<int> value) => new(value.X, value.Y);
535535

536-
private static NativeWindowState ToNative(WebGPUWindowState state) => (NativeWindowState)(int)state;
537-
538-
private static WebGPUWindowState FromNative(NativeWindowState state) => (WebGPUWindowState)(int)state;
539-
540-
private static NativeWindowBorder ToNative(WebGPUWindowBorder border) => (NativeWindowBorder)(int)border;
541-
542-
private static WebGPUWindowBorder FromNative(NativeWindowBorder border) => (WebGPUWindowBorder)(int)border;
536+
private static NativeWindowState ToNative(WebGPUWindowState state)
537+
=> state switch
538+
{
539+
WebGPUWindowState.Normal => NativeWindowState.Normal,
540+
WebGPUWindowState.Minimized => NativeWindowState.Minimized,
541+
WebGPUWindowState.Maximized => NativeWindowState.Maximized,
542+
WebGPUWindowState.Fullscreen => NativeWindowState.Fullscreen,
543+
_ => throw new InvalidOperationException("The WebGPU window state mapping is incomplete.")
544+
};
545+
546+
private static WebGPUWindowState FromNative(NativeWindowState state)
547+
=> state switch
548+
{
549+
NativeWindowState.Normal => WebGPUWindowState.Normal,
550+
NativeWindowState.Minimized => WebGPUWindowState.Minimized,
551+
NativeWindowState.Maximized => WebGPUWindowState.Maximized,
552+
NativeWindowState.Fullscreen => WebGPUWindowState.Fullscreen,
553+
_ => throw new InvalidOperationException("The native window state mapping is incomplete.")
554+
};
555+
556+
private static NativeWindowBorder ToNative(WebGPUWindowBorder border)
557+
=> border switch
558+
{
559+
WebGPUWindowBorder.Resizable => NativeWindowBorder.Resizable,
560+
WebGPUWindowBorder.Fixed => NativeWindowBorder.Fixed,
561+
WebGPUWindowBorder.Hidden => NativeWindowBorder.Hidden,
562+
_ => throw new InvalidOperationException("The WebGPU window border mapping is incomplete.")
563+
};
564+
565+
private static WebGPUWindowBorder FromNative(NativeWindowBorder border)
566+
=> border switch
567+
{
568+
NativeWindowBorder.Resizable => WebGPUWindowBorder.Resizable,
569+
NativeWindowBorder.Fixed => WebGPUWindowBorder.Fixed,
570+
NativeWindowBorder.Hidden => WebGPUWindowBorder.Hidden,
571+
_ => throw new InvalidOperationException("The native window border mapping is incomplete.")
572+
};
543573
}

tests/ImageSharp.Drawing.Tests/Processing/Backends/WebGPUTextureFormatMapperTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace SixLabors.ImageSharp.Drawing.Tests.Processing.Backends;
99
public class WebGPUTextureFormatMapperTests
1010
{
1111
[Fact]
12-
public void Mapper_UsesExactSilkEnumValues_ForAllSupportedFormats()
12+
public void Mapper_UsesExplicitMappings_ForAllSupportedFormats()
1313
{
1414
(WebGPUTextureFormat Drawing, TextureFormat Silk)[] mappings =
1515
[
@@ -23,9 +23,8 @@ public void Mapper_UsesExactSilkEnumValues_ForAllSupportedFormats()
2323

2424
foreach ((WebGPUTextureFormat drawing, TextureFormat silk) in mappings)
2525
{
26-
Assert.Equal((int)silk, (int)drawing);
27-
Assert.Equal(silk, WebGPUTextureFormatMapper.ToSilk(drawing));
28-
Assert.Equal(drawing, WebGPUTextureFormatMapper.FromSilk(silk));
26+
Assert.Equal(silk, WebGPUTextureFormatMapper.ToNative(drawing));
27+
Assert.Equal(drawing, WebGPUTextureFormatMapper.FromNative(silk));
2928
}
3029
}
3130
}

0 commit comments

Comments
 (0)