@@ -13,7 +13,11 @@ import {
1313 TEXTUREPROJECTION_NONE , TEXTUREPROJECTION_CUBE ,
1414 TEXTURETYPE_DEFAULT , TEXTURETYPE_RGBM , TEXTURETYPE_RGBE , TEXTURETYPE_RGBP ,
1515 isIntegerPixelFormat , FILTER_NEAREST , TEXTURELOCK_NONE , TEXTURELOCK_READ ,
16- requiresManualGamma
16+ requiresManualGamma ,
17+ TEXTUREDIMENSION_2D ,
18+ TEXTUREDIMENSION_3D ,
19+ TEXTUREDIMENSION_2D_ARRAY ,
20+ TEXTUREDIMENSION_CUBE
1721} from './constants.js' ;
1822import { TextureUtils } from './texture-utils.js' ;
1923
@@ -103,7 +107,15 @@ class Texture {
103107 * @param {string } [options.name] - The name of the texture. Defaults to null.
104108 * @param {number } [options.width] - The width of the texture in pixels. Defaults to 4.
105109 * @param {number } [options.height] - The height of the texture in pixels. Defaults to 4.
106- * @param {number } [options.depth] - The number of depth slices in a 3D texture.
110+ * @param {number } [options.slices] - The number of depth slices in a 3D texture, the number of textures
111+ * in a texture array or the number of faces for a cubemap.
112+ * @param {string } [options.dimension] - The texture dimension type. Can be:
113+ * - {@link TEXTUREDIMENSION_2D}
114+ * - {@link TEXTUREDIMENSION_2D_ARRAY}
115+ * - {@link TEXTUREDIMENSION_3D}
116+ * - {@link TEXTUREDIMENSION_CUBE}
117+ * Defaults to {@link TEXTUREDIMENSION_2D}. Alternatively, you can specify the dimension using
118+ * the options.cubemap, options.volume or options.array properties.
107119 * @param {number } [options.format] - The pixel format of the texture. Can be:
108120 *
109121 * - {@link PIXELFORMAT_R8}
@@ -157,9 +169,8 @@ class Texture {
157169 * texture. Default is true.
158170 * @param {boolean } [options.cubemap] - Specifies whether the texture is to be a cubemap.
159171 * Defaults to false.
160- * @param {number } [options.arrayLength] - Specifies whether the texture is to be a 2D texture array.
161- * When passed in as undefined or < 1, this is not an array texture. If >= 1, this is an array texture.
162- * Defaults to undefined.
172+ * @param {boolean } [options.array] - Specifies whether the texture is to be a 2D texture array.
173+ * Defaults to false.
163174 * @param {boolean } [options.volume] - Specifies whether the texture is to be a 3D volume.
164175 * Defaults to false.
165176 * @param {string } [options.type] - Specifies the texture type. Can be:
@@ -194,7 +205,7 @@ class Texture {
194205 * Defaults to {@link FUNC_LESS}.
195206 * @param {Uint8Array[]|HTMLCanvasElement[]|HTMLImageElement[]|HTMLVideoElement[]|Uint8Array[][] } [options.levels]
196207 * - Array of Uint8Array or other supported browser interface; or a two-dimensional array
197- * of Uint8Array if options.arrayLength is defined and greater than zero .
208+ * of Uint8Array if options.dimension is { @link TEXTUREDIMENSION_2D_ARRAY} .
198209 * @param {boolean } [options.storage] - Defines if texture can be used as a storage texture by
199210 * a compute shader. Defaults to false.
200211 * @param {boolean } [options.immediate] - If set and true, the texture will be uploaded to the GPU immediately.
@@ -223,13 +234,22 @@ class Texture {
223234 Debug . assert ( this . device , "Texture constructor requires a graphicsDevice to be valid" ) ;
224235 Debug . assert ( ! options . width || Number . isInteger ( options . width ) , "Texture width must be an integer number, got" , options ) ;
225236 Debug . assert ( ! options . height || Number . isInteger ( options . height ) , "Texture height must be an integer number, got" , options ) ;
226- Debug . assert ( ! options . depth || Number . isInteger ( options . depth ) , "Texture depth must be an integer number, got" , options ) ;
237+ Debug . assert ( ! options . slices || Number . isInteger ( options . slices ) , "Texture slices must be an integer number, got" , options ) ;
227238
228239 this . name = options . name ?? '' ;
229240
241+ this . _dimension = options . dimension ?? TEXTUREDIMENSION_2D ;
242+ this . _dimension = options . array ? TEXTUREDIMENSION_2D_ARRAY : this . _dimension ;
243+ this . _dimension = options . cubemap ? TEXTUREDIMENSION_CUBE : this . _dimension ;
244+ this . _dimension = options . volume ? TEXTUREDIMENSION_3D : this . _dimension ;
245+
230246 this . _width = Math . floor ( options . width ?? 4 ) ;
231247 this . _height = Math . floor ( options . height ?? 4 ) ;
232248
249+ this . _slices = Math . floor ( options . slices ?? ( this . _dimension === TEXTUREDIMENSION_CUBE ? 6 : 1 ) ) ;
250+
251+ Debug . assert ( ( this . _dimension === TEXTUREDIMENSION_CUBE ? this . _slices === 6 : true ) , "Texture cube map must have 6 slices" ) ;
252+
233253 this . _format = options . format ?? PIXELFORMAT_RGBA8 ;
234254 this . _compressed = isCompressedPixelFormat ( this . _format ) ;
235255 this . _integerFormat = isIntegerPixelFormat ( this . _format ) ;
@@ -239,12 +259,7 @@ class Texture {
239259 options . magFilter = FILTER_NEAREST ;
240260 }
241261
242- this . _volume = options . volume ?? false ;
243- this . _depth = Math . floor ( options . depth ?? 1 ) ;
244- this . _arrayLength = Math . floor ( options . arrayLength ?? 0 ) ;
245-
246262 this . _storage = options . storage ?? false ;
247- this . _cubemap = options . cubemap ?? false ;
248263 this . _flipY = options . flipY ?? false ;
249264 this . _premultiplyAlpha = options . premultiplyAlpha ?? false ;
250265
@@ -264,7 +279,7 @@ class Texture {
264279 Debug . assert ( ! options . hasOwnProperty ( 'swizzleGGGR' ) , 'Use options.type.' ) ;
265280
266281 this . projection = TEXTUREPROJECTION_NONE ;
267- if ( this . _cubemap ) {
282+ if ( this . cubemap ) {
268283 this . projection = TEXTUREPROJECTION_CUBE ;
269284 } else if ( options . projection && options . projection !== TEXTUREPROJECTION_CUBE ) {
270285 this . projection = options . projection ;
@@ -282,7 +297,7 @@ class Texture {
282297 if ( this . _levels ) {
283298 this . upload ( options . immediate ?? false ) ;
284299 } else {
285- this . _levels = this . _cubemap ? [ [ null , null , null , null , null , null ] ] : [ null ] ;
300+ this . _levels = this . cubemap ? [ [ null , null , null , null , null , null ] ] : [ null ] ;
286301 }
287302
288303 // track the texture
@@ -330,10 +345,10 @@ class Texture {
330345 *
331346 * @param {number } width - The new width of the texture.
332347 * @param {number } height - The new height of the texture.
333- * @param {number } [depth ] - The new depth of the texture. Defaults to 1.
348+ * @param {number } [slices ] - The new number of slices for the texture. Defaults to 1.
334349 * @ignore
335350 */
336- resize ( width , height , depth = 1 ) {
351+ resize ( width , height , slices = 1 ) {
337352
338353 // destroy texture impl
339354 const device = this . device ;
@@ -342,7 +357,7 @@ class Texture {
342357
343358 this . _width = Math . floor ( width ) ;
344359 this . _height = Math . floor ( height ) ;
345- this . _depth = Math . floor ( depth ) ;
360+ this . _slices = Math . floor ( slices ) ;
346361
347362 // re-create the implementation
348363 this . impl = device . createTextureImpl ( this ) ;
@@ -530,7 +545,7 @@ class Texture {
530545 * @type {number }
531546 */
532547 set addressW ( addressW ) {
533- if ( ! this . _volume ) {
548+ if ( ! this . volume ) {
534549 Debug . warn ( "pc.Texture#addressW: Can't set W addressing mode for a non-3D texture." ) ;
535550 return ;
536551 }
@@ -684,7 +699,16 @@ class Texture {
684699 * @type {number }
685700 */
686701 get depth ( ) {
687- return this . _depth ;
702+ return this . _dimension === TEXTUREDIMENSION_3D ? this . _slices : 1 ;
703+ }
704+
705+ /**
706+ * The number of textures in a texture array or the number of faces for a cubemap.
707+ *
708+ * @type {number }
709+ */
710+ get slices ( ) {
711+ return this . _slices ;
688712 }
689713
690714 /**
@@ -726,12 +750,12 @@ class Texture {
726750 * @type {boolean }
727751 */
728752 get cubemap ( ) {
729- return this . _cubemap ;
753+ return this . _dimension === TEXTUREDIMENSION_CUBE ;
730754 }
731755
732756 get gpuSize ( ) {
733757 const mips = this . pot && this . _mipmaps && ! ( this . _compressed && this . _levels . length === 1 ) ;
734- return TextureUtils . calcGpuSize ( this . _width , this . _height , this . _depth , this . _format , mips , this . _cubemap ) ;
758+ return TextureUtils . calcGpuSize ( this . _width , this . _height , this . _slices , this . _format , this . volume , mips ) ;
735759 }
736760
737761 /**
@@ -740,16 +764,7 @@ class Texture {
740764 * @type {boolean }
741765 */
742766 get array ( ) {
743- return this . _arrayLength > 0 ;
744- }
745-
746- /**
747- * Returns the number of textures inside this texture if this is a 2D array texture or 0 otherwise.
748- *
749- * @type {number }
750- */
751- get arrayLength ( ) {
752- return this . _arrayLength ;
767+ return this . _dimension === TEXTUREDIMENSION_2D_ARRAY ;
753768 }
754769
755770 /**
@@ -758,7 +773,7 @@ class Texture {
758773 * @type {boolean }
759774 */
760775 get volume ( ) {
761- return this . _volume ;
776+ return this . _dimension === TEXTUREDIMENSION_3D ;
762777 }
763778
764779 /**
@@ -821,7 +836,7 @@ class Texture {
821836
822837 // Force a full resubmission of the texture to the GPU (used on a context restore event)
823838 dirtyAll ( ) {
824- this . _levelsUpdated = this . _cubemap ? [ [ true , true , true , true , true , true ] ] : [ true ] ;
839+ this . _levelsUpdated = this . cubemap ? [ [ true , true , true , true , true , true ] ] : [ true ] ;
825840
826841 this . _needsUpload = true ;
827842 this . _needsMipmapsUpload = this . _mipmaps ;
@@ -871,7 +886,7 @@ class Texture {
871886 // allocate storage for this mip level
872887 const width = Math . max ( 1 , this . _width >> options . level ) ;
873888 const height = Math . max ( 1 , this . _height >> options . level ) ;
874- const depth = Math . max ( 1 , this . _depth >> options . level ) ;
889+ const depth = Math . max ( 1 , ( this . _dimension === TEXTUREDIMENSION_3D ? this . _slices : 1 ) >> options . level ) ;
875890 const data = new ArrayBuffer ( TextureUtils . calcLevelGpuSize ( width , height , depth , this . _format ) ) ;
876891 levels [ options . level ] = new ( getPixelFormatArrayType ( this . _format ) ) ( data ) ;
877892 }
@@ -894,7 +909,7 @@ class Texture {
894909 let invalid = false ;
895910 let width , height ;
896911
897- if ( this . _cubemap ) {
912+ if ( this . cubemap ) {
898913 if ( source [ 0 ] ) {
899914 // rely on first face sizes
900915 width = source [ 0 ] . width || 0 ;
@@ -946,7 +961,7 @@ class Texture {
946961 this . _height = 4 ;
947962
948963 // remove levels
949- if ( this . _cubemap ) {
964+ if ( this . cubemap ) {
950965 for ( let i = 0 ; i < 6 ; i ++ ) {
951966 this . _levels [ mipLevel ] [ i ] = null ;
952967 this . _levelsUpdated [ mipLevel ] [ i ] = true ;
0 commit comments