@@ -16,7 +16,7 @@ import {
16
16
TEXTURELOCK_WRITE ,
17
17
TEXTUREPROJECTION_NONE , TEXTUREPROJECTION_CUBE ,
18
18
TEXTURETYPE_DEFAULT , TEXTURETYPE_RGBM , TEXTURETYPE_RGBE , TEXTURETYPE_RGBP , TEXTURETYPE_SWIZZLEGGGR ,
19
- isIntegerPixelFormat , FILTER_NEAREST
19
+ isIntegerPixelFormat , FILTER_NEAREST , TEXTURELOCK_NONE , TEXTURELOCK_READ
20
20
} from './constants.js' ;
21
21
22
22
let id = 0 ;
@@ -47,6 +47,9 @@ class Texture {
47
47
/** @protected */
48
48
_lockedLevel = - 1 ;
49
49
50
+ /** @protected */
51
+ _lockedMode = TEXTURELOCK_NONE ;
52
+
50
53
/**
51
54
* A render version used to track the last time the texture properties requiring bind group
52
55
* to be updated were changed.
@@ -377,6 +380,20 @@ class Texture {
377
380
return this . mipmaps ? TextureUtils . calcMipLevelsCount ( this . width , this . height ) : 1 ;
378
381
}
379
382
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
+
380
397
/**
381
398
* The minification filter to be applied to the texture. Can be:
382
399
*
@@ -773,6 +790,19 @@ class Texture {
773
790
options . face ??= 0 ;
774
791
options . mode ??= TEXTURELOCK_WRITE ;
775
792
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 ;
776
806
this . _lockedLevel = options . level ;
777
807
778
808
const levels = this . cubemap ? this . _levels [ options . face ] : this . _levels ;
@@ -900,13 +930,16 @@ class Texture {
900
930
* Unlocks the currently locked mip level and uploads it to VRAM.
901
931
*/
902
932
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 ) ;
905
935
}
906
936
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
+ }
909
941
this . _lockedLevel = - 1 ;
942
+ this . _lockedMode = TEXTURELOCK_NONE ;
910
943
}
911
944
912
945
/**
0 commit comments