Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
29 changes: 23 additions & 6 deletions gpu/gpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strconv"
"strings"

"cogentcore.org/core/base/errors"
"cogentcore.org/core/base/reflectx"
"github.com/cogentcore/webgpu/wgpu"
)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions system/driver/web/drawer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package web
import (
"image"
"image/draw"
"strings"
"syscall/js"

"cogentcore.org/core/gpu"
Expand Down Expand Up @@ -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)
Expand Down
Loading