From 37f6419a20334c13ee16803c9ce0f67d19079a63 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Sun, 6 Nov 2022 22:15:07 -0800 Subject: [PATCH] WebGPUSurface: Add support for WASM/HTML Canvas surface creation Now, we dont actually support WASM yet, but this code is here for that time (soon hopefully!) --- src/WebGPU/Silk.NET.WebGPU/WebGPUSurface.cs | 29 +++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/WebGPU/Silk.NET.WebGPU/WebGPUSurface.cs b/src/WebGPU/Silk.NET.WebGPU/WebGPUSurface.cs index 4538418846..5dc04aa7ad 100644 --- a/src/WebGPU/Silk.NET.WebGPU/WebGPUSurface.cs +++ b/src/WebGPU/Silk.NET.WebGPU/WebGPUSurface.cs @@ -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; @@ -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 { @@ -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; } }