Make structs readonly when applicable (#4002)

* Make all structs readonly when applicable. It should reduce amount of needless defensive copies

* Make structs with trivial boilerplate equality code record structs

* Remove unnecessary readonly modifiers from TextureCreateInfo

* Make BitMap structs readonly too
This commit is contained in:
Andrey Sukharev
2022-12-05 16:47:39 +03:00
committed by GitHub
parent ae13f0ab4d
commit 4da44e09cb
133 changed files with 156 additions and 378 deletions

View File

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.GAL
{
public struct BlendDescriptor
public readonly struct BlendDescriptor
{
public bool Enable { get; }

View File

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.GAL
{
public struct BufferAssignment
public readonly struct BufferAssignment
{
public readonly int Binding;
public readonly BufferRange Range;

View File

@ -1,22 +1,14 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
namespace Ryujinx.Graphics.GAL
{
[StructLayout(LayoutKind.Sequential, Size = 8)]
public struct BufferHandle : IEquatable<BufferHandle>
public readonly record struct BufferHandle
{
private readonly ulong _value;
public static BufferHandle Null => new BufferHandle(0);
private BufferHandle(ulong value) => _value = value;
public override bool Equals(object obj) => obj is BufferHandle handle && Equals(handle);
public bool Equals([AllowNull] BufferHandle other) => other._value == _value;
public override int GetHashCode() => _value.GetHashCode();
public static bool operator ==(BufferHandle left, BufferHandle right) => left.Equals(right);
public static bool operator !=(BufferHandle left, BufferHandle right) => !(left == right);
}
}

View File

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.GAL
{
public struct BufferRange
public readonly struct BufferRange
{
private static readonly BufferRange _empty = new BufferRange(BufferHandle.Null, 0, 0);

View File

@ -2,7 +2,7 @@ using Ryujinx.Graphics.Shader.Translation;
namespace Ryujinx.Graphics.GAL
{
public struct Capabilities
public readonly struct Capabilities
{
public readonly TargetApi Api;
public readonly string VendorName;

View File

@ -1,32 +1,4 @@
using System;
namespace Ryujinx.Graphics.GAL
{
public struct ColorF : IEquatable<ColorF>
{
public float Red { get; }
public float Green { get; }
public float Blue { get; }
public float Alpha { get; }
public ColorF(float red, float green, float blue, float alpha)
{
Red = red;
Green = green;
Blue = blue;
Alpha = alpha;
}
public bool Equals(ColorF color) => Red == color.Red &&
Green == color.Green &&
Blue == color.Blue &&
Alpha == color.Alpha;
public override bool Equals(object obj) => (obj is ColorF color) && Equals(color);
public override int GetHashCode() => HashCode.Combine(Red, Green, Blue, Alpha);
public static bool operator ==(ColorF l, ColorF r) => l.Equals(r);
public static bool operator !=(ColorF l, ColorF r) => !l.Equals(r);
}
public readonly record struct ColorF(float Red, float Green, float Blue, float Alpha);
}

View File

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.GAL
{
public struct DepthTestDescriptor
public readonly struct DepthTestDescriptor
{
public bool TestEnable { get; }
public bool WriteEnable { get; }

View File

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.GAL
{
public struct DeviceInfo
public readonly struct DeviceInfo
{
public readonly string Id;
public readonly string Vendor;

View File

@ -2,7 +2,7 @@ using Ryujinx.Common;
namespace Ryujinx.Graphics.GAL
{
public struct Extents2D
public readonly struct Extents2D
{
public int X1 { get; }
public int Y1 { get; }

View File

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.GAL
{
public struct Extents2DF
public readonly struct Extents2DF
{
public float X1 { get; }
public float Y1 { get; }

View File

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.GAL
{
public struct HardwareInfo
public readonly struct HardwareInfo
{
public string GpuVendor { get; }
public string GpuModel { get; }

View File

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.GAL
{
public struct ImageCrop
public readonly struct ImageCrop
{
public int Left { get; }
public int Right { get; }

View File

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.GAL
{
public struct MultisampleDescriptor
public readonly struct MultisampleDescriptor
{
public bool AlphaToCoverageEnable { get; }
public bool AlphaToCoverageDitherEnable { get; }

View File

@ -6,7 +6,7 @@ namespace Ryujinx.Graphics.GAL
/// <summary>
/// Descriptor for a pipeline buffer binding.
/// </summary>
public struct BufferPipelineDescriptor
public readonly struct BufferPipelineDescriptor
{
public bool Enable { get; }
public int Stride { get; }

View File

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.GAL
{
public struct Rectangle<T> where T : unmanaged
public readonly struct Rectangle<T> where T : unmanaged
{
public T X { get; }
public T Y { get; }

View File

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.GAL
{
public struct SamplerCreateInfo
public readonly struct SamplerCreateInfo
{
public MinFilter MinFilter { get; }
public MagFilter MagFilter { get; }

View File

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.GAL
{
public struct ScreenCaptureImageInfo
public readonly struct ScreenCaptureImageInfo
{
public ScreenCaptureImageInfo(int width, int height, bool isBgra, byte[] data, bool flipX, bool flipY)
{

View File

@ -2,7 +2,7 @@
namespace Ryujinx.Graphics.GAL
{
public struct ShaderBindings
public readonly struct ShaderBindings
{
public IReadOnlyCollection<int> UniformBufferBindings { get; }
public IReadOnlyCollection<int> StorageBufferBindings { get; }

View File

@ -3,7 +3,7 @@ using Ryujinx.Graphics.Shader.Translation;
namespace Ryujinx.Graphics.GAL
{
public struct ShaderSource
public readonly struct ShaderSource
{
public string Code { get; }
public byte[] BinaryCode { get; }

View File

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.GAL
{
public struct StencilTestDescriptor
public readonly struct StencilTestDescriptor
{
public bool TestEnable { get; }

View File

@ -4,7 +4,7 @@ using System.Numerics;
namespace Ryujinx.Graphics.GAL
{
public struct TextureCreateInfo : IEquatable<TextureCreateInfo>
public readonly struct TextureCreateInfo : IEquatable<TextureCreateInfo>
{
public int Width { get; }
public int Height { get; }
@ -62,42 +62,42 @@ namespace Ryujinx.Graphics.GAL
SwizzleA = swizzleA;
}
public readonly int GetMipSize(int level)
public int GetMipSize(int level)
{
return GetMipStride(level) * GetLevelHeight(level) * GetLevelDepth(level);
}
public readonly int GetMipSize2D(int level)
public int GetMipSize2D(int level)
{
return GetMipStride(level) * GetLevelHeight(level);
}
public readonly int GetMipStride(int level)
public int GetMipStride(int level)
{
return BitUtils.AlignUp(GetLevelWidth(level) * BytesPerPixel, 4);
}
private readonly int GetLevelWidth(int level)
private int GetLevelWidth(int level)
{
return BitUtils.DivRoundUp(GetLevelSize(Width, level), BlockWidth);
}
private readonly int GetLevelHeight(int level)
private int GetLevelHeight(int level)
{
return BitUtils.DivRoundUp(GetLevelSize(Height, level), BlockHeight);
}
private readonly int GetLevelDepth(int level)
private int GetLevelDepth(int level)
{
return Target == Target.Texture3D ? GetLevelSize(Depth, level) : GetLayers();
}
public readonly int GetDepthOrLayers()
public int GetDepthOrLayers()
{
return Target == Target.Texture3D ? Depth : GetLayers();
}
public readonly int GetLayers()
public int GetLayers()
{
if (Target == Target.Texture2DArray ||
Target == Target.Texture2DMultisampleArray ||
@ -113,7 +113,7 @@ namespace Ryujinx.Graphics.GAL
return 1;
}
public readonly int GetLevelsClamped()
public int GetLevelsClamped()
{
int maxSize = Width;

View File

@ -1,40 +1,4 @@
using System;
namespace Ryujinx.Graphics.GAL
{
public struct VertexAttribDescriptor : IEquatable<VertexAttribDescriptor>
{
public int BufferIndex { get; }
public int Offset { get; }
public bool IsZero { get; }
public Format Format { get; }
public VertexAttribDescriptor(int bufferIndex, int offset, bool isZero, Format format)
{
BufferIndex = bufferIndex;
Offset = offset;
IsZero = isZero;
Format = format;
}
public override bool Equals(object obj)
{
return obj is VertexAttribDescriptor other && Equals(other);
}
public bool Equals(VertexAttribDescriptor other)
{
return BufferIndex == other.BufferIndex &&
Offset == other.Offset &&
IsZero == other.IsZero &&
Format == other.Format;
}
public override int GetHashCode()
{
return HashCode.Combine(BufferIndex, Offset, IsZero, Format);
}
}
public readonly record struct VertexAttribDescriptor(int BufferIndex, int Offset, bool IsZero, Format Format);
}

View File

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.GAL
{
public struct VertexBufferDescriptor
public readonly struct VertexBufferDescriptor
{
public BufferRange Buffer { get; }

View File

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.GAL
{
public struct Viewport
public readonly struct Viewport
{
public Rectangle<float> Region { get; }