Skip to content

Commit 9d59659

Browse files
authored
Merge pull request #1676 from cogentcore/obwgpu
obwgpu: use oliverbestmann/webgpu instead of cogentcore/webgpu
2 parents b7d29e4 + df1c148 commit 9d59659

49 files changed

Lines changed: 194 additions & 125 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

base/iox/imagex/wrapjs_js.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111
"syscall/js"
1212

1313
"cogentcore.org/core/base/errors"
14+
"cogentcore.org/core/base/jscp"
1415
"cogentcore.org/core/colors/gradient"
15-
"github.com/cogentcore/webgpu/jsx"
16+
"github.com/oliverbestmann/webgpu/jsx"
1617
)
1718

1819
// WrapJS returns a JavaScript optimized wrapper around the given
@@ -155,7 +156,7 @@ type JSImageData struct {
155156

156157
// setImageData sets the JavaScript pointers from given bytes.
157158
func (im *JSImageData) SetImageData(src []byte, sbb image.Rectangle, options map[string]any) {
158-
jsBuf := jsx.BytesToJS(src)
159+
jsBuf := jscp.BytesToJS(src)
159160
imageData := js.Global().Get("ImageData").New(jsBuf, sbb.Dx(), sbb.Dy())
160161
im.Bounds = image.Rectangle{Max: sbb.Size()}
161162
im.Data = imageData

base/jscp/jscp.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//go:build js
2+
3+
// Package jscp provides functions for copying data to JavaScript
4+
package jscp
5+
6+
import (
7+
"syscall/js"
8+
"unsafe"
9+
)
10+
11+
// BytesToJS converts the given bytes to a js Uint8ClampedArray
12+
// by using the global wasm memory bytes. This avoids the
13+
// copying present in [js.CopyBytesToJS].
14+
func BytesToJS(b []byte) js.Value {
15+
ptr := uintptr(unsafe.Pointer(&b[0]))
16+
// We directly pass the offset and length to the constructor to avoid calling subarray or slice,
17+
// thereby improving performance and safety (this fixes a detached array buffer crash).
18+
return js.Global().Get("Uint8ClampedArray").New(js.Global().Get("wasm").Get("instance").Get("exports").Get("mem").Get("buffer"), ptr, len(b))
19+
}

base/websocket/websocket_js.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package websocket
99
import (
1010
"syscall/js"
1111

12-
"github.com/cogentcore/webgpu/jsx"
12+
"cogentcore.org/core/base/jscp"
1313
)
1414

1515
// Client represents a WebSocket client connection.
@@ -51,7 +51,7 @@ func (c *Client) Send(typ MessageTypes, msg []byte) error {
5151
c.ws.Call("send", string(msg))
5252
return nil
5353
}
54-
array := jsx.BytesToJS(msg)
54+
array := jscp.BytesToJS(msg)
5555
c.ws.Call("send", array)
5656
return nil
5757
}

docs/content/gpu.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The main gpu code is in the top-level `gpu` package, with the following sub-pack
1919

2020
* [[doc:gpu/gpudraw]] implements GPU-accelerated texture-based versions of the Go [image/draw](https://pkg.go.dev/image/draw) api. This is used by the `Composer` framework for compositing images in the `core` GUI to construct the final rendered scene, and for drawing that scene on the actual hardware window (see [[render]] for details).
2121

22-
We maintain a separate [webgpu](https://github.com/cogentcore/webgpu) package that provides a Go and JS wrapper around the rust-based [wgpu](https://github.com/gfx-rs/wgpu) and [wgpu-native](https://github.com/gfx-rs/wgpu-native) packages that actually implement WebGPU itself on the desktop and mobile. This "native" version is just as performant as the much more difficult-to-use [Vulkan](https://www.vulkan.org/) framework, which we used to use.
22+
There is a separate [webgpu](https://github.com/oliverbestmann/webgpu) package that provides a Go and JS wrapper around the rust-based [wgpu](https://github.com/gfx-rs/wgpu) and [wgpu-native](https://github.com/gfx-rs/wgpu-native) packages that actually implement WebGPU itself on the desktop and mobile. This "native" version is just as performant as the much more difficult-to-use [Vulkan](https://www.vulkan.org/) framework, which we used to use.
2323

2424
## Selecting a GPU Device
2525

go.mod

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ require (
1313
github.com/bramvdbogaerde/go-scp v1.6.0
1414
github.com/chewxy/math32 v1.11.1
1515
github.com/cogentcore/reisen v0.0.0-20240814194831-4d884b6e7666
16-
github.com/cogentcore/webgpu v0.23.1-0.20260410073005-a2ae7d757168
1716
github.com/cogentcore/yaegi v0.0.0-20260116172027-700fbf8949f3
1817
github.com/coreos/go-oidc/v3 v3.17.0
1918
github.com/ericchiang/css v1.4.0
2019
github.com/faiface/beep v1.1.0
2120
github.com/fsnotify/fsnotify v1.9.0
2221
github.com/go-fonts/latin-modern v0.3.3
23-
github.com/go-gl/glfw/v3.4/glfw v0.1.0-pre.1
22+
github.com/go-gl/glfw/v3.4/glfw v0.1.0-pre.1.0.20260406072232-3ac4aa2bb164
2423
github.com/go-text/typesetting v0.3.5-0.20260418130854-c41d02a44bec
2524
github.com/gomarkdown/markdown v0.0.0-20260417124207-7d523f7318df
2625
github.com/gorilla/websocket v1.5.3
@@ -33,6 +32,7 @@ require (
3332
github.com/mattn/go-shellwords v1.0.12
3433
github.com/mitchellh/go-homedir v1.1.0
3534
github.com/muesli/termenv v0.16.0
35+
github.com/oliverbestmann/webgpu v1.33.2
3636
github.com/pelletier/go-toml/v2 v2.2.4
3737
github.com/stretchr/testify v1.11.1
3838
github.com/tdewolff/parse/v2 v2.8.5
@@ -63,6 +63,11 @@ require (
6363
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
6464
github.com/mattn/go-isatty v0.0.20 // indirect
6565
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
66+
github.com/oliverbestmann/webgpu/libs-android v0.0.0-20260509160813-48db59792a15 // indirect
67+
github.com/oliverbestmann/webgpu/libs-darwin v0.0.0-20260509160802-b09403b07cd3 // indirect
68+
github.com/oliverbestmann/webgpu/libs-ios v0.0.0-20260509160803-765e39d2a48b // indirect
69+
github.com/oliverbestmann/webgpu/libs-linux v0.0.0-20260509160809-2fefaf7c9ead // indirect
70+
github.com/oliverbestmann/webgpu/libs-windows v0.0.0-20260509160807-0bc32b12c7bc // indirect
6671
github.com/pkg/errors v0.9.1 // indirect
6772
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
6873
github.com/rivo/uniseg v0.4.7 // indirect
@@ -75,3 +80,5 @@ require (
7580
modernc.org/knuth v0.5.4 // indirect
7681
modernc.org/token v1.1.0 // indirect
7782
)
83+
84+
replace github.com/go-gl/glfw/v3.4/glfw v0.1.0-pre.1.0.20260406072232-3ac4aa2bb164 => github.com/oliverbestmann/go-gl-glfw/v3.4/glfw v0.0.0-20260510101646-c1f83c493fe1

go.sum

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ github.com/chewxy/math32 v1.11.1 h1:b7PGHlp8KjylDoU8RrcEsRuGZhJuz8haxnKfuMMRqy8=
2929
github.com/chewxy/math32 v1.11.1/go.mod h1:dOB2rcuFrCn6UHrze36WSLVPKtzPMRAQvBvUwkSsLqs=
3030
github.com/cogentcore/reisen v0.0.0-20240814194831-4d884b6e7666 h1:gmXMw/Xcva/2V5qRO920q4am1odNE0xFEGBzG7y7cus=
3131
github.com/cogentcore/reisen v0.0.0-20240814194831-4d884b6e7666/go.mod h1:HoDh/nWYrLffGjfVxUmbJHb0yZvcV3TwrN73WurddNs=
32-
github.com/cogentcore/webgpu v0.23.1-0.20260410073005-a2ae7d757168 h1:K5en4IrBrolIAWvjrLuXRb9CvU1w+WMMi0Eu0KmcMfQ=
33-
github.com/cogentcore/webgpu v0.23.1-0.20260410073005-a2ae7d757168/go.mod h1:DQLhF8pM/WkdaGpX6pKmZJPYsoR9x+hohhA1nnNuCa8=
3432
github.com/cogentcore/yaegi v0.0.0-20260116172027-700fbf8949f3 h1:y3Djpt/g3QTjFdj8cpvy/r8FsZsEa7PqHGjgsKXbta0=
3533
github.com/cogentcore/yaegi v0.0.0-20260116172027-700fbf8949f3/go.mod h1:XkOm++pRmWlk85p+hw71ZItfTeRdzqG23+2xjP9eb+M=
3634
github.com/coreos/go-oidc/v3 v3.17.0 h1:hWBGaQfbi0iVviX4ibC7bk8OKT5qNr4klBaCHVNvehc=
@@ -56,8 +54,6 @@ github.com/go-audio/riff v1.0.0/go.mod h1:l3cQwc85y79NQFCRB7TiPoNiaijp6q8Z0Uv38r
5654
github.com/go-audio/wav v1.0.0/go.mod h1:3yoReyQOsiARkvPl3ERCi8JFjihzG6WhjYpZCf5zAWE=
5755
github.com/go-fonts/latin-modern v0.3.3 h1:g2xNgI8yzdNzIVm+qvbMryB6yGPe0pSMss8QT3QwlJ0=
5856
github.com/go-fonts/latin-modern v0.3.3/go.mod h1:tHaiWDGze4EPB0Go4cLT5M3QzRY3peya09Z/8KSCrpY=
59-
github.com/go-gl/glfw/v3.4/glfw v0.1.0-pre.1 h1:nIVzcwqIaO1mK8LFr0fGkKpgQD4wJDDHRyv4t5k40Ps=
60-
github.com/go-gl/glfw/v3.4/glfw v0.1.0-pre.1/go.mod h1:T5Dn0JwIJOX1euPZ/iT4tq6nFYtmukjcYa7937HuYK8=
6157
github.com/go-jose/go-jose/v4 v4.1.4 h1:moDMcTHmvE6Groj34emNPLs/qtYXRVcd6S7NHbHz3kA=
6258
github.com/go-jose/go-jose/v4 v4.1.4/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08=
6359
github.com/go-text/typesetting v0.3.5-0.20260418130854-c41d02a44bec h1:qkk9+cZlaY8920dQrhY8GoI91N72f58XmJgGDkkDiaA=
@@ -124,6 +120,20 @@ github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc
124120
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
125121
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
126122
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
123+
github.com/oliverbestmann/go-gl-glfw/v3.4/glfw v0.0.0-20260510101646-c1f83c493fe1 h1:NE2h8adQKd5RgELMJRDh6jk+w2AZGJnqiceettnqjAs=
124+
github.com/oliverbestmann/go-gl-glfw/v3.4/glfw v0.0.0-20260510101646-c1f83c493fe1/go.mod h1:T5Dn0JwIJOX1euPZ/iT4tq6nFYtmukjcYa7937HuYK8=
125+
github.com/oliverbestmann/webgpu v1.33.2 h1:uDs3Fx4uAiUs8E4QytblofDpLtqBRGX3pfnLZzlhWGY=
126+
github.com/oliverbestmann/webgpu v1.33.2/go.mod h1:0UEcoJ8VnwnAqo3JfzdFptplxtjhaQasc3n/nYZOrPA=
127+
github.com/oliverbestmann/webgpu/libs-android v0.0.0-20260509160813-48db59792a15 h1:HPxVSV8C8JaxGfa9hjDhzNmryoqPF3EwESBTFWpxNBo=
128+
github.com/oliverbestmann/webgpu/libs-android v0.0.0-20260509160813-48db59792a15/go.mod h1:tczQXCdsoFy+FTJVsZSve/vF8cmWEkxhvjcyY2Rujp8=
129+
github.com/oliverbestmann/webgpu/libs-darwin v0.0.0-20260509160802-b09403b07cd3 h1:NbBG2+pwqKcNfKUKtjWOj+02RkXSUWVkxY3hqzgyjSA=
130+
github.com/oliverbestmann/webgpu/libs-darwin v0.0.0-20260509160802-b09403b07cd3/go.mod h1:XoHM/ZcjQqJQyEfQjU0ScDkxvQRWfZLxKP8IrEz4xyo=
131+
github.com/oliverbestmann/webgpu/libs-ios v0.0.0-20260509160803-765e39d2a48b h1:j+uRWcmAl3Y4RPJ4rmxnc1DRHgZ6VNywZPsRHUEuD0M=
132+
github.com/oliverbestmann/webgpu/libs-ios v0.0.0-20260509160803-765e39d2a48b/go.mod h1:IV+TkwmPA0yMzZZoz0Aj4X+22WLEQq2TOyN4/0k8lgs=
133+
github.com/oliverbestmann/webgpu/libs-linux v0.0.0-20260509160809-2fefaf7c9ead h1:kkY04PFBq6I58BwK5cwJ3LRp+xy7Y5/jrkLHBdcBL68=
134+
github.com/oliverbestmann/webgpu/libs-linux v0.0.0-20260509160809-2fefaf7c9ead/go.mod h1:SOeo2YWe2UxWxOeAHyZtwaSXkBbP78cGnm7I+6lIWV0=
135+
github.com/oliverbestmann/webgpu/libs-windows v0.0.0-20260509160807-0bc32b12c7bc h1:YVCfgeByW1ibKniigozHCwF2wC26TDmAIJwQG40bCBM=
136+
github.com/oliverbestmann/webgpu/libs-windows v0.0.0-20260509160807-0bc32b12c7bc/go.mod h1:58qRJHG2+mjEu/AKJFh026bz3xE1zEHYt41i4TBM8NE=
127137
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
128138
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
129139
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=

gpu/buffer.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import (
88
"fmt"
99

1010
"cogentcore.org/core/base/errors"
11-
"github.com/cogentcore/webgpu/wgpu"
11+
"github.com/oliverbestmann/webgpu/wgpu"
1212
)
1313

1414
// note: WriteBuffer is the preferred method for writing, so we only need to manage Read
1515

1616
// BufferMapAsyncError returns an error message if the status is not success.
17-
func BufferMapAsyncError(status wgpu.BufferMapAsyncStatus) error {
18-
if status != wgpu.BufferMapAsyncStatusSuccess {
17+
func BufferMapAsyncError(status wgpu.MapAsyncStatus) error {
18+
if status != wgpu.MapAsyncStatusSuccess {
1919
return errors.New("gpu BufferMapAsync was not successful")
2020
}
2121
return nil
@@ -24,8 +24,8 @@ func BufferMapAsyncError(status wgpu.BufferMapAsyncStatus) error {
2424
// BufferReadSync does a MapAsync on given buffer, waiting on the device
2525
// until the sync is complete, and returning error if any issues.
2626
func BufferReadSync(device *Device, size int, buffer *wgpu.Buffer) error {
27-
var status wgpu.BufferMapAsyncStatus
28-
err := buffer.MapAsync(wgpu.MapModeRead, 0, uint64(size), func(s wgpu.BufferMapAsyncStatus) {
27+
var status wgpu.MapAsyncStatus
28+
err := buffer.TryMapAsync(wgpu.MapModeRead, 0, uint64(size), func(s wgpu.MapAsyncStatus) {
2929
status = s
3030
})
3131
if errors.Log(err) != nil {
@@ -44,9 +44,9 @@ func ValueReadSync(device *Device, values ...*Value) error {
4444
return nil
4545
}
4646
var errs []error
47-
status := make([]wgpu.BufferMapAsyncStatus, nv)
47+
status := make([]wgpu.MapAsyncStatus, nv)
4848
for i, vl := range values {
49-
err := vl.readBuffer.MapAsync(wgpu.MapModeRead, 0, uint64(vl.AllocSize), func(s wgpu.BufferMapAsyncStatus) {
49+
err := vl.readBuffer.TryMapAsync(wgpu.MapModeRead, 0, uint64(vl.AllocSize), func(s wgpu.MapAsyncStatus) {
5050
status[i] = s
5151
})
5252
if err != nil {

gpu/cmd/webgpuinfo/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
"cogentcore.org/core/base/reflectx"
1212
"cogentcore.org/core/gpu"
13-
"github.com/cogentcore/webgpu/wgpu"
13+
"github.com/oliverbestmann/webgpu/wgpu"
1414
)
1515

1616
func main() {

gpu/compute.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"sync"
1212

1313
"cogentcore.org/core/base/errors"
14-
"github.com/cogentcore/webgpu/wgpu"
14+
"github.com/oliverbestmann/webgpu/wgpu"
1515
)
1616

1717
// ComputeSystem manages a system of ComputePipelines that all share
@@ -111,7 +111,7 @@ func (sy *ComputeSystem) Config() {
111111
// compute commands. This is automatically called by
112112
// BeginRenderPass and the result maintained in [CommandEncoder].
113113
func (sy *ComputeSystem) NewCommandEncoder() (*wgpu.CommandEncoder, error) {
114-
cmd, err := sy.device.Device.CreateCommandEncoder(nil)
114+
cmd, err := sy.device.Device.TryCreateCommandEncoder(nil)
115115
if errors.Log(err) != nil {
116116
return nil, err
117117
}
@@ -150,7 +150,7 @@ func (sy *ComputeSystem) EndComputePass() error {
150150
sy.ComputeEncoder = nil
151151
sy.CommandEncoder = nil
152152
ce.Release() // must happen before Finish
153-
cmdBuffer, err := cmd.Finish(nil)
153+
cmdBuffer, err := cmd.TryFinish(nil)
154154
if errors.Log(err) != nil {
155155
return err
156156
}

gpu/cpipeline.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
"cogentcore.org/core/base/errors"
1313
"cogentcore.org/core/base/fsx"
14-
"github.com/cogentcore/webgpu/wgpu"
14+
"github.com/oliverbestmann/webgpu/wgpu"
1515
)
1616

1717
// ComputePipeline is a compute pipeline, which runs shader code on vars data.
@@ -192,7 +192,7 @@ func (pl *ComputePipeline) Config(rebuild bool) error {
192192
defer play.Release()
193193

194194
sh := pl.EntryByType(ComputeShader)
195-
cp, err := pl.System.Device().Device.CreateComputePipeline(&wgpu.ComputePipelineDescriptor{
195+
cp, err := pl.System.Device().Device.TryCreateComputePipeline(&wgpu.ComputePipelineDescriptor{
196196
Layout: play,
197197
Compute: wgpu.ProgrammableStageDescriptor{
198198
Module: sh.Shader.module,

0 commit comments

Comments
 (0)