Skip to content

Commit 912a837

Browse files
authored
Don't upload texture if locked using TEXTURELOCK_READ (#6003)
* Don't upload texture if locked using TEXTURELOCK_READ * Don't expose _lockedLevel yet * Better assertions, upgrade double-unlock from log to warning
1 parent 204b64c commit 912a837

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

src/platform/graphics/constants.js

+7
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,13 @@ export const STENCILOP_DECREMENTWRAP = 6;
13241324
*/
13251325
export const STENCILOP_INVERT = 7;
13261326

1327+
/**
1328+
* The texture is not in a locked state.
1329+
*
1330+
* @type {number}
1331+
*/
1332+
export const TEXTURELOCK_NONE = 0;
1333+
13271334
/**
13281335
* Read only. Any changes to the locked mip level's pixels will not update the texture.
13291336
*

src/platform/graphics/texture.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
TEXTURELOCK_WRITE,
1717
TEXTUREPROJECTION_NONE, TEXTUREPROJECTION_CUBE,
1818
TEXTURETYPE_DEFAULT, TEXTURETYPE_RGBM, TEXTURETYPE_RGBE, TEXTURETYPE_RGBP, TEXTURETYPE_SWIZZLEGGGR,
19-
isIntegerPixelFormat, FILTER_NEAREST
19+
isIntegerPixelFormat, FILTER_NEAREST, TEXTURELOCK_NONE, TEXTURELOCK_READ
2020
} from './constants.js';
2121

2222
let id = 0;
@@ -47,6 +47,9 @@ class Texture {
4747
/** @protected */
4848
_lockedLevel = -1;
4949

50+
/** @protected */
51+
_lockedMode = TEXTURELOCK_NONE;
52+
5053
/**
5154
* A render version used to track the last time the texture properties requiring bind group
5255
* to be updated were changed.
@@ -377,6 +380,20 @@ class Texture {
377380
return this.mipmaps ? TextureUtils.calcMipLevelsCount(this.width, this.height) : 1;
378381
}
379382

383+
/**
384+
* Returns the current lock mode. One of:
385+
*
386+
* - {@link TEXTURELOCK_NONE}
387+
* - {@link TEXTURELOCK_READ}
388+
* - {@link TEXTURELOCK_WRITE}
389+
*
390+
* @ignore
391+
* @type {number}
392+
*/
393+
get lockedMode() {
394+
return this._lockedMode;
395+
}
396+
380397
/**
381398
* The minification filter to be applied to the texture. Can be:
382399
*
@@ -773,6 +790,19 @@ class Texture {
773790
options.face ??= 0;
774791
options.mode ??= TEXTURELOCK_WRITE;
775792

793+
Debug.assert(
794+
this._lockedMode === TEXTURELOCK_NONE,
795+
'The texture is already locked. Call `texture.unlock()` before attempting to lock again.',
796+
this
797+
);
798+
799+
Debug.assert(
800+
options.mode === TEXTURELOCK_READ || options.mode === TEXTURELOCK_WRITE,
801+
'Cannot lock a texture with TEXTURELOCK_NONE. To unlock a texture, call `texture.unlock()`.',
802+
this
803+
);
804+
805+
this._lockedMode = options.mode;
776806
this._lockedLevel = options.level;
777807

778808
const levels = this.cubemap ? this._levels[options.face] : this._levels;
@@ -900,13 +930,16 @@ class Texture {
900930
* Unlocks the currently locked mip level and uploads it to VRAM.
901931
*/
902932
unlock() {
903-
if (this._lockedLevel === -1) {
904-
Debug.log("pc.Texture#unlock: Attempting to unlock a texture that is not locked.", this);
933+
if (this._lockedMode === TEXTURELOCK_NONE) {
934+
Debug.warn("pc.Texture#unlock: Attempting to unlock a texture that is not locked.", this);
905935
}
906936

907-
// Upload the new pixel data
908-
this.upload();
937+
// Upload the new pixel data if locked in write mode (default)
938+
if (this._lockedMode === TEXTURELOCK_WRITE) {
939+
this.upload();
940+
}
909941
this._lockedLevel = -1;
942+
this._lockedMode = TEXTURELOCK_NONE;
910943
}
911944

912945
/**

0 commit comments

Comments
 (0)