11using Bgfx ;
2+ using Staple . Internal ;
23using Staple . Utilities ;
34using System ;
45using System . Collections . Generic ;
56using System . Numerics ;
7+ using System . Runtime . InteropServices ;
68using System . Text ;
79
810namespace Staple ;
@@ -607,17 +609,24 @@ internal byte[] MakeVertexDataBlob(VertexLayout layout)
607609
608610 var buffer = new byte [ size ] ;
609611
610- void Copy ( byte [ ] source , ref int index )
612+ void Copy < T > ( T source , ref int index ) where T : unmanaged
611613 {
612- if ( index + source . Length > buffer . Length )
614+ var sourceSize = TypeCache . SizeOf ( source . GetType ( ) . FullName ) ;
615+
616+ if ( index + sourceSize > buffer . Length )
613617 {
614- throw new InvalidOperationException ( $ "[Mesh] Buffer Overrun while generating vertex data blob: { index } -> { index + source . Length } "
618+ throw new InvalidOperationException ( $ "[Mesh] Buffer Overrun while generating vertex data blob: { index } -> { index + sourceSize } "
615619 + $ "is larger than buffer { buffer . Length } ") ;
616620 }
617621
618- Buffer . BlockCopy ( source , 0 , buffer , index , source . Length ) ;
622+ unsafe
623+ {
624+ byte * src = ( byte * ) & source ;
625+
626+ Marshal . Copy ( ( nint ) src , buffer , index , sourceSize ) ;
627+ }
619628
620- index += source . Length ;
629+ index += sourceSize ;
621630 }
622631
623632 for ( int i = 0 , index = 0 ; i < vertices . Length ; i ++ )
@@ -627,112 +636,82 @@ void Copy(byte[] source, ref int index)
627636 throw new InvalidOperationException ( "[Mesh] Exceeded expected byte count while generating vertex data blob" ) ;
628637 }
629638
630- //Copy position
631- Copy ( BitConverter . GetBytes ( vertices [ i ] . X ) , ref index ) ;
632- Copy ( BitConverter . GetBytes ( vertices [ i ] . Y ) , ref index ) ;
633- Copy ( BitConverter . GetBytes ( vertices [ i ] . Z ) , ref index ) ;
639+ Copy ( vertices [ i ] , ref index ) ;
634640
635- //Copy normals
636641 if ( HasNormals )
637642 {
638- Copy ( BitConverter . GetBytes ( normals [ i ] . X ) , ref index ) ;
639- Copy ( BitConverter . GetBytes ( normals [ i ] . Y ) , ref index ) ;
640- Copy ( BitConverter . GetBytes ( normals [ i ] . Z ) , ref index ) ;
643+ Copy ( normals [ i ] , ref index ) ;
641644 }
642645
643646 if ( HasTangents )
644647 {
645- Copy ( BitConverter . GetBytes ( tangents [ i ] . X ) , ref index ) ;
646- Copy ( BitConverter . GetBytes ( tangents [ i ] . Y ) , ref index ) ;
647- Copy ( BitConverter . GetBytes ( tangents [ i ] . Z ) , ref index ) ;
648+ Copy ( tangents [ i ] , ref index ) ;
648649 }
649650
650651 if ( HasBitangents )
651652 {
652- Copy ( BitConverter . GetBytes ( bitangents [ i ] . X ) , ref index ) ;
653- Copy ( BitConverter . GetBytes ( bitangents [ i ] . Y ) , ref index ) ;
654- Copy ( BitConverter . GetBytes ( bitangents [ i ] . Z ) , ref index ) ;
653+ Copy ( bitangents [ i ] , ref index ) ;
655654 }
656655
657656 if ( HasColors )
658657 {
659- Copy ( BitConverter . GetBytes ( colors [ i ] . r ) , ref index ) ;
660- Copy ( BitConverter . GetBytes ( colors [ i ] . g ) , ref index ) ;
661- Copy ( BitConverter . GetBytes ( colors [ i ] . b ) , ref index ) ;
662- Copy ( BitConverter . GetBytes ( colors [ i ] . a ) , ref index ) ;
658+ Copy ( colors [ i ] , ref index ) ;
663659 }
664660 else if ( HasColors32 )
665661 {
666662 var c = ( Color ) colors32 [ i ] ;
667663
668- Copy ( BitConverter . GetBytes ( c . r ) , ref index ) ;
669- Copy ( BitConverter . GetBytes ( c . g ) , ref index ) ;
670- Copy ( BitConverter . GetBytes ( c . b ) , ref index ) ;
671- Copy ( BitConverter . GetBytes ( c . a ) , ref index ) ;
664+ Copy ( c , ref index ) ;
672665 }
673666
674667 if ( HasUV )
675668 {
676- Copy ( BitConverter . GetBytes ( uv [ i ] . X ) , ref index ) ;
677- Copy ( BitConverter . GetBytes ( uv [ i ] . Y ) , ref index ) ;
669+ Copy ( uv [ i ] , ref index ) ;
678670 }
679671
680672 if ( HasUV2 )
681673 {
682- Copy ( BitConverter . GetBytes ( uv2 [ i ] . X ) , ref index ) ;
683- Copy ( BitConverter . GetBytes ( uv2 [ i ] . Y ) , ref index ) ;
674+ Copy ( uv2 [ i ] , ref index ) ;
684675 }
685676
686677 if ( HasUV3 )
687678 {
688- Copy ( BitConverter . GetBytes ( uv3 [ i ] . X ) , ref index ) ;
689- Copy ( BitConverter . GetBytes ( uv3 [ i ] . Y ) , ref index ) ;
679+ Copy ( uv3 [ i ] , ref index ) ;
690680 }
691681
692682 if ( HasUV4 )
693683 {
694- Copy ( BitConverter . GetBytes ( uv4 [ i ] . X ) , ref index ) ;
695- Copy ( BitConverter . GetBytes ( uv4 [ i ] . Y ) , ref index ) ;
684+ Copy ( uv4 [ i ] , ref index ) ;
696685 }
697686
698687 if ( HasUV5 )
699688 {
700- Copy ( BitConverter . GetBytes ( uv5 [ i ] . X ) , ref index ) ;
701- Copy ( BitConverter . GetBytes ( uv5 [ i ] . Y ) , ref index ) ;
689+ Copy ( uv5 [ i ] , ref index ) ;
702690 }
703691
704692 if ( HasUV6 )
705693 {
706- Copy ( BitConverter . GetBytes ( uv6 [ i ] . X ) , ref index ) ;
707- Copy ( BitConverter . GetBytes ( uv6 [ i ] . Y ) , ref index ) ;
694+ Copy ( uv6 [ i ] , ref index ) ;
708695 }
709696
710697 if ( HasUV7 )
711698 {
712- Copy ( BitConverter . GetBytes ( uv7 [ i ] . X ) , ref index ) ;
713- Copy ( BitConverter . GetBytes ( uv7 [ i ] . Y ) , ref index ) ;
699+ Copy ( uv7 [ i ] , ref index ) ;
714700 }
715701
716702 if ( HasUV8 )
717703 {
718- Copy ( BitConverter . GetBytes ( uv8 [ i ] . X ) , ref index ) ;
719- Copy ( BitConverter . GetBytes ( uv8 [ i ] . Y ) , ref index ) ;
704+ Copy ( uv8 [ i ] , ref index ) ;
720705 }
721706
722707 if ( HasBoneIndices )
723708 {
724- Copy ( BitConverter . GetBytes ( boneIndices [ i ] . X ) , ref index ) ;
725- Copy ( BitConverter . GetBytes ( boneIndices [ i ] . Y ) , ref index ) ;
726- Copy ( BitConverter . GetBytes ( boneIndices [ i ] . Z ) , ref index ) ;
727- Copy ( BitConverter . GetBytes ( boneIndices [ i ] . W ) , ref index ) ;
709+ Copy ( boneIndices [ i ] , ref index ) ;
728710 }
729711
730712 if ( HasBoneWeights )
731713 {
732- Copy ( BitConverter . GetBytes ( boneWeights [ i ] . X ) , ref index ) ;
733- Copy ( BitConverter . GetBytes ( boneWeights [ i ] . Y ) , ref index ) ;
734- Copy ( BitConverter . GetBytes ( boneWeights [ i ] . Z ) , ref index ) ;
735- Copy ( BitConverter . GetBytes ( boneWeights [ i ] . W ) , ref index ) ;
714+ Copy ( boneWeights [ i ] , ref index ) ;
736715 }
737716 }
738717
0 commit comments