@@ -15,7 +15,11 @@ import {
15
15
TEXTUREPROJECTION_NONE , TEXTUREPROJECTION_CUBE ,
16
16
TEXTURETYPE_DEFAULT , TEXTURETYPE_RGBM , TEXTURETYPE_RGBE , TEXTURETYPE_RGBP ,
17
17
isIntegerPixelFormat , FILTER_NEAREST , TEXTURELOCK_NONE , TEXTURELOCK_READ ,
18
- isLinearFormat
18
+ isLinearFormat ,
19
+ TEXTUREDIMENSION_2D ,
20
+ TEXTUREDIMENSION_3D ,
21
+ TEXTUREDIMENSION_2D_ARRAY ,
22
+ TEXTUREDIMENSION_CUBE
19
23
} from './constants.js' ;
20
24
21
25
let id = 0 ;
@@ -101,7 +105,15 @@ class Texture {
101
105
* @param {string } [options.name] - The name of the texture. Defaults to null.
102
106
* @param {number } [options.width] - The width of the texture in pixels. Defaults to 4.
103
107
* @param {number } [options.height] - The height of the texture in pixels. Defaults to 4.
104
- * @param {number } [options.depth] - The number of depth slices in a 3D texture.
108
+ * @param {number } [options.slices] - The number of depth slices in a 3D texture, the number of textures
109
+ * in a texture array or the number of faces for a cubemap.
110
+ * @param {string } [options.dimension] - The texture dimension type. Can be:
111
+ * - {@link TEXTUREDIMENSION_2D}
112
+ * - {@link TEXTUREDIMENSION_2D_ARRAY}
113
+ * - {@link TEXTUREDIMENSION_3D}
114
+ * - {@link TEXTUREDIMENSION_CUBE}
115
+ * Defaults to {@link TEXTUREDIMENSION_2D}. Alternatively, you can specify the dimension using
116
+ * the options.cubemap, options.volume or options.array properties.
105
117
* @param {number } [options.format] - The pixel format of the texture. Can be:
106
118
*
107
119
* - {@link PIXELFORMAT_R8}
@@ -155,9 +167,8 @@ class Texture {
155
167
* texture. Default is true.
156
168
* @param {boolean } [options.cubemap] - Specifies whether the texture is to be a cubemap.
157
169
* Defaults to false.
158
- * @param {number } [options.arrayLength] - Specifies whether the texture is to be a 2D texture array.
159
- * When passed in as undefined or < 1, this is not an array texture. If >= 1, this is an array texture.
160
- * Defaults to undefined.
170
+ * @param {boolean } [options.array] - Specifies whether the texture is to be a 2D texture array.
171
+ * Defaults to false.
161
172
* @param {boolean } [options.volume] - Specifies whether the texture is to be a 3D volume.
162
173
* Defaults to false.
163
174
* @param {string } [options.type] - Specifies the texture type. Can be:
@@ -192,7 +203,7 @@ class Texture {
192
203
* Defaults to {@link FUNC_LESS}.
193
204
* @param {Uint8Array[]|HTMLCanvasElement[]|HTMLImageElement[]|HTMLVideoElement[]|Uint8Array[][] } [options.levels]
194
205
* - Array of Uint8Array or other supported browser interface; or a two-dimensional array
195
- * of Uint8Array if options.arrayLength is defined and greater than zero .
206
+ * of Uint8Array if options.dimension is { @link TEXTUREDIMENSION_2D_ARRAY} .
196
207
* @param {boolean } [options.storage] - Defines if texture can be used as a storage texture by
197
208
* a compute shader. Defaults to false.
198
209
* @param {boolean } [options.immediate] - If set and true, the texture will be uploaded to the GPU immediately.
@@ -221,13 +232,22 @@ class Texture {
221
232
Debug . assert ( this . device , "Texture constructor requires a graphicsDevice to be valid" ) ;
222
233
Debug . assert ( ! options . width || Number . isInteger ( options . width ) , "Texture width must be an integer number, got" , options ) ;
223
234
Debug . assert ( ! options . height || Number . isInteger ( options . height ) , "Texture height must be an integer number, got" , options ) ;
224
- Debug . assert ( ! options . depth || Number . isInteger ( options . depth ) , "Texture depth must be an integer number, got" , options ) ;
235
+ Debug . assert ( ! options . slices || Number . isInteger ( options . slices ) , "Texture slices must be an integer number, got" , options ) ;
225
236
226
237
this . name = options . name ?? '' ;
227
238
239
+ this . _dimension = options . dimension ?? TEXTUREDIMENSION_2D ;
240
+ this . _dimension = options . array ? TEXTUREDIMENSION_2D_ARRAY : this . _dimension ;
241
+ this . _dimension = options . cubemap ? TEXTUREDIMENSION_CUBE : this . _dimension ;
242
+ this . _dimension = options . volume ? TEXTUREDIMENSION_3D : this . _dimension ;
243
+
228
244
this . _width = Math . floor ( options . width ?? 4 ) ;
229
245
this . _height = Math . floor ( options . height ?? 4 ) ;
230
246
247
+ this . _slices = Math . floor ( options . slices ?? ( this . _dimension === TEXTUREDIMENSION_CUBE ? 6 : 1 ) ) ;
248
+
249
+ Debug . assert ( ( this . _dimension === TEXTUREDIMENSION_CUBE ? this . _slices === 6 : true ) , "Texture cube map must have 6 slices" ) ;
250
+
231
251
this . _format = options . format ?? PIXELFORMAT_RGBA8 ;
232
252
this . _compressed = isCompressedPixelFormat ( this . _format ) ;
233
253
this . _integerFormat = isIntegerPixelFormat ( this . _format ) ;
@@ -237,12 +257,7 @@ class Texture {
237
257
options . magFilter = FILTER_NEAREST ;
238
258
}
239
259
240
- this . _volume = options . volume ?? false ;
241
- this . _depth = Math . floor ( options . depth ?? 1 ) ;
242
- this . _arrayLength = Math . floor ( options . arrayLength ?? 0 ) ;
243
-
244
260
this . _storage = options . storage ?? false ;
245
- this . _cubemap = options . cubemap ?? false ;
246
261
this . _flipY = options . flipY ?? false ;
247
262
this . _premultiplyAlpha = options . premultiplyAlpha ?? false ;
248
263
@@ -262,7 +277,7 @@ class Texture {
262
277
Debug . assert ( ! options . hasOwnProperty ( 'swizzleGGGR' ) , 'Use options.type.' ) ;
263
278
264
279
this . projection = TEXTUREPROJECTION_NONE ;
265
- if ( this . _cubemap ) {
280
+ if ( this . cubemap ) {
266
281
this . projection = TEXTUREPROJECTION_CUBE ;
267
282
} else if ( options . projection && options . projection !== TEXTUREPROJECTION_CUBE ) {
268
283
this . projection = options . projection ;
@@ -280,7 +295,7 @@ class Texture {
280
295
if ( this . _levels ) {
281
296
this . upload ( options . immediate ?? false ) ;
282
297
} else {
283
- this . _levels = this . _cubemap ? [ [ null , null , null , null , null , null ] ] : [ null ] ;
298
+ this . _levels = this . cubemap ? [ [ null , null , null , null , null , null ] ] : [ null ] ;
284
299
}
285
300
286
301
// track the texture
@@ -328,10 +343,10 @@ class Texture {
328
343
*
329
344
* @param {number } width - The new width of the texture.
330
345
* @param {number } height - The new height of the texture.
331
- * @param {number } [depth ] - The new depth of the texture. Defaults to 1.
346
+ * @param {number } [slices ] - The new number of slices for the texture. Defaults to 1.
332
347
* @ignore
333
348
*/
334
- resize ( width , height , depth = 1 ) {
349
+ resize ( width , height , slices = 1 ) {
335
350
336
351
// destroy texture impl
337
352
const device = this . device ;
@@ -340,7 +355,7 @@ class Texture {
340
355
341
356
this . _width = Math . floor ( width ) ;
342
357
this . _height = Math . floor ( height ) ;
343
- this . _depth = Math . floor ( depth ) ;
358
+ this . _slices = Math . floor ( slices ) ;
344
359
345
360
// re-create the implementation
346
361
this . impl = device . createTextureImpl ( this ) ;
@@ -528,7 +543,7 @@ class Texture {
528
543
* @type {number }
529
544
*/
530
545
set addressW ( addressW ) {
531
- if ( ! this . _volume ) {
546
+ if ( ! this . volume ) {
532
547
Debug . warn ( "pc.Texture#addressW: Can't set W addressing mode for a non-3D texture." ) ;
533
548
return ;
534
549
}
@@ -682,7 +697,16 @@ class Texture {
682
697
* @type {number }
683
698
*/
684
699
get depth ( ) {
685
- return this . _depth ;
700
+ return this . _dimension === TEXTUREDIMENSION_3D ? this . _slices : 1 ;
701
+ }
702
+
703
+ /**
704
+ * The number of textures in a texture array or the number of faces for a cubemap.
705
+ *
706
+ * @type {number }
707
+ */
708
+ get slices ( ) {
709
+ return this . _slices ;
686
710
}
687
711
688
712
/**
@@ -724,12 +748,12 @@ class Texture {
724
748
* @type {boolean }
725
749
*/
726
750
get cubemap ( ) {
727
- return this . _cubemap ;
751
+ return this . _dimension === TEXTUREDIMENSION_CUBE ;
728
752
}
729
753
730
754
get gpuSize ( ) {
731
755
const mips = this . pot && this . _mipmaps && ! ( this . _compressed && this . _levels . length === 1 ) ;
732
- return TextureUtils . calcGpuSize ( this . _width , this . _height , this . _depth , this . _format , mips , this . _cubemap ) ;
756
+ return TextureUtils . calcGpuSize ( this . _width , this . _height , this . _slices , this . _format , this . volume , mips ) ;
733
757
}
734
758
735
759
/**
@@ -738,16 +762,7 @@ class Texture {
738
762
* @type {boolean }
739
763
*/
740
764
get array ( ) {
741
- return this . _arrayLength > 0 ;
742
- }
743
-
744
- /**
745
- * Returns the number of textures inside this texture if this is a 2D array texture or 0 otherwise.
746
- *
747
- * @type {number }
748
- */
749
- get arrayLength ( ) {
750
- return this . _arrayLength ;
765
+ return this . _dimension === TEXTUREDIMENSION_2D_ARRAY ;
751
766
}
752
767
753
768
/**
@@ -756,7 +771,7 @@ class Texture {
756
771
* @type {boolean }
757
772
*/
758
773
get volume ( ) {
759
- return this . _volume ;
774
+ return this . _dimension === TEXTUREDIMENSION_3D ;
760
775
}
761
776
762
777
/**
@@ -818,7 +833,7 @@ class Texture {
818
833
819
834
// Force a full resubmission of the texture to the GPU (used on a context restore event)
820
835
dirtyAll ( ) {
821
- this . _levelsUpdated = this . _cubemap ? [ [ true , true , true , true , true , true ] ] : [ true ] ;
836
+ this . _levelsUpdated = this . cubemap ? [ [ true , true , true , true , true , true ] ] : [ true ] ;
822
837
823
838
this . _needsUpload = true ;
824
839
this . _needsMipmapsUpload = this . _mipmaps ;
@@ -868,7 +883,7 @@ class Texture {
868
883
// allocate storage for this mip level
869
884
const width = Math . max ( 1 , this . _width >> options . level ) ;
870
885
const height = Math . max ( 1 , this . _height >> options . level ) ;
871
- const depth = Math . max ( 1 , this . _depth >> options . level ) ;
886
+ const depth = Math . max ( 1 , ( this . _dimension === TEXTUREDIMENSION_3D ? this . _slices : 1 ) >> options . level ) ;
872
887
const data = new ArrayBuffer ( TextureUtils . calcLevelGpuSize ( width , height , depth , this . _format ) ) ;
873
888
levels [ options . level ] = new ( getPixelFormatArrayType ( this . _format ) ) ( data ) ;
874
889
}
@@ -891,7 +906,7 @@ class Texture {
891
906
let invalid = false ;
892
907
let width , height ;
893
908
894
- if ( this . _cubemap ) {
909
+ if ( this . cubemap ) {
895
910
if ( source [ 0 ] ) {
896
911
// rely on first face sizes
897
912
width = source [ 0 ] . width || 0 ;
@@ -943,7 +958,7 @@ class Texture {
943
958
this . _height = 4 ;
944
959
945
960
// remove levels
946
- if ( this . _cubemap ) {
961
+ if ( this . cubemap ) {
947
962
for ( let i = 0 ; i < 6 ; i ++ ) {
948
963
this . _levels [ mipLevel ] [ i ] = null ;
949
964
this . _levelsUpdated [ mipLevel ] [ i ] = true ;
0 commit comments