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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
133 changed files with 156 additions and 378 deletions

View file

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
{
struct InstInfo
readonly struct InstInfo
{
public InstType Type { get; }

View file

@ -13,7 +13,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
{
private static readonly string[] StagePrefixes = new string[] { "cp", "vp", "tcp", "tep", "gp", "fp" };
private struct BuiltInAttribute
private readonly struct BuiltInAttribute
{
public string Name { get; }

View file

@ -3,7 +3,7 @@ using Spv.Generator;
namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
{
struct OperationResult
readonly struct OperationResult
{
public static OperationResult Invalid => new OperationResult(AggregateType.Invalid, null);

View file

@ -8,7 +8,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
/// <summary>
/// Delegate cache for SPIR-V instruction generators. Avoids delegate allocation when passing generators as arguments.
/// </summary>
internal struct SpirvDelegates
internal readonly struct SpirvDelegates
{
// Unary
public readonly FuncUnaryInstruction GlslFAbs;

View file

@ -1,33 +1,4 @@
using System;
namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
{
struct TextureMeta : IEquatable<TextureMeta>
{
public int CbufSlot { get; }
public int Handle { get; }
public TextureFormat Format { get; }
public TextureMeta(int cbufSlot, int handle, TextureFormat format)
{
CbufSlot = cbufSlot;
Handle = handle;
Format = format;
}
public override bool Equals(object obj)
{
return obj is TextureMeta other && Equals(other);
}
public bool Equals(TextureMeta other)
{
return CbufSlot == other.CbufSlot && Handle == other.Handle && Format == other.Format;
}
public override int GetHashCode()
{
return HashCode.Combine(CbufSlot, Handle, Format);
}
}
readonly record struct TextureMeta(int CbufSlot, int Handle, TextureFormat Format);
}

View file

@ -17,7 +17,7 @@ namespace Ryujinx.Graphics.Shader.Decoders
}
}
struct SyncTarget
readonly struct SyncTarget
{
public PushOpInfo PushOpInfo { get; }
public int PushOpId { get; }

View file

@ -4,7 +4,7 @@ using System.Collections.Generic;
namespace Ryujinx.Graphics.Shader.Decoders
{
struct DecodedProgram : IEnumerable<DecodedFunction>
readonly struct DecodedProgram : IEnumerable<DecodedFunction>
{
public DecodedFunction MainFunction { get; }
private readonly IReadOnlyDictionary<ulong, DecodedFunction> _functions;

View file

@ -473,7 +473,7 @@ namespace Ryujinx.Graphics.Shader.Decoders
op = Unsafe.As<ulong, T>(ref rawOp);
}
private struct BlockLocation
private readonly struct BlockLocation
{
public Block Block { get; }
public int Index { get; }

View file

@ -6,7 +6,7 @@ namespace Ryujinx.Graphics.Shader.Decoders
{
private const int EncodingBits = 14;
private struct TableEntry
private readonly struct TableEntry
{
public InstName Name { get; }
public InstEmitter Emitter { get; }

View file

@ -2,7 +2,7 @@ using System;
namespace Ryujinx.Graphics.Shader.Decoders
{
struct Register : IEquatable<Register>
readonly struct Register : IEquatable<Register>
{
public int Index { get; }

View file

@ -5,7 +5,7 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
{
static class InstructionInfo
{
private struct InstInfo
private readonly struct InstInfo
{
public VariableType DestType { get; }

View file

@ -2,7 +2,7 @@ using System.Collections.Generic;
namespace Ryujinx.Graphics.Shader.StructuredIr
{
struct TransformFeedbackOutput
readonly struct TransformFeedbackOutput
{
public readonly bool Valid;
public readonly int Buffer;

View file

@ -2,7 +2,7 @@
namespace Ryujinx.Graphics.Shader.Translation
{
struct AttributeInfo
readonly struct AttributeInfo
{
private static readonly Dictionary<int, AttributeInfo> _builtInAttributes = new Dictionary<int, AttributeInfo>()
{

View file

@ -21,7 +21,7 @@ namespace Ryujinx.Graphics.Shader.Translation
public int OperationsCount => _operations.Count;
private struct BrxTarget
private readonly struct BrxTarget
{
public readonly Operand Selector;
public readonly int ExpectedValue;

View file

@ -87,7 +87,7 @@ namespace Ryujinx.Graphics.Shader.Translation
}
}
private struct TreeNodeUse
private readonly struct TreeNodeUse
{
public TreeNode Node { get; }
public int Index { get; }
@ -345,7 +345,7 @@ namespace Ryujinx.Graphics.Shader.Translation
bool Matches(in InstOp opInfo);
}
private struct PatternTreeNodeUse
private readonly struct PatternTreeNodeUse
{
public IPatternTreeNode Node { get; }
public int Index { get; }

View file

@ -114,7 +114,7 @@ namespace Ryujinx.Graphics.Shader.Translation
}
}
public struct FunctionRegisterUsage
public readonly struct FunctionRegisterUsage
{
public Register[] InArguments { get; }
public Register[] OutArguments { get; }

View file

@ -69,36 +69,7 @@ namespace Ryujinx.Graphics.Shader.Translation
private int _usedStorageBuffers;
private int _usedStorageBuffersWrite;
private struct TextureInfo : IEquatable<TextureInfo>
{
public int CbufSlot { get; }
public int Handle { get; }
public bool Indexed { get; }
public TextureFormat Format { get; }
public TextureInfo(int cbufSlot, int handle, bool indexed, TextureFormat format)
{
CbufSlot = cbufSlot;
Handle = handle;
Indexed = indexed;
Format = format;
}
public override bool Equals(object obj)
{
return obj is TextureInfo other && Equals(other);
}
public bool Equals(TextureInfo other)
{
return CbufSlot == other.CbufSlot && Handle == other.Handle && Indexed == other.Indexed && Format == other.Format;
}
public override int GetHashCode()
{
return HashCode.Combine(CbufSlot, Handle, Indexed, Format);
}
}
private readonly record struct TextureInfo(int CbufSlot, int Handle, bool Indexed, TextureFormat Format);
private struct TextureMeta
{

View file

@ -12,7 +12,7 @@ namespace Ryujinx.Graphics.Shader.Translation
ScreenLinear = 3
}
struct ImapPixelType
readonly struct ImapPixelType
{
public PixelImap X { get; }
public PixelImap Y { get; }

View file

@ -108,7 +108,7 @@ namespace Ryujinx.Graphics.Shader.Translation
}
}
private struct Definition
private readonly struct Definition
{
public BasicBlock Block { get; }
public Operand Local { get; }

View file

@ -1,6 +1,6 @@
namespace Ryujinx.Graphics.Shader.Translation
{
public struct TranslationOptions
public readonly struct TranslationOptions
{
public TargetLanguage TargetLanguage { get; }
public TargetApi TargetApi { get; }

View file

@ -14,7 +14,7 @@ namespace Ryujinx.Graphics.Shader.Translation
{
private const int HeaderSize = 0x50;
internal struct FunctionCode
internal readonly struct FunctionCode
{
public Operation[] Code { get; }