diff --git a/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs b/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs index e30e6719..1770ad27 100644 --- a/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs @@ -83,7 +83,7 @@ namespace System.Reflection.Emit OpCode opcode = Marshal.SizeOf(type) switch { // Use the faster op codes for sizes <= 8 - 1 when type == typeof(bool) || type == typeof(byte) || type == typeof(sbyte) => OpCodes.Stind_I1, + 1 when type == typeof(byte) || type == typeof(sbyte) => OpCodes.Stind_I1, 2 when type == typeof(short) || type == typeof(ushort) => OpCodes.Stind_I2, 4 when type == typeof(float) => OpCodes.Stind_R4, 4 when type == typeof(int) || type == typeof(uint) => OpCodes.Stind_I4, diff --git a/src/ComputeSharp.Shaders/Translation/Enums/MethodType.cs b/src/ComputeSharp.Shaders/Translation/Enums/MethodType.cs deleted file mode 100644 index c3c188e8..00000000 --- a/src/ComputeSharp.Shaders/Translation/Enums/MethodType.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace ComputeSharp.Shaders.Translation.Enums -{ - /// - /// An that indicates a type of method to decompile - /// - internal enum MethodType - { - /// - /// The instance method on some shader type - /// - Execute, - - /// - /// A standalone, static method - /// - Static - } -} diff --git a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs deleted file mode 100644 index 21b9afd9..00000000 --- a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics.Contracts; -using System.Reflection; -using Microsoft.Toolkit.Diagnostics; - -namespace ComputeSharp.Shaders.Translation.Models -{ - /// - /// A that wraps a readable member of another and provides access to it - /// - internal sealed class ReadableMember - { - /// - /// The object wrapped by the current instance - /// - private readonly MemberInfo Member; - - /// - /// Creates a new instance with the given parameters - /// - /// The target object wrapped by the current instance - private ReadableMember(MemberInfo memberInfo) - { - // General properties - Member = memberInfo; - Name = memberInfo.Name; - DeclaringType = memberInfo.DeclaringType!; - - // Type specific properties - if (memberInfo is FieldInfo fieldInfo) - { - Member = fieldInfo; - IsStatic = fieldInfo.IsStatic; - MemberType = fieldInfo.FieldType; - } - else if (memberInfo is PropertyInfo propertyInfo) - { - Member = propertyInfo; - IsStatic = propertyInfo.GetMethod!.IsStatic; - MemberType = propertyInfo.PropertyType; - } - else ThrowHelper.ThrowArgumentException("Field and property can't both be null at the same time"); - } - - /// - /// Gets the name of the wrapped member for the current instance - /// - public string Name { get; } - - /// - /// Gets whether or not the current member is not an instance member - /// - public bool IsStatic { get; } - - /// - /// Gets the where the current member is declared in - /// - public Type DeclaringType { get; } - - /// - /// Gets the of the wrapped member for the current instance - /// - public Type MemberType { get; } - - /// - /// Gets or sets the list of parent members to access the current one from the root instance - /// - public IReadOnlyList? Parents { get; set; } - - /// - /// Returns the value of the wrapped member for the current instance - /// - /// The target instance to use to read the value from - [Pure] - public object GetValue(object? instance) - { - return Member switch - { - FieldInfo field => field.GetValue(instance)!, - PropertyInfo property => property.GetValue(instance)!, - _ => ThrowHelper.ThrowArgumentException("Field and property can't both be null at the same time") - }; - } - - /// - /// Converts a object into a instance - /// - /// The input object to wrap - public static implicit operator ReadableMember(FieldInfo field) => new ReadableMember(field); - - /// - /// Converts a object into a instance - /// - /// The input object to wrap - public static implicit operator ReadableMember(PropertyInfo property) => new ReadableMember(property); - - /// - /// Converts a object into a instance - /// - /// The input object to unwrap - public static implicit operator MemberInfo(ReadableMember member) => member.Member; - - /// - /// Converts a object into a instance - /// - /// The input object to unwrap - public static implicit operator FieldInfo(ReadableMember member) => (FieldInfo)member.Member; - - /// - /// Converts a object into a instance - /// - /// The input object to unwrap - public static implicit operator PropertyInfo(ReadableMember member) => (PropertyInfo)member.Member; - } -} diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs index 17bc3c55..781fa8d7 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.IL.cs @@ -44,9 +44,9 @@ namespace ComputeSharp.Shaders.Translation private DispatchDataLoader? dispatchDataLoader; /// - /// The of instances mapping the captured variables in the current shader. + /// The of instances mapping the captured variables in the current shader. /// - private readonly List capturedMembers = new(); + private readonly List capturedFields = new(); /// /// Gets a new instance with all the captured data for the current shader instance. @@ -92,9 +92,9 @@ namespace ComputeSharp.Shaders.Translation // ldarg.4 = ref byte r1 this.dispatchDataLoader = DynamicMethod.New(il => { - foreach (ReadableMember member in this.capturedMembers) + foreach (FieldInfo fieldInfo in this.capturedFields) { - if (HlslKnownTypes.IsKnownBufferType(member.MemberType)) + if (HlslKnownTypes.IsKnownBufferType(fieldInfo.FieldType)) { // Load the offset address into the resource buffers il.Emit(OpCodes.Ldarg_3); @@ -104,8 +104,8 @@ namespace ComputeSharp.Shaders.Translation il.EmitAddOffset(this.totalResourceCount); } - if (!member.IsStatic) il.Emit(OpCodes.Ldarg_2); - il.EmitReadMember(member); + il.Emit(OpCodes.Ldarg_2); + il.EmitReadMember(fieldInfo); // Call NativeObject.ThrowIfDisposed() il.Emit(OpCodes.Dup); @@ -116,25 +116,23 @@ namespace ComputeSharp.Shaders.Translation // Call Buffer.ThrowIfDeviceMismatch(GraphicsDevice) il.Emit(OpCodes.Dup); il.Emit(OpCodes.Ldarg_1); - il.EmitCall(OpCodes.Callvirt, member.MemberType.GetMethod( + il.EmitCall(OpCodes.Callvirt, fieldInfo.FieldType.GetMethod( nameof(Buffer.ThrowIfDeviceMismatch), BindingFlags.Instance | BindingFlags.NonPublic)!, null); // Access Buffer.D3D12GpuDescriptorHandle - il.EmitReadMember(member.MemberType.GetField( + il.EmitReadMember(fieldInfo.FieldType.GetField( nameof(Buffer.D3D12GpuDescriptorHandle), BindingFlags.Instance | BindingFlags.NonPublic)!); il.EmitStoreToAddress(typeof(D3D12_GPU_DESCRIPTOR_HANDLE)); this.totalResourceCount++; } - else if (HlslKnownTypes.IsKnownScalarType(member.MemberType) || - HlslKnownTypes.IsKnownVectorType(member.MemberType)) + else if (HlslKnownTypes.IsKnownScalarType(fieldInfo.FieldType) || + HlslKnownTypes.IsKnownVectorType(fieldInfo.FieldType)) { // Calculate the right offset with 16-bytes padding (HLSL constant buffer) - int size = member.MemberType == typeof(bool) - ? sizeof(uint) - : Marshal.SizeOf(member.MemberType); // bool is 4 bytes in HLSL + int size = Marshal.SizeOf(fieldInfo.FieldType); if (this.totalVariablesByteSize % 16 > 16 - size) { @@ -145,10 +143,9 @@ namespace ComputeSharp.Shaders.Translation il.Emit(OpCodes.Ldarg_S, (byte)4); il.EmitAddOffset(totalVariablesByteSize); - if (!member.IsStatic) il.Emit(OpCodes.Ldarg_2); - - il.EmitReadMember(member); - il.EmitStoreToAddress(member.MemberType); + il.Emit(OpCodes.Ldarg_2); + il.EmitReadMember(fieldInfo); + il.EmitStoreToAddress(fieldInfo.FieldType); this.totalVariablesByteSize += size; } diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs index 221deb27..cadbf629 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs @@ -147,7 +147,6 @@ namespace ComputeSharp.Shaders.Translation { IReadOnlyList shaderFields = typeof(T).GetFields( BindingFlags.Instance | - BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).ToArray(); @@ -172,11 +171,11 @@ namespace ComputeSharp.Shaders.Translation /// Loads a specified and adds it to the shader model /// /// The boxed instance to use to build the shader - /// The target to load - private void LoadFieldInfo(object shader, ReadableMember memberInfo) + /// The target to load + private void LoadFieldInfo(object shader, FieldInfo fieldInfo) { - Type fieldType = memberInfo.MemberType; - (string hlslName, string hlslType) = Attribute.Fields[memberInfo.Name]; + Type fieldType = fieldInfo.FieldType; + (string hlslName, string hlslType) = Attribute.Fields[fieldInfo.Name]; // Constant buffer if (HlslKnownTypes.IsConstantBufferType(fieldType)) @@ -184,7 +183,7 @@ namespace ComputeSharp.Shaders.Translation D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, this.constantBuffersCount); this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1); - this.capturedMembers.Add(memberInfo); + this.capturedFields.Add(fieldInfo); this.hlslBuffersInfo.Add(new HlslBufferInfo.Constant(hlslType, hlslName, (int)this.constantBuffersCount++)); } else if (HlslKnownTypes.IsReadOnlyBufferType(fieldType)) @@ -193,7 +192,7 @@ namespace ComputeSharp.Shaders.Translation D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, this.readOnlyBuffersCount); this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1); - this.capturedMembers.Add(memberInfo); + this.capturedFields.Add(fieldInfo); this.hlslBuffersInfo.Add(new HlslBufferInfo.ReadOnly(hlslType, hlslName, (int)this.readOnlyBuffersCount++)); } else if (HlslKnownTypes.IsReadWriteBufferType(fieldType)) @@ -202,16 +201,16 @@ namespace ComputeSharp.Shaders.Translation D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, this.readWriteBuffersCount); this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1); - this.capturedMembers.Add(memberInfo); + this.capturedFields.Add(fieldInfo); this.hlslBuffersInfo.Add(new HlslBufferInfo.ReadWrite(hlslType, hlslName, (int)this.readWriteBuffersCount++)); } else if (HlslKnownTypes.IsKnownScalarType(fieldType) || HlslKnownTypes.IsKnownVectorType(fieldType)) { - this.capturedMembers.Add(memberInfo); + this.capturedFields.Add(fieldInfo); this.fieldsInfo.Add(new CapturedFieldInfo(hlslType, hlslName)); } else if (fieldType.IsDelegate() && - memberInfo.GetValue(shader) is Delegate func && + fieldInfo.GetValue(shader) is Delegate func && (func.Method.IsStatic || func.Method.DeclaringType!.IsStatelessDelegateContainer()) && (HlslKnownTypes.IsKnownScalarType(func.Method.ReturnType) || HlslKnownTypes.IsKnownVectorType(func.Method.ReturnType)) && fieldType.GenericTypeArguments.All(type => HlslKnownTypes.IsKnownScalarType(type) ||