1- import { GPUIndexFormat , GPUCompareFunction , GPUFrontFace , GPUCullMode , GPUVertexFormat , GPUBlendFactor , GPUBlendOperation , BlendColorFactor , OneMinusBlendColorFactor , GPUColorWriteFlags , GPUStencilOperation , GPUInputStepMode } from './constants.js' ;
1+ import { GPUIndexFormat , GPUCompareFunction , GPUFrontFace , GPUCullMode , GPUBlendFactor , GPUBlendOperation , BlendColorFactor , OneMinusBlendColorFactor , GPUColorWriteFlags , GPUStencilOperation , GPUInputStepMode } from './constants.js' ;
22import {
3+ Float16BufferAttribute ,
34 FrontSide , BackSide , DoubleSide ,
45 NeverDepth , AlwaysDepth , LessDepth , LessEqualDepth , EqualDepth , GreaterEqualDepth , GreaterDepth , NotEqualDepth ,
56 NeverStencilFunc , AlwaysStencilFunc , LessStencilFunc , LessEqualStencilFunc , EqualStencilFunc , GreaterEqualStencilFunc , GreaterStencilFunc , NotEqualStencilFunc ,
@@ -9,6 +10,26 @@ import {
910 ZeroFactor , OneFactor , SrcColorFactor , OneMinusSrcColorFactor , SrcAlphaFactor , OneMinusSrcAlphaFactor , DstAlphaFactor , OneMinusDstAlphaFactor , DstColorFactor , OneMinusDstColorFactor , SrcAlphaSaturateFactor
1011} from 'three' ;
1112
13+ const typedArraysToVertexFormatPrefix = new Map ( [
14+ [ Int8Array , [ 'sint8' , 'snorm8' ] ] ,
15+ [ Uint8Array , [ 'uint8' , 'unorm8' ] ] ,
16+ [ Int16Array , [ 'sint16' , 'snorm16' ] ] ,
17+ [ Uint16Array , [ 'uint16' , 'unorm16' ] ] ,
18+ [ Int32Array , [ 'sint32' , 'snorm32' ] ] ,
19+ [ Uint32Array , [ 'uint32' , 'unorm32' ] ] ,
20+ [ Float32Array , [ 'float32' , ] ] ,
21+ ] ) ;
22+
23+ const typedAttributeToVertexFormatPrefix = new Map ( [
24+ [ Float16BufferAttribute , [ 'float16' , ] ] ,
25+ ] ) ;
26+
27+ const typeArraysToVertexFormatPrefixForItemSize1 = new Map ( [
28+ [ Int32Array , 'sint32' ] ,
29+ [ Uint32Array , 'uint32' ] ,
30+ [ Float32Array , 'float32' ]
31+ ] ) ;
32+
1233class WebGPURenderPipeline {
1334
1435 constructor ( device , utils ) {
@@ -561,127 +582,48 @@ class WebGPURenderPipeline {
561582
562583 }
563584
564- _getVertexFormat ( type , bytesPerElement ) {
565-
566- // float
567-
568- if ( type === 'float' ) return GPUVertexFormat . Float32 ;
569-
570- if ( type === 'vec2' ) {
571-
572- if ( bytesPerElement === 2 ) {
573-
574- return GPUVertexFormat . Float16x2 ;
575-
576- } else {
577-
578- return GPUVertexFormat . Float32x2 ;
579-
580- }
581-
582- }
583-
584- if ( type === 'vec3' ) return GPUVertexFormat . Float32x3 ;
585-
586- if ( type === 'vec4' ) {
587-
588- if ( bytesPerElement === 2 ) {
589-
590- return GPUVertexFormat . Float16x4 ;
591-
592- } else {
593-
594- return GPUVertexFormat . Float32x4 ;
595-
596- }
597-
598- }
599-
600- // int
601-
602- if ( type === 'int' ) return GPUVertexFormat . Sint32 ;
603-
604- if ( type === 'ivec2' ) {
605-
606- if ( bytesPerElement === 1 ) {
607-
608- return GPUVertexFormat . Sint8x2 ;
609-
610- } else if ( bytesPerElement === 2 ) {
585+ _getVertexFormat ( geometryAttribute ) {
611586
612- return GPUVertexFormat . Sint16x2 ;
587+ const { itemSize, normalized } = geometryAttribute ;
588+ const ArrayType = geometryAttribute . array . constructor ;
589+ const AttributeType = geometryAttribute . constructor ;
613590
614- } else {
591+ let format ;
615592
616- return GPUVertexFormat . Sint32x2 ;
593+ if ( itemSize == 1 ) {
617594
618- }
619-
620- }
621-
622- if ( type === 'ivec3' ) return GPUVertexFormat . Sint32x3 ;
623-
624- if ( type === 'ivec4' ) {
625-
626- if ( bytesPerElement === 1 ) {
627-
628- return GPUVertexFormat . Sint8x4 ;
629-
630- } else if ( bytesPerElement === 2 ) {
631-
632- return GPUVertexFormat . Sint16x4 ;
633-
634- } else {
635-
636- return GPUVertexFormat . Sint32x4 ;
637-
638- }
639-
640- }
641-
642- // uint
595+ format = typeArraysToVertexFormatPrefixForItemSize1 . get ( ArrayType ) ;
643596
644- if ( type === 'uint' ) return GPUVertexFormat . Uint32 ;
645-
646- if ( type === 'uvec2' ) {
597+ } else {
647598
648- if ( bytesPerElement === 1 ) {
599+ const prefixOptions = typedAttributeToVertexFormatPrefix . get ( AttributeType ) || typedArraysToVertexFormatPrefix . get ( ArrayType ) ;
600+ const prefix = prefixOptions [ normalized ? 1 : 0 ] ;
601+ console . log ( prefix ) ;
602+ if ( prefix ) {
649603
650- return GPUVertexFormat . Uint8x2 ;
604+ const bytesPerUnit = ArrayType . BYTES_PER_ELEMENT * itemSize ;
605+ const paddedBytesPerUnit = Math . floor ( ( bytesPerUnit + 3 ) / 4 ) * 4 ;
606+ const paddedItemSize = paddedBytesPerUnit / ArrayType . BYTES_PER_ELEMENT ;
651607
652- } else if ( bytesPerElement === 2 ) {
608+ if ( paddedItemSize % 1 ) {
653609
654- return GPUVertexFormat . Uint16x2 ;
610+ throw new Error ( `bad item size ` ) ;
655611
656- } else {
612+ }
657613
658- return GPUVertexFormat . Uint32x2 ;
614+ format = ` ${ prefix } x ${ paddedItemSize } ` ;
659615
660616 }
661617
662618 }
663619
664- if ( type === 'uvec3' ) return GPUVertexFormat . Uint32x3 ;
665-
666- if ( type === 'uvec4' ) {
667-
668- if ( bytesPerElement === 1 ) {
620+ if ( ! format ) {
669621
670- return GPUVertexFormat . Uint8x4 ;
671-
672- } else if ( bytesPerElement === 2 ) {
673-
674- return GPUVertexFormat . Uint16x4 ;
675-
676- } else {
677-
678- return GPUVertexFormat . Uint32x4 ;
679-
680- }
622+ console . error ( 'THREE.WebGPURenderer: Shader variable type not supported yet.' ) ;
681623
682624 }
683625
684- console . error ( 'THREE.WebGPURenderer: Shader variable type not supported yet.' , type ) ;
626+ return format ;
685627
686628 }
687629
@@ -693,14 +635,12 @@ class WebGPURenderPipeline {
693635 for ( let slot = 0 ; slot < nodeAttributes . length ; slot ++ ) {
694636
695637 const nodeAttribute = nodeAttributes [ slot ] ;
696-
697638 const name = nodeAttribute . name ;
698- const type = nodeAttribute . type ;
699639
700640 const geometryAttribute = geometry . getAttribute ( name ) ;
701641 const bytesPerElement = geometryAttribute . array . BYTES_PER_ELEMENT ;
702642
703- const format = this . _getVertexFormat ( type , bytesPerElement ) ;
643+ const format = this . _getVertexFormat ( geometryAttribute ) ;
704644
705645 let arrayStride = geometryAttribute . itemSize * bytesPerElement ;
706646 let offset = 0 ;
0 commit comments