/// <summary> /// Constructs the <see cref="VectorComponent"/>. /// </summary> /// <param name="type">The type of data that will be stored in the component.</param> /// <param name="channel">The channel type that the component represents.</param> /// <param name="bits">The size of the component in bits.</param> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="bits"/> is less than or equal to zero or /// <paramref name="bits"/> is zero and <paramref name="type"/> is <see cref="VectorComponentType.BitField"/>. /// </exception> public VectorComponent(VectorComponentType type, VectorComponentChannel channel, int?bits = null) { if (bits.HasValue) { if (bits.Value <= 0) { throw new ArgumentOutOfRangeException(nameof(bits)); } Bits = bits.Value; } else { if (type == VectorComponentType.BitField || type == VectorComponentType.Undefined) { throw new ArgumentException( "The bit size could not be inferred from the given component type."); } Bits = type.SizeInBytes() * 8; } Type = type; Channel = channel; }
public static int SizeInBytes(this VectorComponentType type) { return(type switch { VectorComponentType.Int8 => sizeof(sbyte), VectorComponentType.UInt8 => sizeof(byte), VectorComponentType.Int16 => sizeof(short), VectorComponentType.UInt16 => sizeof(ushort), VectorComponentType.Int32 => sizeof(int), VectorComponentType.UInt32 => sizeof(uint), VectorComponentType.Int64 => sizeof(long), VectorComponentType.UInt64 => sizeof(ulong), VectorComponentType.Float16 => Unsafe.SizeOf <HalfSingle>(), VectorComponentType.Float32 => sizeof(float), VectorComponentType.Float64 => sizeof(double), _ => throw new ArgumentOutOfRangeException(nameof(type)), });