public static Vertices <TVertexType> CreateSingleElementVertices(DeviceContext context, TVertexType[] data, DeclarationUsage elementUsage, int index) { if (typeof(float) != typeof(TVertexType) && typeof(Vector2) != typeof(TVertexType) && typeof(Vector3) != typeof(TVertexType) && typeof(Vector4) != typeof(TVertexType) && typeof(Half2) != typeof(TVertexType) && typeof(Half4) != typeof(TVertexType)) { throw new ArgumentException("Only float and vector types are supported for single element vertex buffers"); } if (data == null) { throw new ArgumentNullException(); } if (index >= 16 || index < 0) { throw new ArgumentException("index"); } DeclarationType format = VertexDeclarationBuilder.DetermineFormat(typeof(TVertexType)); VertexElement[] elements = new VertexElement[] { new VertexElement(0, 0, format, DeclarationMethod.Default, elementUsage, (byte)index) }; int stride = VertexElementAttribute.CalculateVertexStride(elements); return(new Vertices <TVertexType>(context, data, elements, stride)); }
public static int CalculateVertexStride(VertexElement[] elements) { int stride = 0; for (int i = 0; i < elements.Length; i++) { stride = Math.Max(stride, elements[i].Offset + VertexElementAttribute.SizeOfFormatType(elements[i].Type)); } return(stride); }
public static Vertices <TVertexType> CreateRawDataVertices(DeviceContext context, TVertexType[] data, VertexElement[] elements) { if (typeof(byte) != typeof(TVertexType)) { throw new ArgumentException("Only byte[] raw vertices are supported at this time"); } if (data == null || elements == null) { throw new ArgumentNullException(); } if (elements.Length == 0) { throw new ArgumentException(); } int stride = VertexElementAttribute.CalculateVertexStride(elements); return(new Vertices <TVertexType>(context, data, elements, stride)); }
public VertexElement[] GetDeclaration(Type type) { lock (_declarationMapping) { VertexElement[] mapping; if (_declarationMapping.TryGetValue(type, out mapping)) { return(mapping); } if (type == typeof(Vector3)) //special case { mapping = new VertexElement[] { new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0) } } ; if (type == typeof(Vector4)) { mapping = new VertexElement[] { new VertexElement(0, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Position, 0) } } ; if (mapping == null) { List <VertexElement> elements = new List <VertexElement>(); int offset = 0; if (type.IsValueType == false) { throw new ArgumentException("Type " + type.Name + " is a not a ValueType (struct)"); } foreach (FieldInfo f in type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) { if (!f.ReflectedType.IsValueType) { throw new ArgumentException("Field " + type.Name + "." + f.Name + " is a not a ValueType (struct)"); } int size = Marshal.SizeOf(f.FieldType); bool attribSet = false; foreach (object o in f.GetCustomAttributes(true)) { if (o is VertexElementAttribute) { VertexElementAttribute att = (VertexElementAttribute)o; DeclarationType format = att.Type; if (format == DeclarationType.Unused) { format = DetermineFormat(f); } else { int formatSize; if (!_formatMappingSize.TryGetValue(format, out formatSize)) { throw new ArgumentException(string.Format("Invlaid DeclarationType ({0}) specified in VertexElementAttribute for {1}.{2}", format, type.FullName, f.Name)); } if (formatSize != Marshal.SizeOf(f.FieldType)) { throw new ArgumentException(string.Format("DeclarationType size mismatch in {4}.{5}, {0} requires a size of {1}, specified type {2} has size {3}", format, formatSize, f.FieldType.FullName, Marshal.SizeOf(f.FieldType), type.FullName, f.Name)); } } elements.Add(new VertexElement(0, (short)offset, format, DeclarationMethod.Default, att.Usage, (byte)att.Index)); attribSet = true; break; } } if (!attribSet) { DeclarationType format = DetermineFormat(f); int index; DeclarationUsage usage = DetermineUsage(elements, f, out index); elements.Add(new VertexElement(0, (short)offset, format, DeclarationMethod.Default, usage, (byte)index)); } offset += size; } elements.Add(VertexElement.VertexDeclarationEnd); mapping = elements.ToArray(); } _declarationMapping.Add(type, mapping); return(mapping); } }