Skip to content

Commit 7f5b384

Browse files
committed
record and release old wgpu BindGroup items, which were otherwise not being released. thanks to @AnyCPU for identifying leak and potential fix, and @StinkyPeach for reporting the issue.
1 parent 0c93812 commit 7f5b384

File tree

7 files changed

+41
-4
lines changed

7 files changed

+41
-4
lines changed

gpu/compute.go

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func (sy *ComputeSystem) EndComputePass() error {
157157
sy.device.Queue.Submit(cmdBuffer)
158158
cmdBuffer.Release()
159159
cmd.Release()
160+
sy.vars.releaseOldBindGroups()
160161
return nil
161162
}
162163

gpu/gpudraw/draw.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (dw *Drawer) Copy(dp image.Point, src image.Image, sr image.Rectangle, op d
3939
dr := sr
4040
del := dp.Sub(sr.Min)
4141
dr.Min = dp
42-
dr.Max.Add(del)
42+
dr.Max = dr.Max.Add(del)
4343
dw.Fill(u.At(0, 0), dr, op)
4444
return
4545
}

gpu/gsystem.go

+1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ func (sy *GraphicsSystem) SubmitRender(rp *wgpu.RenderPassEncoder) error {
269269
sy.device.Queue.Submit(cmdBuffer)
270270
cmdBuffer.Release()
271271
cmd.Release()
272+
sy.vars.releaseOldBindGroups()
272273
return nil
273274
}
274275

gpu/pipeline.go

+9
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,12 @@ func (pl *Pipeline) bindLayout() (*wgpu.PipelineLayout, error) {
131131
}
132132
return rpl, nil
133133
}
134+
135+
func (pl *Pipeline) releaseOldBindGroups() {
136+
vs := pl.Vars()
137+
ngp := vs.NGroups()
138+
for gi := range ngp {
139+
vg := vs.Groups[gi]
140+
vg.releaseOldBindGroups()
141+
}
142+
}

gpu/value.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func (vl *Value) SetFromBytes(from []byte) error {
279279
return errors.Log(err)
280280
}
281281
if vl.buffer == nil || vl.AllocSize != tb {
282-
vl.varGroupDirty()
282+
vl.varGroupDirty() // only if tb is different; buffer created in bindGroupEntry so not nil here
283283
vl.Release()
284284
buf, err := vl.device.Device.CreateBufferInit(&wgpu.BufferInitDescriptor{
285285
Label: vl.Name,
@@ -355,7 +355,7 @@ func (vl *Value) WriteDynamicBuffer() error {
355355
return errors.Log(err)
356356
}
357357
if vl.buffer == nil || nb != vl.AllocSize {
358-
vl.varGroupDirty()
358+
vl.varGroupDirty() // only if nb is different; buffer created in bindGroupEntry so not nil here
359359
vl.Release()
360360
buf, err := vl.device.Device.CreateBufferInit(&wgpu.BufferInitDescriptor{
361361
Label: vl.Name,

gpu/vargroup.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ type VarGroup struct {
6666

6767
// current bind group
6868
currentBindGroup *wgpu.BindGroup
69+
70+
// oldBindGroups are prior bind groups that need to be released
71+
// after current render or compute pass.
72+
oldBindGroups []*wgpu.BindGroup
6973
}
7074

7175
// addVar adds given variable
@@ -223,8 +227,21 @@ func (vg *VarGroup) Config(dev *Device) error {
223227
return errors.Join(errs...)
224228
}
225229

230+
// releaseOldBindGroups releases old bind groups.
231+
func (vg *VarGroup) releaseOldBindGroups() {
232+
if vg.oldBindGroups == nil {
233+
return
234+
}
235+
og := vg.oldBindGroups
236+
vg.oldBindGroups = nil
237+
for _, bg := range og {
238+
bg.Release()
239+
}
240+
}
241+
226242
// Release destroys infrastructure for Group, Vars and Values.
227243
func (vg *VarGroup) Release() {
244+
vg.releaseOldBindGroups()
228245
for _, vr := range vg.Vars {
229246
vr.Release()
230247
}
@@ -318,7 +335,10 @@ func (vg *VarGroup) bindGroup(vs *Vars) (*wgpu.BindGroup, []uint32, error) {
318335
return vg.currentBindGroup, dynamicOffsets, nil
319336
}
320337
vg.bindGroupDirty = false
321-
vg.currentBindGroup = nil // critically, we do NOT release this!
338+
if vg.currentBindGroup != nil {
339+
vg.oldBindGroups = append(vg.oldBindGroups, vg.currentBindGroup) // to be released
340+
}
341+
vg.currentBindGroup = nil
322342
bgl, err := vg.bindLayout(vs)
323343
if err != nil {
324344
return nil, nil, err

gpu/vars.go

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ func (vs *Vars) Release() {
4848
}
4949
}
5050

51+
func (vs *Vars) releaseOldBindGroups() {
52+
for _, vg := range vs.Groups {
53+
vg.releaseOldBindGroups()
54+
}
55+
}
56+
5157
// AddVertexGroup adds a new Vertex Group.
5258
// This is a special Group holding Vertex, Index vars
5359
func (vs *Vars) AddVertexGroup() *VarGroup {

0 commit comments

Comments
 (0)