|
| 1 | +import { AlphaFormat, LuminanceFormat, LuminanceAlphaFormat, RedFormat, RedIntegerFormat, RGFormat, RGIntegerFormat, RGBFormat, RGBAFormat, RGBAIntegerFormat, RGB_S3TC_DXT1_Format, RGBA_S3TC_DXT1_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT5_Format, RGB_PVRTC_2BPPV1_Format, RGBA_PVRTC_2BPPV1_Format, RGB_PVRTC_4BPPV1_Format, RGBA_PVRTC_4BPPV1_Format, RGB_ETC1_Format, RGB_ETC2_Format, RGBA_ETC2_EAC_Format, RGBA_ASTC_4x4_Format, RGBA_ASTC_5x4_Format, RGBA_ASTC_5x5_Format, RGBA_ASTC_6x5_Format, RGBA_ASTC_6x6_Format, RGBA_ASTC_8x5_Format, RGBA_ASTC_8x6_Format, RGBA_ASTC_8x8_Format, RGBA_ASTC_10x5_Format, RGBA_ASTC_10x6_Format, RGBA_ASTC_10x8_Format, RGBA_ASTC_10x10_Format, RGBA_ASTC_12x10_Format, RGBA_ASTC_12x12_Format, RGBA_BPTC_Format, RGB_BPTC_SIGNED_Format, RGB_BPTC_UNSIGNED_Format, RED_RGTC1_Format, SIGNED_RED_RGTC1_Format, RED_GREEN_RGTC2_Format, SIGNED_RED_GREEN_RGTC2_Format, UnsignedByteType, ByteType, UnsignedShortType, ShortType, HalfFloatType, UnsignedShort4444Type, UnsignedShort5551Type, UnsignedIntType, IntType, FloatType, UnsignedInt5999Type } from '../constants.js'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Given the width, height, format, and type of a texture. Determines how many |
| 5 | + * bytes must be used to represent the texture. |
| 6 | + */ |
| 7 | +function getByteLength( width, height, format, type ) { |
| 8 | + |
| 9 | + const typeByteLength = getTextureTypeByteLength( type ); |
| 10 | + |
| 11 | + switch ( format ) { |
| 12 | + |
| 13 | + // https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glTexImage2D.xhtml |
| 14 | + case AlphaFormat: |
| 15 | + return width * height; |
| 16 | + case LuminanceFormat: |
| 17 | + return width * height; |
| 18 | + case LuminanceAlphaFormat: |
| 19 | + return width * height * 2; |
| 20 | + case RedFormat: |
| 21 | + return ( ( width * height ) / typeByteLength.components ) * typeByteLength.byteLength; |
| 22 | + case RedIntegerFormat: |
| 23 | + return ( ( width * height ) / typeByteLength.components ) * typeByteLength.byteLength; |
| 24 | + case RGFormat: |
| 25 | + return ( ( width * height * 2 ) / typeByteLength.components ) * typeByteLength.byteLength; |
| 26 | + case RGIntegerFormat: |
| 27 | + return ( ( width * height * 2 ) / typeByteLength.components ) * typeByteLength.byteLength; |
| 28 | + case RGBFormat: |
| 29 | + return ( ( width * height * 3 ) / typeByteLength.components ) * typeByteLength.byteLength; |
| 30 | + case RGBAFormat: |
| 31 | + return ( ( width * height * 4 ) / typeByteLength.components ) * typeByteLength.byteLength; |
| 32 | + case RGBAIntegerFormat: |
| 33 | + return ( ( width * height * 4 ) / typeByteLength.components ) * typeByteLength.byteLength; |
| 34 | + |
| 35 | + // https://registry.khronos.org/webgl/extensions/WEBGL_compressed_texture_s3tc_srgb/ |
| 36 | + case RGB_S3TC_DXT1_Format: |
| 37 | + case RGBA_S3TC_DXT1_Format: |
| 38 | + return Math.floor( ( width + 3 ) / 4 ) * Math.floor( ( height + 3 ) / 4 ) * 8; |
| 39 | + case RGBA_S3TC_DXT3_Format: |
| 40 | + case RGBA_S3TC_DXT5_Format: |
| 41 | + return Math.floor( ( width + 3 ) / 4 ) * Math.floor( ( height + 3 ) / 4 ) * 16; |
| 42 | + |
| 43 | + // https://registry.khronos.org/webgl/extensions/WEBGL_compressed_texture_pvrtc/ |
| 44 | + case RGB_PVRTC_2BPPV1_Format: |
| 45 | + case RGBA_PVRTC_2BPPV1_Format: |
| 46 | + return ( Math.max( width, 16 ) * Math.max( height, 8 ) ) / 4; |
| 47 | + case RGB_PVRTC_4BPPV1_Format: |
| 48 | + case RGBA_PVRTC_4BPPV1_Format: |
| 49 | + return ( Math.max( width, 8 ) * Math.max( height, 8 ) ) / 2; |
| 50 | + |
| 51 | + // https://registry.khronos.org/webgl/extensions/WEBGL_compressed_texture_etc/ |
| 52 | + case RGB_ETC1_Format: |
| 53 | + case RGB_ETC2_Format: |
| 54 | + return Math.floor( ( width + 3 ) / 4 ) * Math.floor( ( height + 3 ) / 4 ) * 8; |
| 55 | + case RGBA_ETC2_EAC_Format: |
| 56 | + return Math.floor( ( width + 3 ) / 4 ) * Math.floor( ( height + 3 ) / 4 ) * 16; |
| 57 | + |
| 58 | + // https://registry.khronos.org/webgl/extensions/WEBGL_compressed_texture_astc/ |
| 59 | + case RGBA_ASTC_4x4_Format: |
| 60 | + return Math.floor( ( width + 3 ) / 4 ) * Math.floor( ( height + 3 ) / 4 ) * 16; |
| 61 | + case RGBA_ASTC_5x4_Format: |
| 62 | + return Math.floor( ( width + 4 ) / 5 ) * Math.floor( ( height + 3 ) / 4 ) * 16; |
| 63 | + case RGBA_ASTC_5x5_Format: |
| 64 | + return Math.floor( ( width + 4 ) / 5 ) * Math.floor( ( height + 4 ) / 5 ) * 16; |
| 65 | + case RGBA_ASTC_6x5_Format: |
| 66 | + return Math.floor( ( width + 5 ) / 6 ) * Math.floor( ( height + 4 ) / 5 ) * 16; |
| 67 | + case RGBA_ASTC_6x6_Format: |
| 68 | + return Math.floor( ( width + 5 ) / 6 ) * Math.floor( ( height + 5 ) / 6 ) * 16; |
| 69 | + case RGBA_ASTC_8x5_Format: |
| 70 | + return Math.floor( ( width + 7 ) / 8 ) * Math.floor( ( height + 4 ) / 5 ) * 16; |
| 71 | + case RGBA_ASTC_8x6_Format: |
| 72 | + return Math.floor( ( width + 7 ) / 8 ) * Math.floor( ( height + 5 ) / 6 ) * 16; |
| 73 | + case RGBA_ASTC_8x8_Format: |
| 74 | + return Math.floor( ( width + 7 ) / 8 ) * Math.floor( ( height + 7 ) / 8 ) * 16; |
| 75 | + case RGBA_ASTC_10x5_Format: |
| 76 | + return Math.floor( ( width + 9 ) / 10 ) * Math.floor( ( height + 4 ) / 5 ) * 16; |
| 77 | + case RGBA_ASTC_10x6_Format: |
| 78 | + return Math.floor( ( width + 9 ) / 10 ) * Math.floor( ( height + 5 ) / 6 ) * 16; |
| 79 | + case RGBA_ASTC_10x8_Format: |
| 80 | + return Math.floor( ( width + 9 ) / 10 ) * Math.floor( ( height + 7 ) / 8 ) * 16; |
| 81 | + case RGBA_ASTC_10x10_Format: |
| 82 | + return Math.floor( ( width + 9 ) / 10 ) * Math.floor( ( height + 9 ) / 10 ) * 16; |
| 83 | + case RGBA_ASTC_12x10_Format: |
| 84 | + return Math.floor( ( width + 11 ) / 12 ) * Math.floor( ( height + 9 ) / 10 ) * 16; |
| 85 | + case RGBA_ASTC_12x12_Format: |
| 86 | + return Math.floor( ( width + 11 ) / 12 ) * Math.floor( ( height + 11 ) / 12 ) * 16; |
| 87 | + |
| 88 | + // https://registry.khronos.org/webgl/extensions/EXT_texture_compression_bptc/ |
| 89 | + case RGBA_BPTC_Format: |
| 90 | + case RGB_BPTC_SIGNED_Format: |
| 91 | + case RGB_BPTC_UNSIGNED_Format: |
| 92 | + return Math.ceil( width / 4 ) * Math.ceil( height / 4 ) * 16; |
| 93 | + |
| 94 | + // https://registry.khronos.org/webgl/extensions/EXT_texture_compression_rgtc/ |
| 95 | + case RED_RGTC1_Format: |
| 96 | + case SIGNED_RED_RGTC1_Format: |
| 97 | + return Math.ceil( width / 4 ) * Math.ceil( height / 4 ) * 8; |
| 98 | + case RED_GREEN_RGTC2_Format: |
| 99 | + case SIGNED_RED_GREEN_RGTC2_Format: |
| 100 | + return Math.ceil( width / 4 ) * Math.ceil( height / 4 ) * 16; |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + throw new Error( |
| 105 | + `Unable to determine texture byte length for ${format} format.`, |
| 106 | + ); |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | +function getTextureTypeByteLength( type ) { |
| 111 | + |
| 112 | + switch ( type ) { |
| 113 | + |
| 114 | + case UnsignedByteType: |
| 115 | + case ByteType: |
| 116 | + return { byteLength: 1, components: 1 }; |
| 117 | + case UnsignedShortType: |
| 118 | + case ShortType: |
| 119 | + case HalfFloatType: |
| 120 | + return { byteLength: 2, components: 1 }; |
| 121 | + case UnsignedShort4444Type: |
| 122 | + case UnsignedShort5551Type: |
| 123 | + return { byteLength: 2, components: 4 }; |
| 124 | + case UnsignedIntType: |
| 125 | + case IntType: |
| 126 | + case FloatType: |
| 127 | + return { byteLength: 4, components: 1 }; |
| 128 | + case UnsignedInt5999Type: |
| 129 | + return { byteLength: 4, components: 3 }; |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + throw new Error( `Unknown texture type ${type}.` ); |
| 134 | + |
| 135 | +} |
| 136 | + |
| 137 | +const TextureUtils = { |
| 138 | + getByteLength, |
| 139 | +}; |
| 140 | + |
| 141 | +export { getByteLength, TextureUtils }; |
0 commit comments