Remove autoproperties from Color4 and Viewport types

This commit is contained in:
Sergio Pedri
2023-04-25 22:16:52 +02:00
parent ed8dc284a6
commit 26e53cce98
2 changed files with 74 additions and 59 deletions

View File

@@ -20,6 +20,12 @@ public readonly struct Color4
{ {
#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
private readonly Vector128<float> _value; private readonly Vector128<float> _value;
#else
// Note: intentionally using fields, as autoproperties fail to compile on .NET Native (UWP)
private readonly float _a;
private readonly float _r;
private readonly float _g;
private readonly float _b;
#endif #endif
/// <summary> /// <summary>
@@ -31,7 +37,7 @@ public readonly struct Color4
#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
_value = Vector128.Create(value, value, value, value); _value = Vector128.Create(value, value, value, value);
#else #else
A = R = G = B = value; _a = _r = _g = _b = value;
#endif #endif
} }
@@ -47,10 +53,10 @@ public readonly struct Color4
#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
_value = Vector128.Create(red, green, blue, alpha); _value = Vector128.Create(red, green, blue, alpha);
#else #else
R = red; _r = red;
G = green; _g = green;
B = blue; _b = blue;
A = alpha; _a = alpha;
#endif #endif
} }
@@ -63,10 +69,10 @@ public readonly struct Color4
#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
_value = value.AsVector128(); _value = value.AsVector128();
#else #else
R = value.X; _r = value.X;
G = value.Y; _g = value.Y;
B = value.Z; _b = value.Z;
A = value.W; _a = value.W;
#endif #endif
} }
@@ -80,10 +86,10 @@ public readonly struct Color4
#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
_value = Vector128.Create(value.X, value.Y, value.Z, alpha); _value = Vector128.Create(value.X, value.Y, value.Z, alpha);
#else #else
R = value.X; _r = value.X;
G = value.Y; _g = value.Y;
B = value.Z; _b = value.Z;
A = alpha; _a = alpha;
#endif #endif
} }
@@ -140,16 +146,16 @@ public readonly struct Color4
} }
#else #else
/// <summary>Gets the value of the red component.</summary> /// <summary>Gets the value of the red component.</summary>
public float R { get; } public float R => _r;
/// <summary>Gets the value of the green component.</summary> /// <summary>Gets the value of the green component.</summary>
public float G { get; } public float G => _g;
/// <summary>Gets the value of the blue component.</summary> /// <summary>Gets the value of the blue component.</summary>
public float B { get; } public float B => _b;
/// <summary>Gets the value of the alpha component.</summary> /// <summary>Gets the value of the alpha component.</summary>
public float A { get; } public float A => _a;
#endif #endif
public readonly float this[int index] public readonly float this[int index]

View File

