Skip to content

Commit 69c2bf5

Browse files
mvaligurskyMartin Valigursky
andauthored
[Fix] UploadStream functionality handles lost context (playcanvas#8160)
Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
1 parent c367ea7 commit 69c2bf5

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

src/platform/graphics/upload-stream.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @import { GraphicsDevice } from './graphics-device.js'
33
* @import { StorageBuffer } from './storage-buffer.js'
44
* @import { Texture } from './texture.js'
5+
* @import { EventHandle } from '../../core/event-handle.js'
56
*/
67

78
/**
@@ -15,6 +16,14 @@
1516
* @ignore
1617
*/
1718
class UploadStream {
19+
/**
20+
* Event handle for device lost event.
21+
*
22+
* @type {EventHandle|null}
23+
* @protected
24+
*/
25+
_deviceLostEvent = null;
26+
1827
/**
1928
* Create a new UploadStream instance.
2029
*
@@ -29,6 +38,21 @@ class UploadStream {
2938

3039
// Create platform-specific implementation
3140
this.impl = device.createUploadStreamImpl(this);
41+
42+
// Register device lost handler
43+
this._deviceLostEvent = this.device.on('devicelost', this._onDeviceLost, this);
44+
}
45+
46+
/**
47+
* Destroy the upload stream and clean up all pooled resources.
48+
*/
49+
destroy() {
50+
// Remove event listener
51+
this._deviceLostEvent?.off();
52+
this._deviceLostEvent = null;
53+
54+
this.impl?.destroy();
55+
this.impl = null;
3256
}
3357

3458
/**
@@ -53,11 +77,12 @@ class UploadStream {
5377
}
5478

5579
/**
56-
* Destroy the upload stream and clean up all pooled resources.
80+
* Handles device lost event. Override in platform implementations.
81+
*
82+
* @private
5783
*/
58-
destroy() {
59-
this.impl?.destroy();
60-
this.impl = null;
84+
_onDeviceLost() {
85+
this.impl?._onDeviceLost?.();
6186
}
6287
}
6388

src/platform/graphics/webgl/webgl-upload-stream.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ class WebglUploadStream {
4444
});
4545
}
4646

47+
/**
48+
* Handles device lost event by clearing all PBO and sync object arrays.
49+
*
50+
* @protected
51+
*/
52+
_onDeviceLost() {
53+
// Clear arrays without trying to delete objects (context is already lost)
54+
this.availablePBOs.length = 0;
55+
this.pendingPBOs.length = 0;
56+
}
57+
4758
/**
4859
* Update PBOs: poll completed ones and remove undersized buffers.
4960
*

src/platform/graphics/webgpu/webgpu-upload-stream.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ class WebgpuUploadStream {
3939
this.useSingleBuffer = uploadStream.useSingleBuffer;
4040
}
4141

42+
/**
43+
* Handles device lost event.
44+
* TODO: Implement proper WebGPU device lost handling if needed.
45+
*
46+
* @protected
47+
*/
48+
_onDeviceLost() {
49+
// WebGPU device lost handling not yet implemented
50+
}
51+
4252
destroy() {
4353
this._destroyed = true;
4454
this.availableStagingBuffers.forEach(buffer => buffer.destroy());

0 commit comments

Comments
 (0)