private static bool _CheckWeightSum(Span <byte> dst, ENCODING encoding) { if (encoding == ENCODING.UNSIGNED_BYTE) { var weights = dst; int r = 0; for (int j = 0; j < weights.Length; ++j) { r += weights[j]; } return(r == 255); } if (encoding == ENCODING.UNSIGNED_SHORT) { var weights = System.Runtime.InteropServices.MemoryMarshal.Cast <Byte, UInt16>(dst); int r = 0; for (int j = 0; j < weights.Length; ++j) { r += weights[j]; } return(r == 65535); } if (encoding == ENCODING.FLOAT) { var weights = System.Runtime.InteropServices.MemoryMarshal.Cast <Byte, Single>(dst); float nonZero = 0; float sum = 0; for (int j = 0; j < weights.Length; ++j) { var w = weights[j]; if (float.IsNaN(w)) { return(false); } if (w < 0 || w > 1) { return(false); } if (w > 0) { sum += w; nonZero += 1; } } float err = 2e-7f * nonZero; return(Math.Abs(sum - 1) <= err); } return(false); }
public MemoryAccessInfo(string name, int byteOffset, int itemsCount, int byteStride, DIMENSIONS dimensions, ENCODING encoding = ENCODING.FLOAT, Boolean normalized = false) { this.Name = name; this.ByteOffset = byteOffset; this.ItemsCount = itemsCount; this.ByteStride = byteStride; this.Dimensions = dimensions; this.Encoding = encoding; this.Normalized = normalized; }
/// <summary> /// Initializes a new instance of the <see cref="ColorArray"/> struct. /// </summary> /// <param name="source">The array to wrap.</param> /// <param name="byteOffset">The zero-based index of the first <see cref="Byte"/> in <paramref name="source"/>.</param> /// <param name="itemsCount">The number of <see cref="Vector4"/> items in <paramref name="source"/>.</param> /// <param name="byteStride"> /// The byte stride between elements. /// If the value is zero, the size of the item is used instead. /// </param> /// <param name="dimensions">The number of elements per item. Currently only values 3 and 4 are supported.</param> /// <param name="encoding">A value of <see cref="ENCODING"/>.</param> /// <param name="normalized">True if values are normalized.</param> /// <param name="defaultW">If <paramref name="dimensions"/> is 3, the W values are filled with this value</param> public ColorArray(BYTES source, int byteOffset, int itemsCount, int byteStride, int dimensions = 4, ENCODING encoding = ENCODING.FLOAT, Boolean normalized = false, Single defaultW = 1) { Guard.MustBeBetweenOrEqualTo(dimensions, 3, 4, nameof(dimensions)); _Accessor = new FloatingAccessor(source, byteOffset, itemsCount, byteStride, dimensions, encoding, normalized); _Dimensions = dimensions; _DefaultW = defaultW; }
/// <summary> /// Initializes a new instance of the <see cref="ColorArray"/> struct. /// </summary> /// <param name="source">The array to wrap.</param> /// <param name="byteStride"> /// The byte stride between elements. /// If the value is zero, the size of the item is used instead. /// </param> /// <param name="dimensions">The number of elements per item. Currently only values 3 and 4 are supported.</param> /// <param name="encoding">A value of <see cref="ENCODING"/>.</param> /// <param name="normalized">True if values are normalized.</param> /// <param name="defaultW">If <paramref name="dimensions"/> is 3, the W values are filled with this value</param> public ColorArray(BYTES source, int byteStride = 0, int dimensions = 4, ENCODING encoding = ENCODING.FLOAT, Boolean normalized = false, Single defaultW = 1) : this(source, 0, int.MaxValue, byteStride, dimensions, encoding, normalized, defaultW) { }
public static MemoryAccessor CreateIndexMemoryAccessor(this IReadOnlyList <Int32> indices, ENCODING indexEncoding) { if (indices == null || indices.Count == 0) { return(null); } var attribute = new MemoryAccessInfo("INDEX", 0, indices.Count, 0, DIMENSIONS.SCALAR, indexEncoding); // create buffer var ibytes = new Byte[indexEncoding.ByteLength() * indices.Count]; var ibuffer = new ArraySegment <byte>(ibytes); // fill the buffer with indices. var accessor = new MemoryAccessor(ibuffer, attribute.Slice(0, indices.Count)); accessor.AsIntegerArray().Fill(indices); return(accessor); }
public FloatingAccessor(BYTES source, int byteOffset, int itemsCount, int byteStride, int dimensions, ENCODING encoding, Boolean normalized) { var enclen = encoding.ByteLength(); this._Data = source.Slice(byteOffset); this._Getter = null; this._Setter = null; this._ByteStride = Math.Max(byteStride, enclen * dimensions); this._EncodedLen = enclen; this._ItemCount = this._Data.Length / this._ByteStride; // strided buffers require 4 byte word padding. if ((_Data.Length % _ByteStride) >= enclen * dimensions) { ++_ItemCount; } _ItemCount = Math.Min(itemsCount, _ItemCount); if (encoding == ENCODING.FLOAT) { this._Setter = this._SetValue <Single>; this._Getter = this._GetValue <Single>; return; } if (normalized) { switch (encoding) { case ENCODING.BYTE: { this._Setter = this._SetNormalizedS8; this._Getter = this._GetNormalizedS8; break; } case ENCODING.UNSIGNED_BYTE: { this._Setter = this._SetNormalizedU8; this._Getter = this._GetNormalizedU8; break; } case ENCODING.SHORT: { this._Setter = this._SetNormalizedS16; this._Getter = this._GetNormalizedS16; break; } case ENCODING.UNSIGNED_SHORT: { this._Setter = this._SetNormalizedU16; this._Getter = this._GetNormalizedU16; break; } default: throw new ArgumentException(ERR_UNSUPPORTEDENCODING, nameof(encoding)); } } else { switch (encoding) { case ENCODING.BYTE: { this._Setter = this._SetValueS8; this._Getter = this._GetValueS8; break; } case ENCODING.UNSIGNED_BYTE: { this._Setter = this._SetValueU8; this._Getter = this._GetValueU8; break; } case ENCODING.SHORT: { this._Setter = this._SetValueS16; this._Getter = this._GetValueS16; break; } case ENCODING.UNSIGNED_SHORT: { this._Setter = this._SetValueU16; this._Getter = this._GetValueU16; break; } case ENCODING.UNSIGNED_INT: { this._Setter = this._SetValueU32; this._Getter = this._GetValueU32; break; } case ENCODING.FLOAT: break; default: throw new ArgumentException("Unsupported encoding.", nameof(encoding)); } } }
/// <summary> /// Initializes a new instance of the <see cref="ColorArray"/> struct. /// </summary> /// <param name="source">The array to wrap.</param> /// <param name="byteOffset">The zero-based index of the first <see cref="Byte"/> in <paramref name="source"/>.</param> /// <param name="itemsCount">The number of <see cref="Vector4"/> items in <paramref name="source"/>.</param> /// <param name="byteStride"> /// The byte stride between elements. /// If the value is zero, the size of the item is used instead. /// </param> /// <param name="dimensions">The number of elements per item. Currently only values 3 and 4 are supported.</param> /// <param name="encoding">A value of <see cref="ENCODING"/>.</param> /// <param name="normalized">True if values are normalized.</param> /// <param name="defaultW">If <paramref name="dimensions"/> is 3, the W values are filled with this value</param> public ColorArray(Byte[] source, int byteOffset, int itemsCount, int byteStride, int dimensions = 4, ENCODING encoding = ENCODING.FLOAT, Boolean normalized = false, Single defaultW = 1) : this(new BYTES(source), byteOffset, itemsCount, byteStride, dimensions, encoding, normalized, defaultW) { }