@@ -19,6 +19,15 @@ namespace Win32.Numerics;
[StructLayout(LayoutKind.Sequential, Pack = 4)] [StructLayout(LayoutKind.Sequential, Pack = 4)]
public readonly struct Viewport : IEquatable<Viewport> public readonly struct Viewport : IEquatable<Viewport>
{ {
// Note: intentionally using fields, as autoproperties fail to compile on .NET Native (UWP).
// This applies to all targets (including .NET NET 6+), as there's no performance difference.
private readonly float _x;
private readonly float _y;
private readonly float _width;
private readonly float _height;
private readonly float _minDepth;
private readonly float _maxDepth;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Viewport"/> struct. /// Initializes a new instance of the <see cref="Viewport"/> struct.
/// </summary> /// </summary>
@@ -26,12 +35,12 @@ public readonly struct Viewport : IEquatable<Viewport>
/// <param name="height">The height of the viewport in pixels.</param> /// <param name="height">The height of the viewport in pixels.</param>
public Viewport(float width, float height) public Viewport(float width, float height)
{ {
X = 0.0f; _x = 0.0f;
Y = 0.0f; _y = 0.0f;
Width = width; _width = width;
Height = height; _height = height;
MinDepth = 0.0f; _minDepth = 0.0f;
MaxDepth = 1.0f; _maxDepth = 1.0f;
} }
/// <summary> /// <summary>
@@ -43,12 +52,12 @@ public readonly struct Viewport : IEquatable<Viewport>
/// <param name="height">The height of the viewport in pixels.</param> /// <param name="height">The height of the viewport in pixels.</param>
public Viewport(float x, float y, float width, float height) public Viewport(float x, float y, float width, float height)
{ {
X = x; _x = x;
Y = y; _y = y;
Width = width; _width = width;
Height = height; _height = height;
MinDepth = 0.0f; _minDepth = 0.0f;
MaxDepth = 1.0f; _maxDepth = 1.0f;
} }
/// <summary> /// <summary>
@@ -62,12 +71,12 @@ public readonly struct Viewport : IEquatable<Viewport>
/// <param name="maxDepth">The maximum depth of the clip volume.</param> /// <param name="maxDepth">The maximum depth of the clip volume.</param>
public Viewport(float x, float y, float width, float height, float minDepth, float maxDepth) public Viewport(float x, float y, float width, float height, float minDepth, float maxDepth)
{ {
X = x; _x = x;
Y = y; _y = y;
Width = width; _width = width;
Height = height; _height = height;
MinDepth = minDepth; _minDepth = minDepth;
MaxDepth = maxDepth; _maxDepth = maxDepth;
} }
/// <summary> /// <summary>
@@ -76,12 +85,12 @@ public readonly struct Viewport : IEquatable<Viewport>
/// <param name="bounds">A <see cref="RectangleF"/> that defines the location and size of the viewport in a render target.</param> /// <param name="bounds">A <see cref="RectangleF"/> that defines the location and size of the viewport in a render target.</param>
public Viewport(in RectangleF bounds) public Viewport(in RectangleF bounds)
{ {
X = bounds.X; _x = bounds.X;
Y = bounds.Y; _y = bounds.Y;
Width = bounds.Width; _width = bounds.Width;
Height = bounds.Height; _height = bounds.Height;
MinDepth = 0.0f; _minDepth = 0.0f;
MaxDepth = 1.0f; _maxDepth = 1.0f;
} }
/// <summary> /// <summary>
@@ -90,12 +99,12 @@ public readonly struct Viewport : IEquatable<Viewport>
/// <param name="bounds">A <see cref="Rectangle"/> that defines the location and size of the viewport in a render target.</param> /// <param name="bounds">A <see cref="Rectangle"/> that defines the location and size of the viewport in a render target.</param>
public Viewport(in Rectangle bounds) public Viewport(in Rectangle bounds)
{ {
X = bounds.X; _x = bounds.X;
Y = bounds.Y; _y = bounds.Y;
Width = bounds.Width; _width = bounds.Width;
Height = bounds.Height; _height = bounds.Height;
MinDepth = 0.0f; _minDepth = 0.0f;
MaxDepth = 1.0f; _maxDepth = 1.0f;
} }
/// <summary> /// <summary>
@@ -104,43 +113,43 @@ public readonly struct Viewport : IEquatable<Viewport>
/// <param name="bounds">A <see cref="Vector4"/> that defines the location and size of the viewport in a render target.</param> /// <param name="bounds">A <see cref="Vector4"/> that defines the location and size of the viewport in a render target.</param>
public Viewport(in Vector4 bounds) public Viewport(in Vector4 bounds)
{ {
X = bounds.X; _x = bounds.X;
Y = bounds.Y; _y = bounds.Y;
Width = bounds.Z; _width = bounds.Z;
Height = bounds.W; _height = bounds.W;
MinDepth = 0.0f; _minDepth = 0.0f;
MaxDepth = 1.0f; _maxDepth = 1.0f;
} }
/// <summary> /// <summary>
/// Position of the pixel coordinate of the upper-left corner of the viewport. /// Position of the pixel coordinate of the upper-left corner of the viewport.
/// </summary> /// </summary>
public float X { get; } public float X => _x;
/// <summary> /// <summary>
/// Position of the pixel coordinate of the upper-left corner of the viewport. /// Position of the pixel coordinate of the upper-left corner of the viewport.
/// </summary> /// </summary>
public float Y { get; } public float Y => _y;
/// <summary> /// <summary>
/// Width dimension of the viewport. /// Width dimension of the viewport.
/// </summary> /// </summary>
public float Width { get; } public float Width => _width;
/// <summary> /// <summary>
/// Height dimension of the viewport. /// Height dimension of the viewport.
/// </summary> /// </summary>
public float Height { get; } public float Height => _height;
/// <summary> /// <summary>
/// Gets or sets the minimum depth of the clip volume. /// Gets or sets the minimum depth of the clip volume.
/// </summary> /// </summary>
public float MinDepth { get; } public float MinDepth => _minDepth;
/// <summary> /// <summary>
/// Gets or sets the maximum depth of the clip volume. /// Gets or sets the maximum depth of the clip volume.
/// </summary> /// </summary>
public float MaxDepth { get; } public float MaxDepth => _maxDepth;
/// <summary> /// <summary>
/// Gets or sets the bounds of the viewport. /// Gets or sets the bounds of the viewport.