forked from MoonsideGames/MoonWorks
				
			vertex binding API improvements
							parent
							
								
									95981f0f03
								
							
						
					
					
						commit
						c43df10c2a
					
				|  | @ -1,9 +1,10 @@ | |||
| <Project Sdk="Microsoft.NET.Sdk"> | ||||
| 
 | ||||
| 	<PropertyGroup> | ||||
| 		<TargetFrameworks>net6.0;net7.0</TargetFrameworks> | ||||
| 		<TargetFrameworks>net7.0</TargetFrameworks> | ||||
| 		<Platforms>x64</Platforms> | ||||
| 		<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||||
| 		<LangVersion>11</LangVersion> | ||||
| 	</PropertyGroup> | ||||
| 
 | ||||
| 	<PropertyGroup> | ||||
|  |  | |||
|  | @ -2,6 +2,6 @@ namespace MoonWorks.Graphics | |||
| { | ||||
| 	public interface IVertexType | ||||
| 	{ | ||||
| 		VertexAttribute[] Attributes(uint binding = 0); | ||||
| 		static abstract VertexElementFormat[] Formats { get; } | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -145,21 +145,6 @@ namespace MoonWorks.Graphics | |||
| 		public uint Binding; | ||||
| 		public VertexElementFormat Format; | ||||
| 		public uint Offset; | ||||
| 
 | ||||
| 		public static VertexAttribute Create( | ||||
| 			uint binding, | ||||
| 			uint location, | ||||
| 			VertexElementFormat format, | ||||
| 			uint offset | ||||
| 		) { | ||||
| 			return new VertexAttribute | ||||
| 			{ | ||||
| 				Location = location, | ||||
| 				Binding = binding, | ||||
| 				Format = format, | ||||
| 				Offset = offset | ||||
| 			}; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	[StructLayout(LayoutKind.Sequential)] | ||||
|  |  | |||
|  | @ -30,12 +30,41 @@ | |||
| 			VertexAttributes = vertexAttributes; | ||||
| 		} | ||||
| 
 | ||||
| 		public VertexInputState( | ||||
| 			VertexBindingAndAttributes bindingAndAttributes | ||||
| 		) { | ||||
| 			VertexBindings = new VertexBinding[] { bindingAndAttributes.VertexBinding }; | ||||
| 			VertexAttributes = bindingAndAttributes.VertexAttributes; | ||||
| 		} | ||||
| 
 | ||||
| 		public VertexInputState( | ||||
| 			VertexBindingAndAttributes[] bindingAndAttributesArray | ||||
| 		) { | ||||
| 			VertexBindings = new VertexBinding[bindingAndAttributesArray.Length]; | ||||
| 			var attributesLength = 0; | ||||
| 
 | ||||
| 			for (var i = 0; i < bindingAndAttributesArray.Length; i += 1) | ||||
| 			{ | ||||
| 				VertexBindings[i] = bindingAndAttributesArray[i].VertexBinding; | ||||
| 				attributesLength += bindingAndAttributesArray[i].VertexAttributes.Length; | ||||
| 			} | ||||
| 
 | ||||
| 			VertexAttributes = new VertexAttribute[attributesLength]; | ||||
| 
 | ||||
| 			var attributeIndex = 0; | ||||
| 			for (var i = 0; i < bindingAndAttributesArray.Length; i += 1) | ||||
| 			{ | ||||
| 				for (var j = 0; j < bindingAndAttributesArray[i].VertexAttributes.Length; j += 1) | ||||
| 				{ | ||||
| 					VertexAttributes[attributeIndex] = bindingAndAttributesArray[i].VertexAttributes[j]; | ||||
| 					attributeIndex += 1; | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		public static VertexInputState CreateSingleBinding<T>() where T : unmanaged, IVertexType | ||||
| 		{ | ||||
| 			return new VertexInputState( | ||||
| 				VertexBinding.Create<T>(), | ||||
| 				default(T).Attributes() | ||||
| 			); | ||||
| 			return new VertexInputState(VertexBindingAndAttributes.Create<T>(0)); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -1,7 +1,28 @@ | |||
| namespace MoonWorks | ||||
| using System.Collections.Generic; | ||||
| using System.Runtime.InteropServices; | ||||
| using MoonWorks.Graphics; | ||||
| 
 | ||||
| namespace MoonWorks | ||||
| { | ||||
| 	public static class Conversions | ||||
| 	{ | ||||
| 		private readonly static Dictionary<VertexElementFormat, uint> Sizes = new Dictionary<VertexElementFormat, uint> | ||||
| 		{ | ||||
| 			{ VertexElementFormat.Byte4, (uint) Marshal.SizeOf<Byte4>() }, | ||||
| 			{ VertexElementFormat.Color, (uint) Marshal.SizeOf<Color>() }, | ||||
| 			{ VertexElementFormat.Float, (uint) Marshal.SizeOf<float>() }, | ||||
| 			{ VertexElementFormat.HalfVector2, (uint) Marshal.SizeOf<HalfVector2>() }, | ||||
| 			{ VertexElementFormat.HalfVector4, (uint) Marshal.SizeOf<HalfVector4>() }, | ||||
| 			{ VertexElementFormat.NormalizedShort2, (uint) Marshal.SizeOf<NormalizedShort2>() }, | ||||
| 			{ VertexElementFormat.NormalizedShort4, (uint) Marshal.SizeOf<NormalizedShort4>() }, | ||||
| 			{ VertexElementFormat.Short2, (uint) Marshal.SizeOf<Short2>() }, | ||||
| 			{ VertexElementFormat.Short4, (uint) Marshal.SizeOf<Short4>() }, | ||||
| 			{ VertexElementFormat.UInt, (uint) Marshal.SizeOf<uint>() }, | ||||
| 			{ VertexElementFormat.Vector2, (uint) Marshal.SizeOf<Math.Float.Vector2>() }, | ||||
| 			{ VertexElementFormat.Vector3, (uint) Marshal.SizeOf<Math.Float.Vector3>() }, | ||||
| 			{ VertexElementFormat.Vector4, (uint) Marshal.SizeOf<Math.Float.Vector4>() } | ||||
| 		}; | ||||
| 
 | ||||
| 		public static byte BoolToByte(bool b) | ||||
| 		{ | ||||
| 			return (byte) (b ? 1 : 0); | ||||
|  | @ -11,5 +32,10 @@ | |||
| 		{ | ||||
| 			return b != 0; | ||||
| 		} | ||||
| 
 | ||||
| 		public static uint VertexElementFormatSize(VertexElementFormat format) | ||||
| 		{ | ||||
| 			return Sizes[format]; | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -0,0 +1,39 @@ | |||
| namespace MoonWorks.Graphics | ||||
| { | ||||
| 	// This is a convenience structure for pairing a vertex binding with its associated attributes. | ||||
| 	public struct VertexBindingAndAttributes | ||||
| 	{ | ||||
| 		public VertexBinding VertexBinding { get; } | ||||
| 		public VertexAttribute[] VertexAttributes { get; } | ||||
| 
 | ||||
| 		public VertexBindingAndAttributes(VertexBinding binding, VertexAttribute[] attributes) | ||||
| 		{ | ||||
| 			VertexBinding = binding; | ||||
| 			VertexAttributes = attributes; | ||||
| 		} | ||||
| 
 | ||||
| 		public static VertexBindingAndAttributes Create<T>(uint bindingIndex) where T : unmanaged, IVertexType | ||||
| 		{ | ||||
| 			VertexBinding binding = VertexBinding.Create<T>(bindingIndex); | ||||
| 			VertexAttribute[] attributes = new VertexAttribute[T.Formats.Length]; | ||||
| 			uint offset = 0; | ||||
| 
 | ||||
| 			for (uint i = 0; i < T.Formats.Length; i += 1) | ||||
| 			{ | ||||
| 				var format = T.Formats[i]; | ||||
| 
 | ||||
| 				attributes[i] = new VertexAttribute | ||||
| 				{ | ||||
| 					Binding = bindingIndex, | ||||
| 					Location = i, | ||||
| 					Format = format, | ||||
| 					Offset = offset | ||||
| 				}; | ||||
| 
 | ||||
| 				offset += Conversions.VertexElementFormatSize(format); | ||||
| 			} | ||||
| 
 | ||||
| 			return new VertexBindingAndAttributes(binding, attributes); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
		Loading…
	
		Reference in New Issue