-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbuffer_browser.go
More file actions
241 lines (215 loc) · 5.91 KB
/
buffer_browser.go
File metadata and controls
241 lines (215 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//go:build js && wasm
package wgpu
import (
"context"
"fmt"
"github.com/gogpu/wgpu/internal/browser"
)
// Buffer represents a GPU buffer.
type Buffer struct {
browser *browser.Buffer
size uint64
usage BufferUsage
released bool
// mapState tracks the current mapping state. Updated by MapAsync/Unmap.
mapState MapState
}
// Size returns the buffer size in bytes.
func (b *Buffer) Size() uint64 {
return b.size
}
// Usage returns the buffer's usage flags.
func (b *Buffer) Usage() BufferUsage {
return b.usage
}
// Label returns the buffer's debug label.
// Browser WebGPU does not expose label on the object; returns empty string.
func (b *Buffer) Label() string {
return ""
}
// Release destroys the buffer.
func (b *Buffer) Release() {
if b.released {
return
}
b.released = true
b.mapState = MapStateDestroyed
if b.browser != nil {
b.browser.Destroy()
}
}
// MapState returns the current mapping state of the buffer.
func (b *Buffer) MapState() MapState {
if b == nil || b.released {
return MapStateUnmapped
}
return b.mapState
}
// mapModeToJS converts a MapMode to the WebGPU GPUMapMode flags.
// Our MapMode values already match WebGPU spec: MapModeRead=1, MapModeWrite=2.
// This function validates and converts explicitly for clarity.
func mapModeToJS(mode MapMode) uint32 {
var jsMode uint32
if mode&MapModeRead != 0 {
jsMode |= 1 // GPUMapMode.READ
}
if mode&MapModeWrite != 0 {
jsMode |= 2 // GPUMapMode.WRITE
}
return jsMode
}
// Map blocks until a CPU-visible mapping is established for the given
// byte range, or until ctx is canceled.
//
// The buffer must have been created with BufferUsageMapRead or
// BufferUsageMapWrite matching mode. offset must be a multiple of 8 and
// size must be a multiple of 4 (WebGPU MAP_ALIGNMENT).
//
// After Map succeeds, call MappedRange to obtain a byte view and Unmap
// when finished:
//
// if err := buf.Map(ctx, wgpu.MapModeRead, 0, size); err != nil {
// return err
// }
// defer buf.Unmap()
// rng, _ := buf.MappedRange(0, size)
// data := rng.Bytes()
//
// On the browser, Map spawns a goroutine that calls GPUBuffer.mapAsync
// (a JS Promise) via AwaitPromise. The ctx parameter allows cancellation.
func (b *Buffer) Map(ctx context.Context, mode MapMode, offset, size uint64) error {
if b == nil || b.browser == nil {
return ErrReleased
}
if b.released {
return ErrBufferDestroyed
}
if ctx == nil {
ctx = context.Background()
}
if err := ctx.Err(); err != nil {
return err
}
if err := validateMapArgs(b, mode, offset, size); err != nil {
return err
}
// MapAsync sets the state to pending, then blocks on the JS Promise.
// We use a channel + goroutine to support context cancellation.
pending, err := b.MapAsync(mode, offset, size)
if err != nil {
return err
}
return pending.Wait(ctx)
}
// MapAsync initiates a buffer map without blocking the caller.
//
// Returns a *MapPending handle that resolves once the browser's
// GPUBuffer.mapAsync Promise settles. The caller can poll with
// Status() or block with Wait(ctx).
//
// Validation errors (alignment, usage mismatch, range overflow,
// already-mapped state) surface synchronously.
func (b *Buffer) MapAsync(mode MapMode, offset, size uint64) (*MapPending, error) {
if b == nil || b.browser == nil {
return nil, ErrReleased
}
if b.released {
return nil, ErrBufferDestroyed
}
if err := validateMapArgs(b, mode, offset, size); err != nil {
return nil, err
}
b.mapState = MapStatePending
jsMode := mapModeToJS(mode)
// Create a MapPending that will resolve asynchronously.
p := &MapPending{
buf: b,
done: make(chan error, 1),
}
// Launch a goroutine to await the JS Promise. AwaitPromise blocks the
// goroutine (not the main thread) until the Promise settles.
go func() {
err := b.browser.MapAsync(jsMode, offset, size)
if err != nil {
b.mapState = MapStateUnmapped
p.done <- fmt.Errorf("mapAsync: %w", err)
} else {
b.mapState = MapStateMapped
p.done <- nil
}
}()
return p, nil
}
// MappedRange returns a safe view over the mapped region [offset, offset+size).
//
// The buffer must be in the Mapped state (Map or MapAsync resolved, or the
// buffer was created with MappedAtCreation: true). The returned MappedRange
// is invalidated by Unmap.
func (b *Buffer) MappedRange(offset, size uint64) (*MappedRange, error) {
if b == nil || b.browser == nil {
return nil, ErrReleased
}
if b.released {
return nil, ErrBufferDestroyed
}
if b.mapState != MapStateMapped {
return nil, ErrMapNotMapped
}
if offset%8 != 0 || size%4 != 0 {
return nil, ErrMapAlignment
}
if offset+size > b.size {
return nil, ErrMapRangeOverflow
}
return &MappedRange{
buf: b,
offset: offset,
size: size,
valid: true,
}, nil
}
// Unmap releases the current mapping and invalidates all outstanding
// MappedRange handles. Safe to call multiple times; a second call returns
// ErrMapNotMapped.
func (b *Buffer) Unmap() error {
if b == nil || b.browser == nil {
return ErrReleased
}
if b.released {
return ErrBufferDestroyed
}
if b.mapState != MapStateMapped && b.mapState != MapStatePending {
return ErrMapNotMapped
}
b.browser.Unmap()
b.mapState = MapStateUnmapped
return nil
}
// validateMapArgs performs synchronous validation of mapping parameters.
func validateMapArgs(b *Buffer, mode MapMode, offset, size uint64) error {
if b.mapState == MapStatePending {
return ErrMapAlreadyPending
}
if b.mapState == MapStateMapped {
return ErrMapAlreadyMapped
}
if b.mapState == MapStateDestroyed {
return ErrBufferDestroyed
}
// Check usage flags.
if mode&MapModeRead != 0 && b.usage&BufferUsageMapRead == 0 {
return ErrMapInvalidMode
}
if mode&MapModeWrite != 0 && b.usage&BufferUsageMapWrite == 0 {
return ErrMapInvalidMode
}
// Alignment: offset must be multiple of 8, size must be multiple of 4.
if offset%8 != 0 || size%4 != 0 {
return ErrMapAlignment
}
// Range overflow.
if offset+size > b.size {
return ErrMapRangeOverflow
}
return nil
}