Skip to content

Commit 449c640

Browse files
authored
WebGPUTextures: Basic support for ASTC. (#25865)
* WebGPUTextures: Basic support for ASTC. * WebGPUTextures: Update _getBlockData().
1 parent a6603ad commit 449c640

File tree

2 files changed

+196
-67
lines changed

2 files changed

+196
-67
lines changed

examples/jsm/renderers/webgpu/WebGPUTextures.js

Lines changed: 164 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { GPUTextureFormat, GPUAddressMode, GPUFilterMode, GPUTextureDimension } from './constants.js';
22
import { VideoTexture, CubeTexture, Texture, NearestFilter, NearestMipmapNearestFilter, NearestMipmapLinearFilter, LinearFilter, RepeatWrapping, MirroredRepeatWrapping, RGB_ETC2_Format, RGBA_ETC2_EAC_Format,
3-
RGBAFormat, RedFormat, RGFormat, RGBA_S3TC_DXT1_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT5_Format, UnsignedByteType, FloatType, HalfFloatType, SRGBColorSpace, DepthFormat, DepthTexture
3+
RGBAFormat, RedFormat, RGFormat, RGBA_S3TC_DXT1_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT5_Format, UnsignedByteType, FloatType, HalfFloatType, SRGBColorSpace, DepthFormat, DepthTexture,
4+
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,
5+
RGBA_ASTC_10x6_Format, RGBA_ASTC_10x8_Format, RGBA_ASTC_10x10_Format, RGBA_ASTC_12x10_Format, RGBA_ASTC_12x12_Format
46
} from 'three';
57
import WebGPUTextureUtils from './WebGPUTextureUtils.js';
68

@@ -580,6 +582,32 @@ class WebGPUTextures {
580582
if ( format === GPUTextureFormat.BC6HRGBUFloat || format === GPUTextureFormat.BC6HRGBFloat ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (float)
581583
if ( format === GPUTextureFormat.BC7RGBAUnorm || format === GPUTextureFormat.BC7RGBAUnormSRGB ) return { byteLength: 16, width: 4, height: 4 }; // BPTC (unorm)
582584

585+
if ( format === GPUTextureFormat.ETC2RGB8Unorm || format === GPUTextureFormat.ETC2RGB8UnormSRGB ) return { byteLength: 8, width: 4, height: 4 };
586+
if ( format === GPUTextureFormat.ETC2RGB8A1Unorm || format === GPUTextureFormat.ETC2RGB8A1UnormSRGB ) return { byteLength: 8, width: 4, height: 4 };
587+
if ( format === GPUTextureFormat.ETC2RGBA8Unorm || format === GPUTextureFormat.ETC2RGBA8UnormSRGB ) return { byteLength: 16, width: 4, height: 4 };
588+
if ( format === GPUTextureFormat.EACR11Unorm ) return { byteLength: 8, width: 4, height: 4 };
589+
if ( format === GPUTextureFormat.EACR11Snorm ) return { byteLength: 8, width: 4, height: 4 };
590+
if ( format === GPUTextureFormat.EACRG11Unorm ) return { byteLength: 16, width: 4, height: 4 };
591+
if ( format === GPUTextureFormat.EACRG11Snorm ) return { byteLength: 16, width: 4, height: 4 };
592+
593+
if ( format === GPUTextureFormat.ASTC4x4Unorm || format === GPUTextureFormat.ASTC4x4UnormSRGB ) return { byteLength: 16, width: 4, height: 4 };
594+
if ( format === GPUTextureFormat.ASTC5x4Unorm || format === GPUTextureFormat.ASTC5x4UnormSRGB ) return { byteLength: 16, width: 5, height: 4 };
595+
if ( format === GPUTextureFormat.ASTC5x5Unorm || format === GPUTextureFormat.ASTC5x5UnormSRGB ) return { byteLength: 16, width: 5, height: 5 };
596+
if ( format === GPUTextureFormat.ASTC6x5Unorm || format === GPUTextureFormat.ASTC6x5UnormSRGB ) return { byteLength: 16, width: 6, height: 5 };
597+
if ( format === GPUTextureFormat.ASTC6x6Unorm || format === GPUTextureFormat.ASTC6x6UnormSRGB ) return { byteLength: 16, width: 6, height: 6 };
598+
if ( format === GPUTextureFormat.ASTC8x5Unorm || format === GPUTextureFormat.ASTC8x5UnormSRGB ) return { byteLength: 16, width: 8, height: 5 };
599+
if ( format === GPUTextureFormat.ASTC8x6Unorm || format === GPUTextureFormat.ASTC8x6UnormSRGB ) return { byteLength: 16, width: 8, height: 6 };
600+
if ( format === GPUTextureFormat.ASTC8x8Unorm || format === GPUTextureFormat.ASTC8x8UnormSRGB ) return { byteLength: 16, width: 8, height: 8 };
601+
if ( format === GPUTextureFormat.ASTC10x5Unorm || format === GPUTextureFormat.ASTC10x5UnormSRGB ) return { byteLength: 16, width: 10, height: 5 };
602+
if ( format === GPUTextureFormat.ASTC10x6Unorm || format === GPUTextureFormat.ASTC10x6UnormSRGB ) return { byteLength: 16, width: 10, height: 6 };
603+
if ( format === GPUTextureFormat.ASTC10x8Unorm || format === GPUTextureFormat.ASTC10x8UnormSRGB ) return { byteLength: 16, width: 10, height: 8 };
604+
if ( format === GPUTextureFormat.ASTC10x10Unorm || format === GPUTextureFormat.ASTC10x10UnormSRGB ) return { byteLength: 16, width: 10, height: 10 };
605+
if ( format === GPUTextureFormat.ASTC12x10Unorm || format === GPUTextureFormat.ASTC12x10UnormSRGB ) return { byteLength: 16, width: 12, height: 10 };
606+
if ( format === GPUTextureFormat.ASTC12x12Unorm || format === GPUTextureFormat.ASTC12x12UnormSRGB ) return { byteLength: 16, width: 12, height: 12 };
607+
608+
609+
610+
583611
}
584612

585613
_getBytesPerTexel( format ) {
@@ -622,103 +650,172 @@ class WebGPUTextures {
622650

623651
let formatGPU;
624652

625-
switch ( format ) {
653+
if ( texture.isCompressedTexture === true ) {
626654

627-
case RGBA_S3TC_DXT1_Format:
628-
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC1RGBAUnormSRGB : GPUTextureFormat.BC1RGBAUnorm;
629-
break;
655+
switch ( format ) {
630656

631-
case RGBA_S3TC_DXT3_Format:
632-
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC2RGBAUnormSRGB : GPUTextureFormat.BC2RGBAUnorm;
633-
break;
657+
case RGBA_S3TC_DXT1_Format:
658+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC1RGBAUnormSRGB : GPUTextureFormat.BC1RGBAUnorm;
659+
break;
634660

635-
case RGBA_S3TC_DXT5_Format:
636-
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC3RGBAUnormSRGB : GPUTextureFormat.BC3RGBAUnorm;
637-
break;
661+
case RGBA_S3TC_DXT3_Format:
662+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC2RGBAUnormSRGB : GPUTextureFormat.BC2RGBAUnorm;
663+
break;
638664

639-
case RGB_ETC2_Format:
640-
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ETC2RGB8UnormSRGB : GPUTextureFormat.ETC2RGB8Unorm;
641-
break;
665+
case RGBA_S3TC_DXT5_Format:
666+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.BC3RGBAUnormSRGB : GPUTextureFormat.BC3RGBAUnorm;
667+
break;
642668

643-
case RGBA_ETC2_EAC_Format:
644-
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ETC2RGBA8UnormSRGB : GPUTextureFormat.ETC2RGBA8Unorm;
645-
break;
669+
case RGB_ETC2_Format:
670+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ETC2RGB8UnormSRGB : GPUTextureFormat.ETC2RGB8Unorm;
671+
break;
646672

647-
case DepthFormat:
648-
formatGPU = GPUTextureFormat.Depth32Float;
649-
break;
673+
case RGBA_ETC2_EAC_Format:
674+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ETC2RGBA8UnormSRGB : GPUTextureFormat.ETC2RGBA8Unorm;
675+
break;
650676

651-
case RGBAFormat:
677+
case RGBA_ASTC_4x4_Format:
678+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC4x4UnormSRGB : GPUTextureFormat.ASTC4x4Unorm;
679+
break;
652680

653-
switch ( type ) {
681+
case RGBA_ASTC_5x4_Format:
682+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC5x4UnormSRGB : GPUTextureFormat.ASTC5x4Unorm;
683+
break;
654684

655-
case UnsignedByteType:
656-
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.RGBA8UnormSRGB : GPUTextureFormat.RGBA8Unorm;
657-
break;
685+
case RGBA_ASTC_5x5_Format:
686+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC5x5UnormSRGB : GPUTextureFormat.ASTC5x5Unorm;
687+
break;
658688

659-
case HalfFloatType:
660-
formatGPU = GPUTextureFormat.RGBA16Float;
661-
break;
689+
case RGBA_ASTC_6x5_Format:
690+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC6x5UnormSRGB : GPUTextureFormat.ASTC6x5Unorm;
691+
break;
662692

663-
case FloatType:
664-
formatGPU = GPUTextureFormat.RGBA32Float;
665-
break;
693+
case RGBA_ASTC_6x6_Format:
694+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC6x6UnormSRGB : GPUTextureFormat.ASTC6x6Unorm;
695+
break;
666696

667-
default:
668-
console.error( 'WebGPURenderer: Unsupported texture type with RGBAFormat.', type );
697+
case RGBA_ASTC_8x5_Format:
698+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC8x5UnormSRGB : GPUTextureFormat.ASTC8x5Unorm;
699+
break;
669700

670-
}
701+
case RGBA_ASTC_8x6_Format:
702+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC8x6UnormSRGB : GPUTextureFormat.ASTC8x6Unorm;
703+
break;
671704

672-
break;
705+
case RGBA_ASTC_8x8_Format:
706+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC8x8UnormSRGB : GPUTextureFormat.ASTC8x8Unorm;
707+
break;
673708

674-
case RedFormat:
709+
case RGBA_ASTC_10x5_Format:
710+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x5UnormSRGB : GPUTextureFormat.ASTC10x5Unorm;
711+
break;
675712

676-
switch ( type ) {
713+
case RGBA_ASTC_10x6_Format:
714+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x6UnormSRGB : GPUTextureFormat.ASTC10x6Unorm;
715+
break;
677716

678-
case UnsignedByteType:
679-
formatGPU = GPUTextureFormat.R8Unorm;
680-
break;
717+
case RGBA_ASTC_10x8_Format:
718+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x8UnormSRGB : GPUTextureFormat.ASTC10x8Unorm;
719+
break;
681720

682-
case HalfFloatType:
683-
formatGPU = GPUTextureFormat.R16Float;
684-
break;
721+
case RGBA_ASTC_10x10_Format:
722+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC10x10UnormSRGB : GPUTextureFormat.ASTC10x10Unorm;
723+
break;
685724

686-
case FloatType:
687-
formatGPU = GPUTextureFormat.R32Float;
688-
break;
725+
case RGBA_ASTC_12x10_Format:
726+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC12x10UnormSRGB : GPUTextureFormat.ASTC12x10Unorm;
727+
break;
689728

690-
default:
691-
console.error( 'WebGPURenderer: Unsupported texture type with RedFormat.', type );
729+
case RGBA_ASTC_12x12_Format:
730+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.ASTC12x12UnormSRGB : GPUTextureFormat.ASTC12x12Unorm;
731+
break;
692732

693-
}
733+
case DepthFormat:
734+
formatGPU = GPUTextureFormat.Depth32Float;
735+
break;
736+
737+
default:
738+
console.error( 'WebGPURenderer: Unsupported texture format.', format );
694739

695-
break;
740+
}
741+
742+
} else {
696743

697-
case RGFormat:
744+
switch ( format ) {
698745

699-
switch ( type ) {
746+
case RGBAFormat:
700747

701-
case UnsignedByteType:
702-
formatGPU = GPUTextureFormat.RG8Unorm;
703-
break;
748+
switch ( type ) {
704749

705-
case HalfFloatType:
706-
formatGPU = GPUTextureFormat.RG16Float;
707-
break;
750+
case UnsignedByteType:
751+
formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat.RGBA8UnormSRGB : GPUTextureFormat.RGBA8Unorm;
752+
break;
708753

709-
case FloatType:
710-
formatGPU = GPUTextureFormat.RG32Float;
711-
break;
754+
case HalfFloatType:
755+
formatGPU = GPUTextureFormat.RGBA16Float;
756+
break;
712757

713-
default:
714-
console.error( 'WebGPURenderer: Unsupported texture type with RGFormat.', type );
758+
case FloatType:
759+
formatGPU = GPUTextureFormat.RGBA32Float;
760+
break;
715761

716-
}
762+
default:
763+
console.error( 'WebGPURenderer: Unsupported texture type with RGBAFormat.', type );
764+
765+
}
766+
767+
break;
768+
769+
case RedFormat:
770+
771+
switch ( type ) {
717772

718-
break;
773+
case UnsignedByteType:
774+
formatGPU = GPUTextureFormat.R8Unorm;
775+
break;
719776

720-
default:
721-
console.error( 'WebGPURenderer: Unsupported texture format.', format );
777+
case HalfFloatType:
778+
formatGPU = GPUTextureFormat.R16Float;
779+
break;
780+
781+
case FloatType:
782+
formatGPU = GPUTextureFormat.R32Float;
783+
break;
784+
785+
default:
786+
console.error( 'WebGPURenderer: Unsupported texture type with RedFormat.', type );
787+
788+
}
789+
790+
break;
791+
792+
case RGFormat:
793+
794+
switch ( type ) {
795+
796+
case UnsignedByteType:
797+
formatGPU = GPUTextureFormat.RG8Unorm;
798+
break;
799+
800+
case HalfFloatType:
801+
formatGPU = GPUTextureFormat.RG16Float;
802+
break;
803+
804+
case FloatType:
805+
formatGPU = GPUTextureFormat.RG32Float;
806+
break;
807+
808+
default:
809+
console.error( 'WebGPURenderer: Unsupported texture type with RGFormat.', type );
810+
811+
}
812+
813+
break;
814+
815+
default:
816+
console.error( 'WebGPURenderer: Unsupported texture format.', format );
817+
818+
}
722819

723820
}
724821

examples/jsm/renderers/webgpu/constants.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,38 @@ export const GPUTextureFormat = {
170170
EACRG11Unorm: 'eac-rg11unorm',
171171
EACRG11Snorm: 'eac-rg11snorm',
172172

173+
// ASTC compressed formats usable if 'texture-compression-astc' is both
174+
// supported by the device/user agent and enabled in requestDevice.
175+
176+
ASTC4x4Unorm: 'astc-4x4-unorm',
177+
ASTC4x4UnormSRGB: 'astc-4x4-unorm-srgb',
178+
ASTC5x4Unorm: 'astc-5x4-unorm',
179+
ASTC5x4UnormSRGB: 'astc-5x4-unorm-srgb',
180+
ASTC5x5Unorm: 'astc-5x5-unorm',
181+
ASTC5x5UnormSRGB: 'astc-5x5-unorm-srgb',
182+
ASTC6x5Unorm: 'astc-6x5-unorm',
183+
ASTC6x5UnormSRGB: 'astc-6x5-unorm-srgb',
184+
ASTC6x6Unorm: 'astc-6x6-unorm',
185+
ASTC6x6UnormSRGB: 'astc-6x6-unorm-srgb',
186+
ASTC8x5Unorm: 'astc-8x5-unorm',
187+
ASTC8x5UnormSRGB: 'astc-8x5-unorm-srgb',
188+
ASTC8x6Unorm: 'astc-8x6-unorm',
189+
ASTC8x6UnormSRGB: 'astc-8x6-unorm-srgb',
190+
ASTC8x8Unorm: 'astc-8x8-unorm',
191+
ASTC8x8UnormSRGB: 'astc-8x8-unorm-srgb',
192+
ASTC10x5Unorm: 'astc-10x5-unorm',
193+
ASTC10x5UnormSRGB: 'astc-10x5-unorm-srgb',
194+
ASTC10x6Unorm: 'astc-10x6-unorm',
195+
ASTC10x6UnormSRGB: 'astc-10x6-unorm-srgb',
196+
ASTC10x8Unorm: 'astc-10x8-unorm',
197+
ASTC10x8UnormSRGB: 'astc-10x8-unorm-srgb',
198+
ASTC10x10Unorm: 'astc-10x10-unorm',
199+
ASTC10x10UnormSRGB: 'astc-10x10-unorm-srgb',
200+
ASTC12x10Unorm: 'astc-12x10-unorm',
201+
ASTC12x10UnormSRGB: 'astc-12x10-unorm-srgb',
202+
ASTC12x12Unorm: 'astc-12x12-unorm',
203+
ASTC12x12UnormSRGB: 'astc-12x12-unorm-srgb',
204+
173205
// 'depth24unorm-stencil8' extension
174206

175207
Depth24UnormStencil8: 'depth24unorm-stencil8',

0 commit comments

Comments
 (0)