Compare commits

...

5 Commits

10 changed files with 126 additions and 67 deletions

View File

@ -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>

@ -1 +1 @@
Subproject commit 6567310793f90e24438ae8682492c660207275af
Subproject commit 11ba6b37509a6c2fa2690f2643ee7bf5ce2ab4f2

@ -1 +1 @@
Subproject commit 9068263afcf5743ac8f2c023eb68610523feb905
Subproject commit 1643061386177f62b516ccaad0ea04607cae2333

@ -1 +1 @@
Subproject commit eb2178f6d042038463fb18bd756576cd5152003e
Subproject commit f8c6fc407fbb22072fdafcda918aec52b2102519

View File

@ -0,0 +1,7 @@
namespace MoonWorks.Graphics
{
public interface IVertexType
{
static abstract VertexElementFormat[] Formats { get; }
}
}

View File

@ -127,12 +127,11 @@ namespace MoonWorks.Graphics
public uint Stride;
public VertexInputRate InputRate;
// Shortcut for the common case of having a single vertex binding.
public unsafe static VertexBinding Create<T>() where T : unmanaged
public static VertexBinding Create<T>(uint binding = 0) where T : unmanaged
{
return new VertexBinding
{
Binding = 0,
Binding = binding,
InputRate = VertexInputRate.Vertex,
Stride = (uint) Marshal.SizeOf<T>()
};
@ -146,28 +145,6 @@ namespace MoonWorks.Graphics
public uint Binding;
public VertexElementFormat Format;
public uint Offset;
public static VertexAttribute Create<T>(
string fieldName,
uint location,
uint binding = 0
)
{
var fieldInfo = typeof(T).GetField(fieldName);
if (fieldInfo == null)
{
throw new System.ArgumentException("Field not recognized!");
}
return new VertexAttribute
{
Binding = binding,
Location = location,
Format = Conversions.TypeToVertexElementFormat(fieldInfo.FieldType),
Offset = (uint) Marshal.OffsetOf<T>(fieldName)
};
}
}
[StructLayout(LayoutKind.Sequential)]

View File

@ -272,14 +272,12 @@ namespace MoonWorks.Graphics
const uint DDS_MAGIC = 0x20534444;
const uint DDS_HEADERSIZE = 124;
const uint DDS_PIXFMTSIZE = 32;
const uint DDSD_CAPS = 0x1;
const uint DDSD_HEIGHT = 0x2;
const uint DDSD_WIDTH = 0x4;
const uint DDSD_PITCH = 0x8;
const uint DDSD_FMT = 0x1000;
const uint DDSD_LINEARSIZE = 0x80000;
const uint DDSD_REQ = (
DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_FMT
DDSD_HEIGHT | DDSD_WIDTH
);
const uint DDSCAPS_MIPMAP = 0x400000;
const uint DDSCAPS_TEXTURE = 0x1000;

View File

@ -10,16 +10,61 @@
public static readonly VertexInputState Empty = new VertexInputState
{
VertexBindings = new VertexBinding[0],
VertexAttributes = new VertexAttribute[0]
VertexBindings = System.Array.Empty<VertexBinding>(),
VertexAttributes = System.Array.Empty<VertexAttribute>()
};
public VertexInputState(
VertexBinding vertexBinding,
params VertexAttribute[] vertexAttributes
VertexAttribute[] vertexAttributes
) {
VertexBindings = new VertexBinding[] { vertexBinding };
VertexAttributes = vertexAttributes;
}
public VertexInputState(
VertexBinding[] vertexBindings,
VertexAttribute[] vertexAttributes
) {
VertexBindings = vertexBindings;
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(VertexBindingAndAttributes.Create<T>(0));
}
}
}

View File

@ -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);
@ -12,38 +33,9 @@
return b != 0;
}
public static Graphics.VertexElementFormat TypeToVertexElementFormat(System.Type type)
public static uint VertexElementFormatSize(VertexElementFormat format)
{
if (type == typeof(uint))
{
return Graphics.VertexElementFormat.UInt;
}
if (type == typeof(float))
{
return Graphics.VertexElementFormat.Float;
}
else if (type == typeof(Math.Float.Vector2))
{
return Graphics.VertexElementFormat.Vector2;
}
else if (type == typeof(Math.Float.Vector3))
{
return Graphics.VertexElementFormat.Vector3;
}
else if (type == typeof(Math.Float.Vector4))
{
return Graphics.VertexElementFormat.Vector4;
}
else if (type == typeof(Graphics.Color))
{
return Graphics.VertexElementFormat.Color;
}
else
{
throw new System.ArgumentException(
"Cannot automatically convert this type to a VertexElementFormat!"
);
}
return Sizes[format];
}
}
}

View File

@ -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);
}
}
}