Skip to content

Commit

Permalink
More DirectX work
Browse files Browse the repository at this point in the history
  • Loading branch information
David Hall committed Dec 26, 2024
1 parent 49cde0b commit 7c18a56
Show file tree
Hide file tree
Showing 20 changed files with 1,540 additions and 599 deletions.
53 changes: 40 additions & 13 deletions Core/InteropServices/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,23 +187,50 @@ public static Matrix CreateFilled(int rows, int columns, float value)
return r;
}

/// <summary>Gets an identity matrix of the specified size.</summary>
/// <summary>Gets an identity matrix of the specified balanced size.</summary>
/// <param name="dimensions">The rows and columns in the matrix.</param>
/// <returns>An identity matrix.</returns>
/// <returns>An identity matrix with the value of all diagonal entries set to <c>1.0f</c>.</returns>
/// <exception cref="System.ArgumentException">The number of rows must be equal to the number of columns.</exception>
public static Matrix CreateIdentity(int dimensions) => CreateIdentity(dimensions, dimensions);

/// <summary>Gets an identity matrix of the specified size.</summary>
/// <param name="rows">The number of rows.</param>
/// <param name="columns">The number of columns.</param>
/// <returns>An identity matrix with the value of all diagonal entries set to <c>1.0f</c>.</returns>
/// <exception cref="System.ArgumentException">The number of rows must be equal to the number of columns.</exception>
public static Matrix CreateIdentity(int dimensions)
public static Matrix CreateIdentity(int rows, int columns)
{
if (dimensions < 1)
throw new ArgumentOutOfRangeException(nameof(dimensions), "Dimension must be greater than 0.");
Memory<float> m = new float[dimensions * dimensions];
if (rows < 1)
throw new ArgumentOutOfRangeException(nameof(rows), "Rows must be greater than 0.");
if (columns < 1)
throw new ArgumentOutOfRangeException(nameof(columns), "Columns must be greater than 0.");
Memory<float> m = new float[rows * columns];
Span<float> sp = m.Span;
for (int i = 0; i < dimensions; i++)
{
int c1 = i * dimensions;
for (int j = 0; j < dimensions; j++)
sp[c1 + j] = i == j ? 1f : 0f;
}
return new(m, dimensions, dimensions);
for (int i = 0; i < Math.Min(rows, columns); i++)
sp[i * columns + i] = 1f;
return new(m, rows, columns);
}

/// <summary>Creates a scaling matrix from the list of scalars.</summary>
/// <param name="scalars">
/// The scalars to use as diagonal values. Note, the resulting matrix will be one dimension larger than the number of scalars in this
/// array and that diagonal entry will be set to <c>1.0f</c>.
/// </param>
/// <returns>
/// A matrix one dimension larger than the number of scalars in <paramref name="scalars"/> whose diagnoal entries are set to each
/// subsequent value of <paramref name="scalars"/> and whose final diagonal entry will be set to <c>1.0f</c>.
/// </returns>
/// <exception cref="System.ArgumentNullException">scalars</exception>
/// <exception cref="System.ArgumentOutOfRangeException">scalars - At least one scaling value must be provided.</exception>
public static Matrix CreateScale(float[] scalars)
{
if (scalars is null) throw new ArgumentNullException(nameof(scalars));
if (scalars.Length < 1)
throw new ArgumentOutOfRangeException(nameof(scalars), "At least one scaling value must be provided.");
Matrix r = CreateIdentity(scalars.Length + 1);
for (int i = 0; i < scalars.Length; i++)
r[i, i] = scalars[i];
return r;
}

/// <summary>Performs an implicit conversion from <see cref="float"/>[,] to <see cref="Matrix"/>.</summary>
Expand Down
375 changes: 361 additions & 14 deletions PInvoke/DXGI/DCommon.cs

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions PInvoke/DXGI/DXGI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,13 @@ public struct D3DCOLORVALUE(float r, float g, float b, float a = 1.0f) : IEquata
/// </summary>
public float a = a;

/// <summary>Initializes a new instance of the <see cref="D3DCOLORVALUE"/> struct.</summary>
/// <param name="color">The color.</param>
/// <param name="a">The alpha value.</param>
public D3DCOLORVALUE(System.Drawing.Color color, float a = 1.0f) : this(color.R / 255f, color.G / 255f, color.B / 255f, a)
{
}

/// <inheritdoc/>
public override bool Equals(object? obj) => obj is D3DCOLORVALUE dCOLORVALUE && Equals(dCOLORVALUE);

Expand Down Expand Up @@ -976,12 +983,12 @@ public override int GetHashCode()
/// <returns>The result of the conversion.</returns>
public static explicit operator D3DCOLOR(D3DCOLORVALUE cv) => new((byte)(cv.r * 255), (byte)(cv.g * 255), (byte)(cv.b * 255), (byte)(cv.a * 255));

/// <summary>Performs an explicit conversion from <see cref="Vanara.PInvoke.DXGI.D3DCOLORVALUE"/> to <see cref="float"/>[].</summary>
/// <summary>Performs an explicit conversion from <see cref="D3DCOLORVALUE"/> to <see cref="float"/>[].</summary>
/// <param name="cv">The color value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator float[](D3DCOLORVALUE cv) => [cv.r, cv.g, cv.b, cv.a];

/// <summary>Performs an explicit conversion from <see cref="float"/>[] to <see cref="Vanara.PInvoke.DXGI.D3DCOLORVALUE"/>.</summary>
/// <summary>Performs an explicit conversion from <see cref="float"/>[] to <see cref="D3DCOLORVALUE"/>.</summary>
/// <param name="cv">The color value array.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator D3DCOLORVALUE(float[] cv) => cv is not null && cv.Length == 4 ? new(cv[0], cv[1], cv[2], cv[3]) : throw new ArgumentException("An array of four values is required.", nameof(cv));
Expand Down
Loading

0 comments on commit 7c18a56

Please sign in to comment.