src/AnvilPacker/Level/Physics/VoxelShape.cs
/// <summary> Rasterizes the specified axis plane of the box to a 16x16 bitmap. </summary>
Rasterizes the specified axis plane of the box to a 16x16 bitmap.
Rasterizes the specified axis plane of the box to a 16x16 bitmap .
private static void RasterizePlane(Box8 box, ushort* bmp, Axis axis)
        {
            var (x1, y1, x2, y2) = axis switch {
                Axis.X => (box.MinZ, box.MinY, box.MaxZ, box.MaxY),
                Axis.Y => (box.MinX, box.MinZ, box.MaxX, box.MaxZ),
                Axis.Z => (box.MinX, box.MinY, box.MaxX, box.MaxY),
                _ => throw new InvalidOperationException()
            };
            x1 = Clamp16(x1);
            y1 = Clamp16(y1);
            x2 = Clamp16(x2);
            y2 = Clamp16(y2);
            var rowMask = (ushort)Maths.CreateRangeMask(x1, x2);
            if (Avx2.IsSupported) {
                Rasterize_AVX2(bmp, y1, y2, rowMask);
            } else {
                Rasterize_Scalar(bmp, y1, y2, rowMask);
            }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static void Rasterize_Scalar(ushort* bmp, int y1, int y2, ushort rowMask)
            {
                while (y2 - y1 >= 4) {
                    *(ulong*)&bmp[y1] |= (ulong)rowMask * 0x0001_0001_0001_0001ul;
                    y1 += 4;
                }
                while (y1 < y2) {
                    bmp[y1++] |= rowMask;
                }
            }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static void Rasterize_AVX2(ushort* bmp, int y1, int y2, ushort rowMask)
            {
                var colIdx = Vector256.Create(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
                var colMask = Avx2.AndNot(
                    Avx2.CompareGreaterThan(Vector256.Create((short)y1), colIdx),
                    Avx2.CompareGreaterThan(Vector256.Create((short)y2), colIdx)
                ).AsUInt16();
                var col = Avx2.LoadVector256(&bmp[0]);
                var row = Vector256.Create(rowMask);
                col = Avx2.Or(col, Avx2.And(row, colMask));
                Avx2.Store(&bmp[0], col);
            }
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            static sbyte Clamp16(sbyte x)
            {
                return (sbyte)(x < 0 ? 0 : x > 16 ? 16 : x);
            }
        }
private static void RasterizePlane ( Box8 box , ushort * bmp , Axis axis ) { var ( x1 , y1 , x2 , y2 ) = axis switch { Axis . X => ( box . MinZ , box . MinY , box . MaxZ , box . MaxY ) , Axis . Y => ( box . MinX , box . MinZ , box . MaxX , box . MaxZ ) , Axis . Z => ( box . MinX , box . MinY , box . MaxX , box . MaxY ) , _ => throw new InvalidOperationException ( ) } ; x1 = Clamp16 ( x1 ) ; y1 = Clamp16 ( y1 ) ; x2 = Clamp16 ( x2 ) ; y2 = Clamp16 ( y2 ) ; var rowMask = ( ushort ) Maths . CreateRangeMask ( x1 , x2 ) ; if ( Avx2 . IsSupported ) { Rasterize_AVX2 ( bmp , y1 , y2 , rowMask ) ; } else { Rasterize_Scalar ( bmp , y1 , y2 , rowMask ) ; } [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ] static void Rasterize_Scalar ( ushort * bmp , int y1 , int y2 , ushort rowMask ) { while ( y2 - y1 >= 4 ) { * ( ulong * ) & bmp [ y1 ] |= ( ulong ) rowMask * 0x0001_0001_0001_0001ul ; y1 += 4 ; } while ( y1 < y2 ) { bmp [ y1 ++ ] |= rowMask ; } } [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ] static void Rasterize_AVX2 ( ushort * bmp , int y1 , int y2 , ushort rowMask ) { var colIdx = Vector256 . Create ( 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 ) ; var colMask = Avx2 . AndNot ( Avx2 . CompareGreaterThan ( Vector256 . Create ( ( short ) y1 ) , colIdx ) , Avx2 . CompareGreaterThan ( Vector256 . Create ( ( short ) y2 ) , colIdx ) ) . AsUInt16 ( ) ; var col = Avx2 . LoadVector256 ( & bmp [ 0 ] ) ; var row = Vector256 . Create ( rowMask ) ; col = Avx2 . Or ( col , Avx2 . And ( row , colMask ) ) ; Avx2 . Store ( & bmp [ 0 ] , col ) ; } [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ] static sbyte Clamp16 ( sbyte x ) { return ( sbyte ) ( x < 0 ? 0 : x > 16 ? 16 : x ) ; } }
Rasterizes the specified axis plane of the box to a 16x16 bitmap.
Rasterizes the specified axis plane of the box to a 16x16 bitmap .
//pick plane coords //draw to the bitmap //colMask = set ~0 words in range [y1..y2] //&bmp[0] |= broadcast((ushort)rowMask) & colMask;
box
Box8
bmp
ushort*
axis
Axis