diff --git a/go.mod b/go.mod index 6551da10e4..baaaa58ae8 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/bramvdbogaerde/go-scp v1.4.0 github.com/chewxy/math32 v1.10.1 github.com/cogentcore/reisen v0.0.0-20240814194831-4d884b6e7666 - github.com/cogentcore/webgpu v0.0.0-20250117020559-3809548f4891 + github.com/cogentcore/webgpu v0.0.0-20250118183535-3dd1436165cf github.com/cogentcore/yaegi v0.0.0-20240724064145-e32a03faad56 github.com/coreos/go-oidc/v3 v3.10.0 github.com/ericchiang/css v1.3.0 diff --git a/go.sum b/go.sum index 1bdb06ad1e..8c4c3db4b1 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,8 @@ github.com/chewxy/math32 v1.10.1 h1:LFpeY0SLJXeaiej/eIp2L40VYfscTvKh/FSEZ68uMkU= github.com/chewxy/math32 v1.10.1/go.mod h1:dOB2rcuFrCn6UHrze36WSLVPKtzPMRAQvBvUwkSsLqs= github.com/cogentcore/reisen v0.0.0-20240814194831-4d884b6e7666 h1:gmXMw/Xcva/2V5qRO920q4am1odNE0xFEGBzG7y7cus= github.com/cogentcore/reisen v0.0.0-20240814194831-4d884b6e7666/go.mod h1:HoDh/nWYrLffGjfVxUmbJHb0yZvcV3TwrN73WurddNs= -github.com/cogentcore/webgpu v0.0.0-20250117020559-3809548f4891 h1:cIwWHCSlztHgtm6YSrKuRJqeLVtski0jXttMa1j4TLc= -github.com/cogentcore/webgpu v0.0.0-20250117020559-3809548f4891/go.mod h1:ciqaxChrmRRMU1SnI5OE12Cn3QWvOKO+e5nSy+N9S1o= +github.com/cogentcore/webgpu v0.0.0-20250118183535-3dd1436165cf h1:efac1kg29kwhSLyMd9EjwHbNX8jJpiRG5Dm2QIb56YQ= +github.com/cogentcore/webgpu v0.0.0-20250118183535-3dd1436165cf/go.mod h1:ciqaxChrmRRMU1SnI5OE12Cn3QWvOKO+e5nSy+N9S1o= github.com/cogentcore/yaegi v0.0.0-20240724064145-e32a03faad56 h1:Fz1uHiFCHnijFcMXzn36KLamcx5q4pxoR5rKCrcXIcQ= github.com/cogentcore/yaegi v0.0.0-20240724064145-e32a03faad56/go.mod h1:+MGpZ0srBmeJ7aaOLTdVss8WLolt0/y/plVHLpxgd3A= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= diff --git a/gpu/gpu.go b/gpu/gpu.go index 39c9862b80..27bef9df28 100644 --- a/gpu/gpu.go +++ b/gpu/gpu.go @@ -14,6 +14,7 @@ import ( "strconv" "strings" + "cogentcore.org/core/base/errors" "cogentcore.org/core/base/reflectx" "github.com/cogentcore/webgpu/wgpu" ) @@ -109,34 +110,50 @@ type GPU struct { // NewGPU returns a new GPU, configured and ready to use. // If only doing compute, use [NewComputeGPU]. -// The surface is used to select an appropriate adapter, and -// is recommended but not essential. +// The surface can be used to select an appropriate adapter, and +// is recommended but not essential. Returns nil and logs an error +// if the current platform is not supported by WebGPU. func NewGPU(sf *wgpu.Surface) *GPU { gp := &GPU{} - gp.init(sf) - return gp + if errors.Log(gp.init(sf)) == nil { + return gp + } + return nil } // NewComputeGPU returns a new GPU, configured and ready to use, // for purely compute use, which causes it to listen to // use the GPU_COMPUTE_DEVICE variable for which GPU device to use. +// Returns nil and logs an error if the current platform +// is not supported by WebGPU. func NewComputeGPU() *GPU { gp := &GPU{} gp.ComputeOnly = true - gp.init(nil) - return gp + if errors.Log(gp.init(nil)) == nil { + return gp + } + return nil } // init configures the GPU func (gp *GPU) init(sf *wgpu.Surface) error { inst := Instance() + if inst == nil { + return errors.New("WebGPU is not supported on this machine: could not create an instance") + } gpIndex := 0 if gp.ComputeOnly { gpus := inst.EnumerateAdapters(nil) + if len(gpus) == 0 { + return errors.New("WebGPU is not supported on this machine: no adapters available") + } gpIndex = gp.SelectComputeGPU(gpus) gp.GPU = gpus[gpIndex] } else { gpus := inst.EnumerateAdapters(nil) + if len(gpus) == 0 { + return errors.New("WebGPU is not supported on this machine: no adapters available") + } gpIndex = gp.SelectGraphicsGPU(gpus) gp.GPU = gpus[gpIndex] // note: below is a more standard way of doing it, but until we fix the issues diff --git a/system/driver/web/drawer.go b/system/driver/web/drawer.go index 147a1ae593..700d221c51 100644 --- a/system/driver/web/drawer.go +++ b/system/driver/web/drawer.go @@ -9,7 +9,6 @@ package web import ( "image" "image/draw" - "strings" "syscall/js" "cogentcore.org/core/gpu" @@ -41,15 +40,11 @@ func (dw *Drawer) AsGPUDrawer() *gpudraw.Drawer { // InitDrawer sets the [Drawer] to a WebGPU-based drawer if the browser // supports WebGPU and a backup 2D image drawer otherwise. func (a *App) InitDrawer() { - // TODO(wgpu): various mobile and Linux browsers do not fully support WebGPU yet. - isMobile := a.SystemPlatform().IsMobile() || a.SystemPlatform() == system.Linux - // TODO(wgpu): Firefox currently does not support WebGPU in release builds. - isFirefox := strings.Contains(js.Global().Get("navigator").Get("userAgent").String(), "Firefox") - if isMobile || isFirefox || !js.Global().Get("navigator").Get("gpu").Truthy() { + gp := gpu.NewGPU(nil) + if gp == nil { a.Draw.context2D = js.Global().Get("document").Call("querySelector", "canvas").Call("getContext", "2d") return } - gp := gpu.NewGPU(nil) surf := gpu.Instance().CreateSurface(nil) sf := gpu.NewSurface(gp, surf, a.Scrn.PixelSize, 1, gpu.UndefinedType) a.Draw.wgpu = gpudraw.NewDrawer(gp, sf)