@@ -106,7 +106,7 @@ class WebGPUTextureUtils {
106
106
const { width, height, depth, levels } = options ;
107
107
108
108
const dimension = this . _getDimension ( texture ) ;
109
- const format = texture . internalFormat || this . _getFormat ( texture ) ;
109
+ const format = texture . internalFormat || getFormat ( texture , this . device ) ;
110
110
111
111
const sampleCount = options . sampleCount !== undefined ? options . sampleCount : 1 ;
112
112
const primarySampleCount = texture . isRenderTargetTexture ? 1 : sampleCount ;
@@ -649,237 +649,237 @@ class WebGPUTextureUtils {
649
649
650
650
}
651
651
652
- _getFormat ( texture ) {
652
+ }
653
653
654
- const format = texture . format ;
655
- const type = texture . type ;
656
- const colorSpace = texture . colorSpace ;
654
+ export function getFormat ( texture , device = null ) {
657
655
658
- let formatGPU ;
656
+ const format = texture . format ;
657
+ const type = texture . type ;
658
+ const colorSpace = texture . colorSpace ;
659
659
660
- if ( /*texture.isRenderTargetTexture === true ||*/ texture . isFramebufferTexture === true ) {
660
+ let formatGPU ;
661
661
662
- formatGPU = GPUTextureFormat . BGRA8Unorm ;
662
+ if ( /*texture.isRenderTargetTexture === true ||*/ texture . isFramebufferTexture === true ) {
663
663
664
- } else if ( texture . isCompressedTexture === true ) {
664
+ formatGPU = GPUTextureFormat . BGRA8Unorm ;
665
665
666
- switch ( format ) {
666
+ } else if ( texture . isCompressedTexture === true ) {
667
667
668
- case RGBA_S3TC_DXT1_Format :
669
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . BC1RGBAUnormSRGB : GPUTextureFormat . BC1RGBAUnorm ;
670
- break ;
668
+ switch ( format ) {
671
669
672
- case RGBA_S3TC_DXT3_Format :
673
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . BC2RGBAUnormSRGB : GPUTextureFormat . BC2RGBAUnorm ;
674
- break ;
670
+ case RGBA_S3TC_DXT1_Format :
671
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . BC1RGBAUnormSRGB : GPUTextureFormat . BC1RGBAUnorm ;
672
+ break ;
675
673
676
- case RGBA_S3TC_DXT5_Format :
677
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . BC3RGBAUnormSRGB : GPUTextureFormat . BC3RGBAUnorm ;
678
- break ;
674
+ case RGBA_S3TC_DXT3_Format :
675
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . BC2RGBAUnormSRGB : GPUTextureFormat . BC2RGBAUnorm ;
676
+ break ;
679
677
680
- case RGB_ETC2_Format :
681
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ETC2RGB8UnormSRGB : GPUTextureFormat . ETC2RGB8Unorm ;
682
- break ;
678
+ case RGBA_S3TC_DXT5_Format :
679
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . BC3RGBAUnormSRGB : GPUTextureFormat . BC3RGBAUnorm ;
680
+ break ;
683
681
684
- case RGBA_ETC2_EAC_Format :
685
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ETC2RGBA8UnormSRGB : GPUTextureFormat . ETC2RGBA8Unorm ;
686
- break ;
682
+ case RGB_ETC2_Format :
683
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ETC2RGB8UnormSRGB : GPUTextureFormat . ETC2RGB8Unorm ;
684
+ break ;
687
685
688
- case RGBA_ASTC_4x4_Format :
689
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC4x4UnormSRGB : GPUTextureFormat . ASTC4x4Unorm ;
690
- break ;
686
+ case RGBA_ETC2_EAC_Format :
687
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ETC2RGBA8UnormSRGB : GPUTextureFormat . ETC2RGBA8Unorm ;
688
+ break ;
691
689
692
- case RGBA_ASTC_5x4_Format :
693
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC5x4UnormSRGB : GPUTextureFormat . ASTC5x4Unorm ;
694
- break ;
690
+ case RGBA_ASTC_4x4_Format :
691
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC4x4UnormSRGB : GPUTextureFormat . ASTC4x4Unorm ;
692
+ break ;
695
693
696
- case RGBA_ASTC_5x5_Format :
697
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC5x5UnormSRGB : GPUTextureFormat . ASTC5x5Unorm ;
698
- break ;
694
+ case RGBA_ASTC_5x4_Format :
695
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC5x4UnormSRGB : GPUTextureFormat . ASTC5x4Unorm ;
696
+ break ;
699
697
700
- case RGBA_ASTC_6x5_Format :
701
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC6x5UnormSRGB : GPUTextureFormat . ASTC6x5Unorm ;
702
- break ;
698
+ case RGBA_ASTC_5x5_Format :
699
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC5x5UnormSRGB : GPUTextureFormat . ASTC5x5Unorm ;
700
+ break ;
703
701
704
- case RGBA_ASTC_6x6_Format :
705
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC6x6UnormSRGB : GPUTextureFormat . ASTC6x6Unorm ;
706
- break ;
702
+ case RGBA_ASTC_6x5_Format :
703
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC6x5UnormSRGB : GPUTextureFormat . ASTC6x5Unorm ;
704
+ break ;
707
705
708
- case RGBA_ASTC_8x5_Format :
709
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC8x5UnormSRGB : GPUTextureFormat . ASTC8x5Unorm ;
710
- break ;
706
+ case RGBA_ASTC_6x6_Format :
707
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC6x6UnormSRGB : GPUTextureFormat . ASTC6x6Unorm ;
708
+ break ;
711
709
712
- case RGBA_ASTC_8x6_Format :
713
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC8x6UnormSRGB : GPUTextureFormat . ASTC8x6Unorm ;
714
- break ;
710
+ case RGBA_ASTC_8x5_Format :
711
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC8x5UnormSRGB : GPUTextureFormat . ASTC8x5Unorm ;
712
+ break ;
715
713
716
- case RGBA_ASTC_8x8_Format :
717
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC8x8UnormSRGB : GPUTextureFormat . ASTC8x8Unorm ;
718
- break ;
714
+ case RGBA_ASTC_8x6_Format :
715
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC8x6UnormSRGB : GPUTextureFormat . ASTC8x6Unorm ;
716
+ break ;
719
717
720
- case RGBA_ASTC_10x5_Format :
721
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC10x5UnormSRGB : GPUTextureFormat . ASTC10x5Unorm ;
722
- break ;
718
+ case RGBA_ASTC_8x8_Format :
719
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC8x8UnormSRGB : GPUTextureFormat . ASTC8x8Unorm ;
720
+ break ;
723
721
724
- case RGBA_ASTC_10x6_Format :
725
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC10x6UnormSRGB : GPUTextureFormat . ASTC10x6Unorm ;
726
- break ;
722
+ case RGBA_ASTC_10x5_Format :
723
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC10x5UnormSRGB : GPUTextureFormat . ASTC10x5Unorm ;
724
+ break ;
727
725
728
- case RGBA_ASTC_10x8_Format :
729
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC10x8UnormSRGB : GPUTextureFormat . ASTC10x8Unorm ;
730
- break ;
726
+ case RGBA_ASTC_10x6_Format :
727
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC10x6UnormSRGB : GPUTextureFormat . ASTC10x6Unorm ;
728
+ break ;
731
729
732
- case RGBA_ASTC_10x10_Format :
733
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC10x10UnormSRGB : GPUTextureFormat . ASTC10x10Unorm ;
734
- break ;
730
+ case RGBA_ASTC_10x8_Format :
731
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC10x8UnormSRGB : GPUTextureFormat . ASTC10x8Unorm ;
732
+ break ;
735
733
736
- case RGBA_ASTC_12x10_Format :
737
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC12x10UnormSRGB : GPUTextureFormat . ASTC12x10Unorm ;
738
- break ;
734
+ case RGBA_ASTC_10x10_Format :
735
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC10x10UnormSRGB : GPUTextureFormat . ASTC10x10Unorm ;
736
+ break ;
739
737
740
- case RGBA_ASTC_12x12_Format :
741
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC12x12UnormSRGB : GPUTextureFormat . ASTC12x12Unorm ;
742
- break ;
738
+ case RGBA_ASTC_12x10_Format :
739
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC12x10UnormSRGB : GPUTextureFormat . ASTC12x10Unorm ;
740
+ break ;
743
741
744
- default :
745
- console . error ( 'WebGPURenderer: Unsupported texture format.' , format ) ;
742
+ case RGBA_ASTC_12x12_Format :
743
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . ASTC12x12UnormSRGB : GPUTextureFormat . ASTC12x12Unorm ;
744
+ break ;
746
745
747
- }
746
+ default :
747
+ console . error ( 'WebGPURenderer: Unsupported texture format.' , format ) ;
748
748
749
- } else {
749
+ }
750
750
751
- switch ( format ) {
751
+ } else {
752
752
753
- case RGBAFormat :
753
+ switch ( format ) {
754
754
755
- switch ( type ) {
755
+ case RGBAFormat :
756
756
757
- case UnsignedByteType :
758
- formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . RGBA8UnormSRGB : GPUTextureFormat . RGBA8Unorm ;
759
- break ;
757
+ switch ( type ) {
760
758
761
- case HalfFloatType :
762
- formatGPU = GPUTextureFormat . RGBA16Float ;
763
- break ;
759
+ case UnsignedByteType :
760
+ formatGPU = ( colorSpace === SRGBColorSpace ) ? GPUTextureFormat . RGBA8UnormSRGB : GPUTextureFormat . RGBA8Unorm ;
761
+ break ;
764
762
765
- case FloatType :
766
- formatGPU = GPUTextureFormat . RGBA32Float ;
767
- break ;
763
+ case HalfFloatType :
764
+ formatGPU = GPUTextureFormat . RGBA16Float ;
765
+ break ;
768
766
769
- default :
770
- console . error ( 'WebGPURenderer: Unsupported texture type with RGBAFormat.' , type ) ;
767
+ case FloatType :
768
+ formatGPU = GPUTextureFormat . RGBA32Float ;
769
+ break ;
771
770
772
- }
771
+ default :
772
+ console . error ( 'WebGPURenderer: Unsupported texture type with RGBAFormat.' , type ) ;
773
773
774
- break ;
774
+ }
775
775
776
- case RedFormat :
776
+ break ;
777
777
778
- switch ( type ) {
778
+ case RedFormat :
779
779
780
- case UnsignedByteType :
781
- formatGPU = GPUTextureFormat . R8Unorm ;
782
- break ;
780
+ switch ( type ) {
783
781
784
- case HalfFloatType :
785
- formatGPU = GPUTextureFormat . R16Float ;
786
- break ;
782
+ case UnsignedByteType :
783
+ formatGPU = GPUTextureFormat . R8Unorm ;
784
+ break ;
787
785
788
- case FloatType :
789
- formatGPU = GPUTextureFormat . R32Float ;
790
- break ;
786
+ case HalfFloatType :
787
+ formatGPU = GPUTextureFormat . R16Float ;
788
+ break ;
791
789
792
- default :
793
- console . error ( 'WebGPURenderer: Unsupported texture type with RedFormat.' , type ) ;
790
+ case FloatType :
791
+ formatGPU = GPUTextureFormat . R32Float ;
792
+ break ;
794
793
795
- }
794
+ default :
795
+ console . error ( 'WebGPURenderer: Unsupported texture type with RedFormat.' , type ) ;
796
796
797
- break ;
797
+ }
798
798
799
- case RGFormat :
799
+ break ;
800
800
801
- switch ( type ) {
801
+ case RGFormat :
802
802
803
- case UnsignedByteType :
804
- formatGPU = GPUTextureFormat . RG8Unorm ;
805
- break ;
803
+ switch ( type ) {
806
804
807
- case HalfFloatType :
808
- formatGPU = GPUTextureFormat . RG16Float ;
809
- break ;
805
+ case UnsignedByteType :
806
+ formatGPU = GPUTextureFormat . RG8Unorm ;
807
+ break ;
810
808
811
- case FloatType :
812
- formatGPU = GPUTextureFormat . RG32Float ;
813
- break ;
809
+ case HalfFloatType :
810
+ formatGPU = GPUTextureFormat . RG16Float ;
811
+ break ;
814
812
815
- default :
816
- console . error ( 'WebGPURenderer: Unsupported texture type with RGFormat.' , type ) ;
813
+ case FloatType :
814
+ formatGPU = GPUTextureFormat . RG32Float ;
815
+ break ;
817
816
818
- }
817
+ default :
818
+ console . error ( 'WebGPURenderer: Unsupported texture type with RGFormat.' , type ) ;
819
819
820
- break ;
820
+ }
821
821
822
- case DepthFormat :
822
+ break ;
823
823
824
- switch ( type ) {
824
+ case DepthFormat :
825
825
826
- case UnsignedShortType :
827
- formatGPU = GPUTextureFormat . Depth16Unorm ;
828
- break ;
826
+ switch ( type ) {
829
827
830
- case UnsignedIntType :
831
- formatGPU = GPUTextureFormat . Depth24Plus ;
832
- break ;
828
+ case UnsignedShortType :
829
+ formatGPU = GPUTextureFormat . Depth16Unorm ;
830
+ break ;
833
831
834
- case FloatType :
835
- formatGPU = GPUTextureFormat . Depth32Float ;
836
- break ;
832
+ case UnsignedIntType :
833
+ formatGPU = GPUTextureFormat . Depth24Plus ;
834
+ break ;
837
835
838
- default :
839
- console . error ( 'WebGPURenderer: Unsupported texture type with DepthFormat.' , type ) ;
836
+ case FloatType :
837
+ formatGPU = GPUTextureFormat . Depth32Float ;
838
+ break ;
840
839
841
- }
840
+ default :
841
+ console . error ( 'WebGPURenderer: Unsupported texture type with DepthFormat.' , type ) ;
842
842
843
- break ;
843
+ }
844
844
845
- case DepthStencilFormat :
845
+ break ;
846
846
847
- switch ( type ) {
847
+ case DepthStencilFormat :
848
848
849
- case UnsignedInt248Type :
850
- formatGPU = GPUTextureFormat . Depth24PlusStencil8 ;
851
- break ;
849
+ switch ( type ) {
852
850
853
- case FloatType :
851
+ case UnsignedInt248Type :
852
+ formatGPU = GPUTextureFormat . Depth24PlusStencil8 ;
853
+ break ;
854
854
855
- if ( this . device . features . has ( GPUFeatureName . Depth32FloatStencil8 ) === false ) {
855
+ case FloatType :
856
856
857
- console . error ( 'WebGPURenderer: Depth textures with DepthStencilFormat + FloatType can only be used with the "depth32float-stencil8" GPU feature.' ) ;
857
+ if ( device && device . features . has ( GPUFeatureName . Depth32FloatStencil8 ) === false ) {
858
858
859
- }
859
+ console . error ( 'WebGPURenderer: Depth textures with DepthStencilFormat + FloatType can only be used with the "depth32float-stencil8" GPU feature.' ) ;
860
860
861
- formatGPU = GPUTextureFormat . Depth32FloatStencil8 ;
861
+ }
862
862
863
- break ;
863
+ formatGPU = GPUTextureFormat . Depth32FloatStencil8 ;
864
864
865
- default :
866
- console . error ( 'WebGPURenderer: Unsupported texture type with DepthStencilFormat.' , type ) ;
865
+ break ;
867
866
868
- }
867
+ default :
868
+ console . error ( 'WebGPURenderer: Unsupported texture type with DepthStencilFormat.' , type ) ;
869
869
870
- break ;
870
+ }
871
871
872
- default :
873
- console . error ( 'WebGPURenderer: Unsupported texture format.' , format ) ;
872
+ break ;
874
873
875
- }
874
+ default :
875
+ console . error ( 'WebGPURenderer: Unsupported texture format.' , format ) ;
876
876
877
877
}
878
878
879
- return formatGPU ;
880
-
881
879
}
882
880
881
+ return formatGPU ;
882
+
883
883
}
884
884
885
885
export default WebGPUTextureUtils ;
0 commit comments