-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbuffer.go
More file actions
313 lines (290 loc) · 9.89 KB
/
buffer.go
File metadata and controls
313 lines (290 loc) · 9.89 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//go:build !(js && wasm)
package wgpu
import (
"context"
"log/slog"
"runtime"
"sync/atomic"
"unsafe"
"github.com/gogpu/wgpu/core"
"github.com/gogpu/wgpu/hal"
)
// bufferCleanupRef holds the data needed to destroy a buffer's HAL resources
// from a runtime.AddCleanup callback. This struct must NOT reference the Buffer
// itself — runtime.AddCleanup requires the callback argument to be independent
// of the cleaned-up object to avoid preventing garbage collection.
type bufferCleanupRef struct {
label string
released *atomic.Bool
destroyQueue *core.DestroyQueue
lastSubIdx func() uint64
destroyFn func()
}
// Buffer represents a GPU buffer.
type Buffer struct {
core *core.Buffer
device *Device
cleanup runtime.Cleanup
// released is heap-allocated separately so that the cleanup ref can share
// it without holding an interior pointer into the Buffer struct. An interior
// pointer would make the Buffer reachable from the cleanup arg, preventing
// GC collection and causing the cleanup to never fire.
released *atomic.Bool
}
// Size returns the buffer size in bytes.
func (b *Buffer) Size() uint64 { return b.core.Size() }
// Usage returns the buffer's usage flags.
func (b *Buffer) Usage() BufferUsage { return b.core.Usage() }
// Label returns the buffer's debug label.
func (b *Buffer) Label() string { return b.core.Label() }
// Release destroys the buffer. The underlying HAL buffer is not freed
// immediately — destruction is deferred until the GPU completes any submission
// that may reference it. This prevents use-after-free on DX12/Vulkan when a
// buffer is released while the GPU is still reading from it (BUG-DX12-TDR).
func (b *Buffer) Release() {
if b.released == nil || !b.released.CompareAndSwap(false, true) {
return
}
// Cancel the GC cleanup — we are destroying explicitly.
b.cleanup.Stop()
if b.device == nil {
b.core.Destroy()
return
}
dq := b.device.destroyQueue()
if dq == nil {
// No DestroyQueue (legacy path or no HAL) — destroy immediately.
b.core.Destroy()
return
}
// Defer destruction until GPU completes the latest known submission.
subIdx := b.device.lastSubmissionIndex()
label := b.core.Label()
dq.Defer(subIdx, "Buffer:"+label, func() {
b.core.Destroy()
})
}
// MapState returns the current mapping state of the buffer.
//
// This is a synchronized snapshot — the state may change immediately
// after return if another goroutine calls Unmap or Destroy, but the
// value reflects the state at the moment of the call.
func (b *Buffer) MapState() MapState {
if b == nil || b.core == nil {
return MapStateUnmapped
}
return mapStateFromCore(b.core.CurrentMapState())
}
// 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. The primary usage pattern mirrors database/sql rows:
//
// if err := buf.Map(ctx, wgpu.MapModeRead, 0, size); err != nil {
// return err
// }
// defer buf.Unmap()
// rng, _ := buf.MappedRange(0, size)
// data := rng.Bytes()
//
// Map drives Device.Poll internally; callers do not need to schedule
// polling themselves. If you need non-blocking behavior use MapAsync.
func (b *Buffer) Map(ctx context.Context, mode MapMode, offset, size uint64) error {
if b == nil || b.core == nil {
return ErrReleased
}
if ctx == nil {
ctx = context.Background()
}
if err := ctx.Err(); err != nil {
return err
}
pending, err := b.MapAsync(mode, offset, size)
if err != nil {
return err
}
// Drain the device once synchronously — for backends that complete
// instantly (software, noop) and for the common case where the
// buffer has no in-flight submission this returns immediately with
// the map already resolved.
if b.device != nil {
b.device.Poll(PollPoll)
}
if done, werr := pending.Status(); done {
pending.Release()
return werr
}
// Wait with the caller's context; a PollWait runs inside Device.Poll
// concurrently with Wait so the HAL fence advances even if nobody
// else calls Submit before the context deadline.
if b.device != nil {
// Kick a PollWait on a worker goroutine so Wait's select unblocks
// as soon as the fence advances. Allocation is a single channel
// inside MapPending.Wait; the zero-alloc path is Status-driven.
go func() {
b.device.Poll(PollWait)
}()
}
werr := pending.Wait(ctx)
pending.Release()
return werr
}
// MapAsync initiates a buffer map without blocking the caller.
//
// Returns a *MapPending handle that resolves once the GPU submission
// that last wrote to the buffer completes. The caller must periodically
// drive Device.Poll(PollPoll) (or, more commonly, rely on the auto-poll
// at the tail of Queue.Submit) to let the mapping resolve.
//
// Validation errors surface synchronously — alignment, usage mismatch,
// range overflow, already-mapped state, etc. A returned MapPending is
// always valid; its Status() may resolve as failed later if the buffer
// is destroyed or the map is canceled.
func (b *Buffer) MapAsync(mode MapMode, offset, size uint64) (*MapPending, error) {
if b == nil || b.core == nil {
return nil, ErrReleased
}
cerr := b.core.BeginMap(mode.toInternal(), offset, size)
if cerr != nil {
return nil, coreErrToTyped(cerr)
}
// Register the buffer on the device's pending-map tracker so
// Device.Poll eventually calls hal.MapBuffer. Use the latest known
// submission index as the completion gate; if no Submit has happened
// yet the index is 0 and a subsequent Poll resolves immediately.
subIdx := uint64(0)
if b.device != nil {
subIdx = b.device.lastSubmissionIndex()
}
b.core.Device().RegisterPendingMap(subIdx, b.core)
return acquireMapPending(b, b.core.Waiter()), 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).
// The returned range overlaps with neither the rest of the buffer nor
// any previously-returned MappedRange that has not been Unmap'd — WebGPU
// spec §5.3.4 forbids overlapping getMappedRange calls on the same
// buffer and we enforce this synchronously.
//
// The returned slice (via MappedRange.Bytes) is invalidated by Unmap.
func (b *Buffer) MappedRange(offset, size uint64) (*MappedRange, error) {
if b == nil || b.core == nil {
return nil, ErrReleased
}
if offset%8 != 0 || size%4 != 0 {
return nil, ErrMapAlignment
}
if cerr := b.core.TryRegisterMappedRange(offset, size); cerr != nil {
return nil, coreErrToTyped(cerr)
}
base, pendingOffset, _, ok := b.core.MappingInfo()
if !ok || base == nil {
return nil, ErrMapNotMapped
}
// The HAL returned a pointer at pendingOffset; shift it so m.data
// points at the user-requested offset.
delta := offset - pendingOffset
var data unsafe.Pointer
if delta == 0 {
data = base
} else {
data = unsafe.Add(base, uintptr(delta))
}
m := acquireMappedRange()
m.buf = b
m.offset = offset
m.size = size
m.gen = b.core.Generation()
m.data = data
return m, nil
}
// Unmap releases the current mapping and invalidates all outstanding
// MappedRange handles for this buffer. Safe to call multiple times;
// a second call returns ErrMapNotMapped.
//
// Unmap also cancels a Pending map (the associated MapPending resolves
// with ErrMapCancelled).
func (b *Buffer) Unmap() error {
if b == nil || b.core == nil {
return ErrReleased
}
if b.device == nil {
return ErrReleased
}
halDev := b.device.halDevice()
// We do not require halDev != nil — Unmap on a device-destroyed
// buffer still clears the state machine; the HAL call is skipped.
sl := b.core.Device().SnatchLock()
if sl == nil {
return ErrReleased
}
guard := sl.Read()
defer guard.Release()
if cerr := b.core.UnmapBuffer(guard, halDev); cerr != nil {
return coreErrToTyped(cerr)
}
return nil
}
// coreBuffer returns the underlying core.Buffer.
func (b *Buffer) coreBuffer() *core.Buffer { return b.core }
// halBuffer returns the underlying HAL buffer.
func (b *Buffer) halBuffer() hal.Buffer {
if b.core == nil || b.device == nil {
return nil
}
if !b.core.HasHAL() {
return nil
}
guard := b.device.core.SnatchLock().Read()
defer guard.Release()
return b.core.Raw(guard)
}
// registerBufferCleanup registers a runtime.AddCleanup handler on the buffer.
// When GC collects the buffer without an explicit Release(), the cleanup
// schedules deferred destruction via DestroyQueue — the same path as Release().
//
// The cleanup ref struct captures only the label, released flag, DestroyQueue,
// and core destroy function — NOT the Buffer pointer itself. This is a Go 1.24
// runtime.AddCleanup requirement: the callback argument must not reference the
// object being cleaned up.
func registerBufferCleanup(buf *Buffer, dev *Device, coreBuf *core.Buffer, label string) runtime.Cleanup {
dq := dev.destroyQueue()
if dq == nil {
// No DestroyQueue — register cleanup that destroys immediately.
return runtime.AddCleanup(buf, func(ref bufferCleanupRef) {
if !ref.released.CompareAndSwap(false, true) {
return
}
slog.Warn("wgpu: Buffer released by GC (missing explicit Release)", "label", ref.label)
ref.destroyFn()
}, bufferCleanupRef{
label: label,
released: buf.released,
destroyFn: func() {
coreBuf.Destroy()
},
})
}
return runtime.AddCleanup(buf, func(ref bufferCleanupRef) {
if !ref.released.CompareAndSwap(false, true) {
return
}
slog.Warn("wgpu: Buffer released by GC (missing explicit Release)", "label", ref.label)
subIdx := ref.lastSubIdx()
ref.destroyQueue.Defer(subIdx, "Buffer(GC):"+ref.label, ref.destroyFn)
}, bufferCleanupRef{
label: label,
released: buf.released,
destroyQueue: dq,
lastSubIdx: dev.lastSubmissionIndex,
destroyFn: func() {
coreBuf.Destroy()
},
})
}