Skip to content

Commit

Permalink
Make WebGPU surface creation an extension method on IView
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley committed Nov 3, 2022
1 parent 0ce52c7 commit 960be3f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/Lab/Experiments/WebGPUTriangle/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static unsafe class Program
{
// ReSharper disable once InconsistentNaming
private static WebGPU wgpu = null!;
private static WebGPUDisposal? webGpuDisposal;
private static WebGPUDisposal? webGpuDisposal = null!;
private static IWindow? _Window;

private static Surface* _Surface;
Expand Down Expand Up @@ -73,7 +73,7 @@ private static void WindowOnLoad()

webGpuDisposal = new WebGPUDisposal(wgpu);

_Surface = WebGPUWindow.CreateSurface(wgpu, _Window);
_Surface = _Window.CreateWebGPUSurface(wgpu);

{ //Get adapter
var requestAdapterOptions = new RequestAdapterOptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#nullable enable
Silk.NET.Windowing.Extensions.WebGPU.WebGPUWindow
~static Silk.NET.Windowing.Extensions.WebGPU.WebGPUWindow.CreateSurface(Silk.NET.WebGPU.WebGPU wgpu, Silk.NET.Windowing.IView window, Silk.NET.WebGPU.Instance* instance = null) -> Silk.NET.WebGPU.Surface*
~static Silk.NET.Windowing.Extensions.WebGPU.WebGPUWindow.CreateWebGPUSurface(this Silk.NET.Windowing.IView view, Silk.NET.WebGPU.WebGPU wgpu, Silk.NET.WebGPU.Instance* instance = null) -> Silk.NET.WebGPU.Surface*
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#nullable enable
Silk.NET.Windowing.Extensions.WebGPU.WebGPUWindow
~static Silk.NET.Windowing.Extensions.WebGPU.WebGPUWindow.CreateSurface(Silk.NET.WebGPU.WebGPU wgpu, Silk.NET.Windowing.IView window, Silk.NET.WebGPU.Instance* instance = null) -> Silk.NET.WebGPU.Surface*
~static Silk.NET.Windowing.Extensions.WebGPU.WebGPUWindow.CreateWebGPUSurface(this Silk.NET.Windowing.IView view, Silk.NET.WebGPU.WebGPU wgpu, Silk.NET.WebGPU.Instance* instance = null) -> Silk.NET.WebGPU.Surface*
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#nullable enable
Silk.NET.WIndowing.Extensions.WebGPU.WebGPUWindow
Silk.NET.Windowing.Extensions.WebGPU.WebGPUWindow
~static Silk.NET.Windowing.Extensions.WebGPU.WebGPUWindow.CreateSurface(Silk.NET.WebGPU.WebGPU wgpu, Silk.NET.Windowing.IView window, Silk.NET.WebGPU.Instance* instance = null) -> Silk.NET.WebGPU.Surface*
~static Silk.NET.Windowing.Extensions.WebGPU.WebGPUWindow.CreateWebGPUSurface(this Silk.NET.Windowing.IView view, Silk.NET.WebGPU.WebGPU wgpu, Silk.NET.WebGPU.Instance* instance = null) -> Silk.NET.WebGPU.Surface*
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#nullable enable
Silk.NET.Windowing.Extensions.WebGPU.WebGPUWindow
~static Silk.NET.Windowing.Extensions.WebGPU.WebGPUWindow.CreateSurface(Silk.NET.WebGPU.WebGPU wgpu, Silk.NET.Windowing.IView window, Silk.NET.WebGPU.Instance* instance = null) -> Silk.NET.WebGPU.Surface*
~static Silk.NET.Windowing.Extensions.WebGPU.WebGPUWindow.CreateWebGPUSurface(this Silk.NET.Windowing.IView view, Silk.NET.WebGPU.WebGPU wgpu, Silk.NET.WebGPU.Instance* instance = null) -> Silk.NET.WebGPU.Surface*
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public static class WebGPUWindow
/// Creates a WebGPU surface from a window.
/// </summary>
/// <param name="wgpu">A WebGPU instance</param>
/// <param name="window">The window to get the surface from</param>
/// <param name="view">The window to get the surface from</param>
/// <returns>A newly created Surface*/</returns>
/// <exception cref="NotSupportedException">Throws when an unsupported platform is detected.</exception>
public static unsafe Surface* CreateSurface(NET.WebGPU.WebGPU wgpu, IView window, Instance* instance = null)
public static unsafe Surface* CreateWebGPUSurface(this IView view, NET.WebGPU.WebGPU wgpu, Instance* instance = null)
{
var descriptor = new SurfaceDescriptor();

if (window.Native.X11 != null)
if (view.Native.X11 != null)
{
var xlibDescriptor = new SurfaceDescriptorFromXlibWindow
{
Expand All @@ -31,17 +31,17 @@ public static class WebGPUWindow
Next = null,
SType = SType.SurfaceDescriptorFromXlibWindow
},
Display = (void*) window.Native.X11.Value.Display,
Window = (uint) window.Native.X11.Value.Window
Display = (void*) view.Native.X11.Value.Display,
Window = (uint) view.Native.X11.Value.Window
};

descriptor.NextInChain = (ChainedStruct*) (&xlibDescriptor);
}
else if (window.Native.Cocoa != null)
else if (view.Native.Cocoa != null)
{
throw new NotSupportedException("WebGPU on MacOS is not supported at this time!)");

var cocoa = window.Native.Cocoa.Value;
var cocoa = view.Native.Cocoa.Value;

var cocoaDescriptor = new SurfaceDescriptorFromMetalLayer
{
Expand All @@ -53,7 +53,7 @@ public static class WebGPUWindow
Layer = null //TODO: Get the layer from the window
};
}
else if (window.Native.Wayland != null)
else if (view.Native.Wayland != null)
{
var waylandDescriptor = new SurfaceDescriptorFromWaylandSurface
{
Expand All @@ -62,13 +62,13 @@ public static class WebGPUWindow
Next = null,
SType = SType.SurfaceDescriptorFromWaylandSurface
},
Display = (void*) window.Native.Wayland.Value.Display,
Surface = (void*) window.Native.Wayland.Value.Surface
Display = (void*) view.Native.Wayland.Value.Display,
Surface = (void*) view.Native.Wayland.Value.Surface
};

descriptor.NextInChain = (ChainedStruct*) (&waylandDescriptor);
}
else if (window.Native.Win32 != null)
else if (view.Native.Win32 != null)
{
var win32Descriptor = new SurfaceDescriptorFromWindowsHWND
{
Expand All @@ -77,13 +77,13 @@ public static class WebGPUWindow
Next = null,
SType = SType.SurfaceDescriptorFromWindowsHwnd
},
Hwnd = (void*) window.Native.Win32.Value.Hwnd,
Hinstance = (void*) window.Native.Win32.Value.HInstance
Hwnd = (void*) view.Native.Win32.Value.Hwnd,
Hinstance = (void*) view.Native.Win32.Value.HInstance
};

descriptor.NextInChain = (ChainedStruct*) (&win32Descriptor);
}
else if (window.Native.Android != null)
else if (view.Native.Android != null)
{
var androidDescriptor = new SurfaceDescriptorFromAndroidNativeWindow
{
Expand All @@ -92,14 +92,14 @@ public static class WebGPUWindow
Next = null,
SType = SType.SurfaceDescriptorFromAndroidNativeWindow
},
Window = (void*) window.Native.Android.Value.Window
Window = (void*) view.Native.Android.Value.Window
};

descriptor.NextInChain = (ChainedStruct*) (&androidDescriptor);
}
else
{
throw new NotSupportedException($"Your platform is not supported! {window.Native.Kind}");
throw new NotSupportedException($"Your platform is not supported! {view.Native.Kind}");
}

return wgpu.InstanceCreateSurface(instance, ref descriptor);
Expand Down

0 comments on commit 960be3f

Please sign in to comment.