11// Copyright (c) Six Labors.
22// Licensed under the Six Labors Split License.
33
4- using System . Diagnostics . CodeAnalysis ;
54using Silk . NET . WebGPU ;
65using SixLabors . ImageSharp . PixelFormats ;
76
@@ -13,66 +12,45 @@ namespace SixLabors.ImageSharp.Drawing.Processing.Backends;
1312internal static unsafe class WebGPURenderTargetAllocation
1413{
1514 /// <summary>
16- /// Tries to allocate a WebGPU render target for the specified pixel type.
15+ /// Allocates a WebGPU render target for the specified pixel type.
1716 /// </summary>
1817 /// <typeparam name="TPixel">The pixel format.</typeparam>
1918 /// <param name="api">The WebGPU API instance used to allocate native resources.</param>
2019 /// <param name="deviceHandle">The wrapped <c>WGPUDevice*</c> handle.</param>
2120 /// <param name="queueHandle">The wrapped <c>WGPUQueue*</c> handle.</param>
2221 /// <param name="width">The texture width in pixels.</param>
2322 /// <param name="height">The texture height in pixels.</param>
24- /// <param name="surface">Receives the native surface wrapping the allocated texture.</param>
2523 /// <param name="textureHandle">Receives the allocated wrapped <c>WGPUTexture*</c> handle.</param>
2624 /// <param name="textureViewHandle">Receives the allocated wrapped <c>WGPUTextureView*</c> handle.</param>
2725 /// <param name="formatId">Receives the allocated texture format identifier.</param>
28- /// <param name="error">Receives the failure reason when allocation fails.</param>
29- /// <returns><see langword="true"/> when allocation succeeds; otherwise <see langword="false"/>.</returns>
30- internal static bool TryCreateRenderTarget < TPixel > (
26+ /// <returns>The native surface wrapping the allocated texture.</returns>
27+ internal static NativeSurface CreateRenderTarget < TPixel > (
3128 WebGPU api ,
3229 WebGPUDeviceHandle deviceHandle ,
3330 WebGPUQueueHandle queueHandle ,
3431 int width ,
3532 int height ,
36- out NativeSurface surface ,
37- [ NotNullWhen ( true ) ] out WebGPUTextureHandle ? textureHandle ,
38- [ NotNullWhen ( true ) ] out WebGPUTextureViewHandle ? textureViewHandle ,
39- out WebGPUTextureFormatId formatId ,
40- out string error )
33+ out WebGPUTextureHandle textureHandle ,
34+ out WebGPUTextureViewHandle textureViewHandle ,
35+ out WebGPUTextureFormatId formatId )
4136 where TPixel : unmanaged, IPixel < TPixel >
4237 {
43- surface = new NativeSurface ( TPixel . GetPixelTypeInfo ( ) ) ;
44- textureHandle = null ;
45- textureViewHandle = null ;
46- formatId = default ;
47-
4838 if ( deviceHandle . IsInvalid )
4939 {
50- error = "Device handle must be non-zero." ;
51- return false ;
40+ throw new InvalidOperationException ( "The WebGPU device handle is invalid." ) ;
5241 }
5342
5443 if ( queueHandle . IsInvalid )
5544 {
56- error = "Queue handle must be non-zero." ;
57- return false ;
58- }
59-
60- if ( width <= 0 )
61- {
62- error = "Width must be greater than zero." ;
63- return false ;
45+ throw new InvalidOperationException ( "The WebGPU queue handle is invalid." ) ;
6446 }
6547
66- if ( height <= 0 )
67- {
68- error = "Height must be greater than zero." ;
69- return false ;
70- }
48+ Guard . MustBeGreaterThan ( width , 0 , nameof ( width ) ) ;
49+ Guard . MustBeGreaterThan ( height , 0 , nameof ( height ) ) ;
7150
7251 if ( ! WebGPUDrawingBackend . TryGetCompositeTextureFormat < TPixel > ( out formatId , out FeatureName requiredFeature ) )
7352 {
74- error = $ "Pixel type '{ typeof ( TPixel ) . Name } ' is not supported by the WebGPU backend.";
75- return false ;
53+ throw new NotSupportedException ( $ "Pixel type '{ typeof ( TPixel ) . Name } ' is not supported by the WebGPU backend.") ;
7654 }
7755
7856 using WebGPUHandle . HandleReference deviceReference = deviceHandle . AcquireReference ( ) ;
@@ -81,8 +59,7 @@ internal static bool TryCreateRenderTarget<TPixel>(
8159 if ( requiredFeature != FeatureName . Undefined &&
8260 ! WebGPURuntime . GetOrCreateDeviceState ( api , deviceHandle ) . HasFeature ( requiredFeature ) )
8361 {
84- error = $ "Device does not support required feature '{ requiredFeature } ' for pixel type '{ typeof ( TPixel ) . Name } '.";
85- return false ;
62+ throw new NotSupportedException ( $ "The WebGPU device does not support required feature '{ requiredFeature } ' for pixel type '{ typeof ( TPixel ) . Name } '.") ;
8663 }
8764
8865 TextureFormat textureFormat = WebGPUTextureFormatMapper . ToSilk ( formatId ) ;
@@ -99,8 +76,7 @@ internal static bool TryCreateRenderTarget<TPixel>(
9976 Texture * texture = api . DeviceCreateTexture ( device , in textureDescriptor ) ;
10077 if ( texture is null )
10178 {
102- error = "WebGPU.DeviceCreateTexture returned null." ;
103- return false ;
79+ throw new InvalidOperationException ( "The WebGPU device could not create a render-target texture." ) ;
10480 }
10581
10682 TextureViewDescriptor textureViewDescriptor = new ( )
@@ -118,8 +94,7 @@ internal static bool TryCreateRenderTarget<TPixel>(
11894 if ( textureView is null )
11995 {
12096 api . TextureRelease ( texture ) ;
121- error = "WebGPU.TextureCreateView returned null." ;
122- return false ;
97+ throw new InvalidOperationException ( "The WebGPU device could not create a render-target texture view." ) ;
12398 }
12499
125100 WebGPUTextureHandle ? createdTextureHandle = null ;
@@ -128,7 +103,7 @@ internal static bool TryCreateRenderTarget<TPixel>(
128103 {
129104 createdTextureHandle = new WebGPUTextureHandle ( api , ( nint ) texture , ownsHandle : true ) ;
130105 createdTextureViewHandle = new WebGPUTextureViewHandle ( api , ( nint ) textureView , ownsHandle : true ) ;
131- surface = WebGPUNativeSurfaceFactory . Create < TPixel > (
106+ NativeSurface surface = WebGPUNativeSurfaceFactory . Create < TPixel > (
132107 deviceHandle ,
133108 queueHandle ,
134109 createdTextureHandle ,
@@ -138,10 +113,9 @@ internal static bool TryCreateRenderTarget<TPixel>(
138113 height ) ;
139114 textureHandle = createdTextureHandle ;
140115 textureViewHandle = createdTextureViewHandle ;
141- error = string . Empty ;
142- return true ;
116+ return surface ;
143117 }
144- catch ( Exception ex )
118+ catch
145119 {
146120 createdTextureViewHandle ? . Dispose ( ) ;
147121 createdTextureHandle ? . Dispose ( ) ;
@@ -156,8 +130,7 @@ internal static bool TryCreateRenderTarget<TPixel>(
156130 api . TextureRelease ( texture ) ;
157131 }
158132
159- error = ex . Message ;
160- return false ;
133+ throw ;
161134 }
162135 }
163136}
0 commit comments