Skip to content

Commit

Permalink
In debug mode, throw when passing NaN to effect parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
flibitijibibo committed Aug 11, 2022
1 parent 31ff5cd commit 46fdbd5
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/Graphics/Effect/EffectParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,9 @@ public void SetValue(int[] value)
public void SetValueTranspose(Matrix value)
{
// FIXME: All Matrix sizes... this will get ugly. -flibit
#if DEBUG
value.CheckForNaNs();
#endif
unsafe
{
float* dstPtr = (float*) values;
Expand Down Expand Up @@ -654,6 +657,9 @@ public void SetValueTranspose(Matrix[] value)
{
for (int i = 0; i < value.Length; i += 1, dstPtr += 16)
{
#if DEBUG
value[i].CheckForNaNs();
#endif
dstPtr[0] = value[i].M11;
dstPtr[1] = value[i].M12;
dstPtr[2] = value[i].M13;
Expand All @@ -676,6 +682,9 @@ public void SetValueTranspose(Matrix[] value)
{
for (int i = 0; i < value.Length; i += 1, dstPtr += 12)
{
#if DEBUG
value[i].CheckForNaNs();
#endif
dstPtr[0] = value[i].M11;
dstPtr[1] = value[i].M12;
dstPtr[2] = value[i].M13;
Expand All @@ -691,6 +700,9 @@ public void SetValueTranspose(Matrix[] value)
{
for (int i = 0; i < value.Length; i += 1, dstPtr += 16)
{
#if DEBUG
value[i].CheckForNaNs();
#endif
dstPtr[0] = value[i].M11;
dstPtr[1] = value[i].M12;
dstPtr[2] = value[i].M13;
Expand All @@ -709,6 +721,9 @@ public void SetValueTranspose(Matrix[] value)
{
for (int i = 0; i < value.Length; i += 1, dstPtr += 12)
{
#if DEBUG
value[i].CheckForNaNs();
#endif
dstPtr[0] = value[i].M11;
dstPtr[1] = value[i].M12;
dstPtr[2] = value[i].M13;
Expand All @@ -727,6 +742,9 @@ public void SetValueTranspose(Matrix[] value)
{
for (int i = 0; i < value.Length; i += 1, dstPtr += 8)
{
#if DEBUG
value[i].CheckForNaNs();
#endif
dstPtr[0] = value[i].M11;
dstPtr[1] = value[i].M12;
dstPtr[4] = value[i].M21;
Expand All @@ -747,6 +765,9 @@ public void SetValueTranspose(Matrix[] value)
public void SetValue(Matrix value)
{
// FIXME: All Matrix sizes... this will get ugly. -flibit
#if DEBUG
value.CheckForNaNs();
#endif
unsafe
{
float* dstPtr = (float*) values;
Expand Down Expand Up @@ -839,6 +860,9 @@ public void SetValue(Matrix[] value)
{
for (int i = 0; i < value.Length; i += 1, dstPtr += 16)
{
#if DEBUG
value[i].CheckForNaNs();
#endif
dstPtr[0] = value[i].M11;
dstPtr[1] = value[i].M21;
dstPtr[2] = value[i].M31;
Expand All @@ -861,6 +885,9 @@ public void SetValue(Matrix[] value)
{
for (int i = 0; i < value.Length; i += 1, dstPtr += 12)
{
#if DEBUG
value[i].CheckForNaNs();
#endif
dstPtr[0] = value[i].M11;
dstPtr[1] = value[i].M21;
dstPtr[2] = value[i].M31;
Expand All @@ -876,6 +903,9 @@ public void SetValue(Matrix[] value)
{
for (int i = 0; i < value.Length; i += 1, dstPtr += 12)
{
#if DEBUG
value[i].CheckForNaNs();
#endif
dstPtr[0] = value[i].M11;
dstPtr[1] = value[i].M21;
dstPtr[2] = value[i].M31;
Expand All @@ -894,6 +924,9 @@ public void SetValue(Matrix[] value)
{
for (int i = 0; i < value.Length; i += 1, dstPtr += 16)
{
#if DEBUG
value[i].CheckForNaNs();
#endif
dstPtr[0] = value[i].M11;
dstPtr[1] = value[i].M21;
dstPtr[2] = value[i].M31;
Expand All @@ -912,6 +945,9 @@ public void SetValue(Matrix[] value)
{
for (int i = 0; i < value.Length; i += 1, dstPtr += 8)
{
#if DEBUG
value[i].CheckForNaNs();
#endif
dstPtr[0] = value[i].M11;
dstPtr[1] = value[i].M21;
dstPtr[4] = value[i].M12;
Expand All @@ -931,6 +967,9 @@ public void SetValue(Matrix[] value)

public void SetValue(Quaternion value)
{
#if DEBUG
value.CheckForNaNs();
#endif
unsafe
{
float* dstPtr = (float*) values;
Expand All @@ -948,6 +987,9 @@ public void SetValue(Quaternion[] value)
float* dstPtr = (float*) values;
for (int i = 0; i < value.Length; i += 1, dstPtr += 4)
{
#if DEBUG
value[i].CheckForNaNs();
#endif
dstPtr[0] = value[i].X;
dstPtr[1] = value[i].Y;
dstPtr[2] = value[i].Z;
Expand All @@ -958,6 +1000,12 @@ public void SetValue(Quaternion[] value)

public void SetValue(float value)
{
#if DEBUG
if (float.IsNaN(value))
{
throw new InvalidOperationException("Effect parameter is NaN!");
}
#endif
unsafe
{
float* dstPtr = (float*) values;
Expand All @@ -967,6 +1015,15 @@ public void SetValue(float value)

public void SetValue(float[] value)
{
#if DEBUG
foreach (float f in value)
{
if (float.IsNaN(f))
{
throw new InvalidOperationException("Effect parameter contains NaN!");
}
}
#endif
for (int i = 0, j = 0; i < value.Length; i += ColumnCount, j += 16)
{
Marshal.Copy(value, i, values + j, ColumnCount);
Expand All @@ -989,6 +1046,9 @@ public void SetValue(Texture value)

public void SetValue(Vector2 value)
{
#if DEBUG
value.CheckForNaNs();
#endif
unsafe
{
float* dstPtr = (float*) values;
Expand All @@ -1004,6 +1064,9 @@ public void SetValue(Vector2[] value)
float* dstPtr = (float*) values;
for (int i = 0; i < value.Length; i += 1, dstPtr += 4)
{
#if DEBUG
value[i].CheckForNaNs();
#endif
dstPtr[0] = value[i].X;
dstPtr[1] = value[i].Y;
}
Expand All @@ -1012,6 +1075,9 @@ public void SetValue(Vector2[] value)

public void SetValue(Vector3 value)
{
#if DEBUG
value.CheckForNaNs();
#endif
unsafe
{
float* dstPtr = (float*) values;
Expand All @@ -1028,6 +1094,9 @@ public void SetValue(Vector3[] value)
float* dstPtr = (float*) values;
for (int i = 0; i < value.Length; i += 1, dstPtr += 4)
{
#if DEBUG
value[i].CheckForNaNs();
#endif
dstPtr[0] = value[i].X;
dstPtr[1] = value[i].Y;
dstPtr[2] = value[i].Z;
Expand All @@ -1037,6 +1106,9 @@ public void SetValue(Vector3[] value)

public void SetValue(Vector4 value)
{
#if DEBUG
value.CheckForNaNs();
#endif
unsafe
{
float* dstPtr = (float*) values;
Expand All @@ -1054,6 +1126,9 @@ public void SetValue(Vector4[] value)
float* dstPtr = (float*) values;
for (int i = 0; i < value.Length; i += 1, dstPtr += 4)
{
#if DEBUG
value[i].CheckForNaNs();
#endif
dstPtr[0] = value[i].X;
dstPtr[1] = value[i].Y;
dstPtr[2] = value[i].Z;
Expand Down
28 changes: 28 additions & 0 deletions src/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,34 @@ public override string ToString()

#endregion

#region Internal Methods

[Conditional("DEBUG")]
internal void CheckForNaNs()
{
if ( float.IsNaN(M11) ||
float.IsNaN(M12) ||
float.IsNaN(M13) ||
float.IsNaN(M14) ||
float.IsNaN(M21) ||
float.IsNaN(M22) ||
float.IsNaN(M23) ||
float.IsNaN(M24) ||
float.IsNaN(M31) ||
float.IsNaN(M32) ||
float.IsNaN(M33) ||
float.IsNaN(M34) ||
float.IsNaN(M41) ||
float.IsNaN(M42) ||
float.IsNaN(M43) ||
float.IsNaN(M44) )
{
throw new InvalidOperationException("Matrix contains NaNs!");
}
}

#endregion

#region Public Static Methods

/// <summary>
Expand Down
16 changes: 16 additions & 0 deletions src/Quaternion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,22 @@ public override string ToString()

#endregion

#region Internal Methods

[Conditional("DEBUG")]
internal void CheckForNaNs()
{
if ( float.IsNaN(X) ||
float.IsNaN(Y) ||
float.IsNaN(Z) ||
float.IsNaN(W) )
{
throw new InvalidOperationException("Quaternion contains NaNs!");
}
}

#endregion

#region Public Static Methods

/// <summary>
Expand Down
13 changes: 13 additions & 0 deletions src/Vector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,19 @@ public override string ToString()

#endregion

#region Internal Methods

[Conditional("DEBUG")]
internal void CheckForNaNs()
{
if (float.IsNaN(X) || float.IsNaN(Y))
{
throw new InvalidOperationException("Vector2 contains NaNs!");
}
}

#endregion

#region Public Static Methods

/// <summary>
Expand Down
15 changes: 15 additions & 0 deletions src/Vector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,21 @@ public override string ToString()

#endregion

#region Internal Methods

[Conditional("DEBUG")]
internal void CheckForNaNs()
{
if ( float.IsNaN(X) ||
float.IsNaN(Y) ||
float.IsNaN(Z) )
{
throw new InvalidOperationException("Vector3 contains NaNs!");
}
}

#endregion

#region Public Static Methods

/// <summary>
Expand Down
16 changes: 16 additions & 0 deletions src/Vector4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,22 @@ public override string ToString()

#endregion

#region Internal Methods

[Conditional("DEBUG")]
internal void CheckForNaNs()
{
if ( float.IsNaN(X) ||
float.IsNaN(Y) ||
float.IsNaN(Z) ||
float.IsNaN(W) )
{
throw new InvalidOperationException("Vector4 contains NaNs!");
}
}

#endregion

#region Public Static Methods

/// <summary>
Expand Down

0 comments on commit 46fdbd5

Please sign in to comment.