@@ -15,7 +15,11 @@ import {
1515 TEXTURELOCK_WRITE ,
1616 TEXTUREPROJECTION_NONE , TEXTUREPROJECTION_CUBE ,
1717 TEXTURETYPE_DEFAULT , TEXTURETYPE_RGBM , TEXTURETYPE_RGBE , TEXTURETYPE_RGBP ,
18- isIntegerPixelFormat , FILTER_NEAREST , TEXTURELOCK_NONE , TEXTURELOCK_READ
18+ isIntegerPixelFormat , FILTER_NEAREST , TEXTURELOCK_NONE , TEXTURELOCK_READ ,
19+ TEXTUREDIMENSION_2D ,
20+ TEXTUREDIMENSION_3D ,
21+ TEXTUREDIMENSION_2D_ARRAY ,
22+ TEXTUREDIMENSION_CUBE
1923} from './constants.js' ;
2024
2125let id = 0 ;
@@ -101,7 +105,15 @@ class Texture {
101105 * @param {string } [options.name] - The name of the texture. Defaults to null.
102106 * @param {number } [options.width] - The width of the texture in pixels. Defaults to 4.
103107 * @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.
105117 * @param {number } [options.format] - The pixel format of the texture. Can be:
106118 *
107119 * - {@link PIXELFORMAT_R8}
@@ -155,9 +167,8 @@ class Texture {
155167 * texture. Default is true.
156168 * @param {boolean } [options.cubemap] - Specifies whether the texture is to be a cubemap.
157169 * 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.
161172 * @param {boolean } [options.volume] - Specifies whether the texture is to be a 3D volume.
162173 * Defaults to false.
163174 * @param {string } [options.type] - Specifies the texture type. Can be:
@@ -192,7 +203,7 @@ class Texture {
192203 * Defaults to {@link FUNC_LESS}.
193204 * @param {Uint8Array[]|HTMLCanvasElement[]|HTMLImageElement[]|HTMLVideoElement[]|Uint8Array[][] } [options.levels]
194205 * - 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} .
196207 * @param {boolean } [options.storage] - Defines if texture can be used as a storage texture by
197208 * a compute shader. Defaults to false.
198209 * @param {boolean } [options.immediate] - If set and true, the texture will be uploaded to the GPU immediately.
@@ -221,13 +232,22 @@ class Texture {
221232 Debug . assert ( this . device , "Texture constructor requires a graphicsDevice to be valid" ) ;
222233 Debug . assert ( ! options . width || Number . isInteger ( options . width ) , "Texture width must be an integer number, got" , options ) ;
223234 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 ) ;
225236
226237 this . name = options . name ?? '' ;
227238
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+
228244 this . _width = Math . floor ( options . width ?? 4 ) ;
229245 this . _height = Math . floor ( options . height ?? 4 ) ;
230246
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+
231251 this . _format = options . format ?? PIXELFORMAT_RGBA8 ;
232252 this . _compressed = isCompressedPixelFormat ( this . _format ) ;
233253 this . _integerFormat = isIntegerPixelFormat ( this . _format ) ;
@@ -237,12 +257,7 @@ class Texture {
237257 options . magFilter = FILTER_NEAREST ;
238258 }
239259
240- this . _volume = options . volume ?? false ;
241- this . _depth = Math . floor ( options . depth ?? 1 ) ;
242- this . _arrayLength = Math . floor ( options . arrayLength ?? 0 ) ;
243-
244260 this . _storage = options . storage ?? false ;
245- this . _cubemap = options . cubemap ?? false ;
246261 this . _flipY = options . flipY ?? false ;
247262 this . _premultiplyAlpha = options . premultiplyAlpha ?? false ;
248263
@@ -262,7 +277,7 @@ class Texture {
262277 Debug . assert ( ! options . hasOwnProperty ( 'swizzleGGGR' ) , 'Use options.type.' ) ;
263278
264279 this . projection = TEXTUREPROJECTION_NONE ;
265- if ( this . _cubemap ) {
280+ if ( this . cubemap ) {
266281 this . projection = TEXTUREPROJECTION_CUBE ;
267282 } else if ( options . projection && options . projection !== TEXTUREPROJECTION_CUBE ) {
268283 this . projection = options . projection ;
@@ -280,7 +295,7 @@ class Texture {
280295 if ( this . _levels ) {
281296 this . upload ( options . immediate ?? false ) ;
282297 } 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 ] ;
284299 }
285300
286301 // track the texture
@@ -328,10 +343,10 @@ class Texture {
328343 *
329344 * @param {number } width - The new width of the texture.
330345 * @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.
332347 * @ignore
333348 */
334- resize ( width , height , depth = 1 ) {
349+ resize ( width , height , slices = 1 ) {
335350
336351 // destroy texture impl
337352 const device = this . device ;
@@ -340,7 +355,7 @@ class Texture {
340355
341356 this . _width = Math . floor ( width ) ;
342357 this . _height = Math . floor ( height ) ;
343- this . _depth = Math . floor ( depth ) ;
358+ this . _slices = Math . floor ( slices ) ;
344359
345360 // re-create the implementation
346361 this . impl = device . createTextureImpl ( this ) ;
@@ -528,7 +543,7 @@ class Texture {
528543 * @type {number }
529544 */
530545 set addressW ( addressW ) {
531- if ( ! this . _volume ) {
546+ if ( ! this . volume ) {
532547 Debug . warn ( "pc.Texture#addressW: Can't set W addressing mode for a non-3D texture." ) ;
533548 return ;
534549 }
@@ -682,7 +697,16 @@ class Texture {
682697 * @type {number }
683698 */
684699 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 ;
686710 }
687711
688712 /**
@@ -724,12 +748,12 @@ class Texture {
724748 * @type {boolean }
725749 */
726750 get cubemap ( ) {
727- return this . _cubemap ;
751+ return this . _dimension === TEXTUREDIMENSION_CUBE ;
728752 }
729753
730754 get gpuSize ( ) {
731755 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 ) ;
733757 }
734758
735759 /**
@@ -738,16 +762,7 @@ class Texture {
738762 * @type {boolean }
739763 */
740764 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 ;
751766 }
752767
753768 /**
@@ -756,7 +771,7 @@ class Texture {
756771 * @type {boolean }
757772 */
758773 get volume ( ) {
759- return this . _volume ;
774+ return this . _dimension === TEXTUREDIMENSION_3D ;
760775 }
761776
762777 /**
@@ -822,7 +837,7 @@ class Texture {
822837
823838 // Force a full resubmission of the texture to the GPU (used on a context restore event)
824839 dirtyAll ( ) {
825- this . _levelsUpdated = this . _cubemap ? [ [ true , true , true , true , true , true ] ] : [ true ] ;
840+ this . _levelsUpdated = this . cubemap ? [ [ true , true , true , true , true , true ] ] : [ true ] ;
826841
827842 this . _needsUpload = true ;
828843 this . _needsMipmapsUpload = this . _mipmaps ;
@@ -872,7 +887,7 @@ class Texture {
872887 // allocate storage for this mip level
873888 const width = Math . max ( 1 , this . _width >> options . level ) ;
874889 const height = Math . max ( 1 , this . _height >> options . level ) ;
875- const depth = Math . max ( 1 , this . _depth >> options . level ) ;
890+ const depth = Math . max ( 1 , ( this . _dimension === TEXTUREDIMENSION_3D ? this . _slices : 1 ) >> options . level ) ;
876891 const data = new ArrayBuffer ( TextureUtils . calcLevelGpuSize ( width , height , depth , this . _format ) ) ;
877892 levels [ options . level ] = new ( getPixelFormatArrayType ( this . _format ) ) ( data ) ;
878893 }
@@ -895,7 +910,7 @@ class Texture {
895910 let invalid = false ;
896911 let width , height ;
897912
898- if ( this . _cubemap ) {
913+ if ( this . cubemap ) {
899914 if ( source [ 0 ] ) {
900915 // rely on first face sizes
901916 width = source [ 0 ] . width || 0 ;
@@ -947,7 +962,7 @@ class Texture {
947962 this . _height = 4 ;
948963
949964 // remove levels
950- if ( this . _cubemap ) {
965+ if ( this . cubemap ) {
951966 for ( let i = 0 ; i < 6 ; i ++ ) {
952967 this . _levels [ mipLevel ] [ i ] = null ;
953968 this . _levelsUpdated [ mipLevel ] [ i ] = true ;
0 commit comments