@loaders.gl/textures
Version:
Framework-independent loaders for compressed and super compressed (basis) textures
163 lines (155 loc) • 7.03 kB
text/typescript
// loaders.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
/* eslint-disable camelcase */
import type {TextureFormat} from '@loaders.gl/schema';
import type {GLTextureFormat} from '../gl-types';
import {
GL_RGBA32F,
GL_COMPRESSED_R11_EAC,
GL_COMPRESSED_RED_GREEN_RGTC2_EXT,
GL_COMPRESSED_RED_RGTC1_EXT,
GL_COMPRESSED_RG11_EAC,
GL_COMPRESSED_RGB8_ETC2,
GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
GL_COMPRESSED_RGB_ATC_WEBGL,
GL_COMPRESSED_RGB_ETC1_WEBGL,
GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG,
GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG,
GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
GL_COMPRESSED_RGBA8_ETC2_EAC,
GL_COMPRESSED_RGBA_ASTC_10x10_KHR,
GL_COMPRESSED_RGBA_ASTC_10x5_KHR,
GL_COMPRESSED_RGBA_ASTC_10x6_KHR,
GL_COMPRESSED_RGBA_ASTC_10x8_KHR,
GL_COMPRESSED_RGBA_ASTC_12x10_KHR,
GL_COMPRESSED_RGBA_ASTC_12x12_KHR,
GL_COMPRESSED_RGBA_ASTC_4x4_KHR,
GL_COMPRESSED_RGBA_ASTC_5x4_KHR,
GL_COMPRESSED_RGBA_ASTC_5x5_KHR,
GL_COMPRESSED_RGBA_ASTC_6x5_KHR,
GL_COMPRESSED_RGBA_ASTC_6x6_KHR,
GL_COMPRESSED_RGBA_ASTC_8x5_KHR,
GL_COMPRESSED_RGBA_ASTC_8x6_KHR,
GL_COMPRESSED_RGBA_ASTC_8x8_KHR,
GL_COMPRESSED_RGBA_ATC_EXPLICIT_ALPHA_WEBGL,
GL_COMPRESSED_RGBA_ATC_INTERPOLATED_ALPHA_WEBGL,
GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG,
GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG,
GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
GL_COMPRESSED_SIGNED_R11_EAC,
GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT,
GL_COMPRESSED_SIGNED_RED_RGTC1_EXT,
GL_COMPRESSED_SIGNED_RG11_EAC,
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR,
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR,
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR,
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR,
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR,
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR,
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR,
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR,
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR,
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,
GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,
GL_COMPRESSED_SRGB8_ETC2,
GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT,
GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT,
GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT,
GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
} from '../gl-extensions';
const WEBGL_TO_TEXTURE_FORMAT: Record<number, TextureFormat> = {
[]: 'rgba32float',
[]: 'bc1-rgb-unorm-webgl',
[]: 'bc1-rgb-unorm-srgb-webgl',
[]: 'bc1-rgba-unorm',
[]: 'bc1-rgba-unorm-srgb',
[]: 'bc2-rgba-unorm',
[]: 'bc2-rgba-unorm-srgb',
[]: 'bc3-rgba-unorm',
[]: 'bc3-rgba-unorm-srgb',
[]: 'bc4-r-unorm',
[]: 'bc4-r-snorm',
[]: 'bc5-rg-unorm',
[]: 'bc5-rg-snorm',
[]: 'etc2-rgb8unorm',
[]: 'etc2-rgb8unorm-srgb',
[]: 'etc2-rgb8a1unorm',
[]: 'etc2-rgb8a1unorm-srgb',
[]: 'etc2-rgba8unorm',
[]: 'etc2-rgba8unorm-srgb',
[]: 'eac-r11unorm',
[]: 'eac-r11snorm',
[]: 'eac-rg11unorm',
[]: 'eac-rg11snorm',
[]: 'astc-4x4-unorm',
[]: 'astc-4x4-unorm-srgb',
[]: 'astc-5x4-unorm',
[]: 'astc-5x4-unorm-srgb',
[]: 'astc-5x5-unorm',
[]: 'astc-5x5-unorm-srgb',
[]: 'astc-6x5-unorm',
[]: 'astc-6x5-unorm-srgb',
[]: 'astc-6x6-unorm',
[]: 'astc-6x6-unorm-srgb',
[]: 'astc-8x5-unorm',
[]: 'astc-8x5-unorm-srgb',
[]: 'astc-8x6-unorm',
[]: 'astc-8x6-unorm-srgb',
[]: 'astc-8x8-unorm',
[]: 'astc-8x8-unorm-srgb',
[]: 'astc-10x5-unorm',
[]: 'astc-10x5-unorm-srgb',
[]: 'astc-10x6-unorm',
[]: 'astc-10x6-unorm-srgb',
[]: 'astc-10x8-unorm',
[]: 'astc-10x8-unorm-srgb',
[]: 'astc-10x10-unorm',
[]: 'astc-10x10-unorm-srgb',
[]: 'astc-12x10-unorm',
[]: 'astc-12x10-unorm-srgb',
[]: 'astc-12x12-unorm',
[]: 'astc-12x12-unorm-srgb',
[]: 'pvrtc-rgb4unorm-webgl',
[]: 'pvrtc-rgba4unorm-webgl',
[]: 'pvrtc-rgb2unorm-webgl',
[]: 'pvrtc-rgba2unorm-webgl',
[]: 'etc1-rgb-unorm-webgl',
[]: 'atc-rgb-unorm-webgl',
[]: 'atc-rgba-unorm-webgl',
[]: 'atc-rgbai-unorm-webgl'
};
const TEXTURE_FORMAT_TO_WEBGL = Object.fromEntries(
Object.entries(WEBGL_TO_TEXTURE_FORMAT).map(([format, textureFormat]) => [
textureFormat,
Number(format)
])
) as Partial<Record<TextureFormat, GLTextureFormat>>;
export function getTextureFormatFromWebGLFormat(
format?: GLTextureFormat
): TextureFormat | undefined {
if (format === undefined) {
return undefined;
}
return WEBGL_TO_TEXTURE_FORMAT[format];
}
/**
* Returns the WebGL/OpenGL compressed texture constant for a canonical loaders.gl texture format.
* @param textureFormat - Canonical texture format string.
* @returns Matching WebGL/OpenGL constant, if known.
*/
export function getWebGLFormatFromTextureFormat(
textureFormat?: TextureFormat
): GLTextureFormat | undefined {
if (textureFormat === undefined) {
return undefined;
}
return TEXTURE_FORMAT_TO_WEBGL[textureFormat];
}