Skip to content

Commit

Permalink
WebGPUSurface: Add support for WASM/HTML Canvas surface creation
Browse files Browse the repository at this point in the history
Now, we dont actually support WASM yet, but this code is here for that time (soon hopefully!)
  • Loading branch information
Beyley committed Nov 7, 2022
1 parent 2221a1d commit 37f6419
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/WebGPU/Silk.NET.WebGPU/WebGPUSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;
using Silk.NET.Core.Contexts;
using Silk.NET.Core.Native;

namespace Silk.NET.WebGPU;

Expand All @@ -22,7 +24,21 @@ public static class WebGPUSurface
{
var descriptor = new SurfaceDescriptor();

if (view.Native.X11 != null)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER")))
{
var htmlDescriptor = new SurfaceDescriptorFromCanvasHTMLSelector
{
Chain = new ChainedStruct
{
Next = null,
SType = SType.SurfaceDescriptorFromCanvasHtmlselector
},
Selector = (byte*) SilkMarshal.StringToPtr("canvas")
};

descriptor.NextInChain = (ChainedStruct*) (&htmlDescriptor);
}
else if (view.Native.X11 != null)
{
var xlibDescriptor = new SurfaceDescriptorFromXlibWindow
{
Expand Down Expand Up @@ -102,6 +118,15 @@ public static class WebGPUSurface
throw new PlatformNotSupportedException($"Your platform is not supported! {view.Native.Kind}");
}

return wgpu.InstanceCreateSurface(instance, ref descriptor);
var surface = wgpu.InstanceCreateSurface(instance, ref descriptor);

if (descriptor.NextInChain->Next->SType == SType.SurfaceDescriptorFromCanvasHtmlselector)
{
var htmlDescriptor = (SurfaceDescriptorFromCanvasHTMLSelector*) descriptor.NextInChain;

SilkMarshal.Free((IntPtr) htmlDescriptor->Selector);
}

return surface;
}
}

0 comments on commit 37f6419

Please sign in to comment.