Code refactoring, removed unnecessary code
このコミットが含まれているのは:
1 親
d70dc975eb
コミット
47ca3adf0c
2ファイル変更
+118
-135
検索条件に一致するファイルはありません
@@ -12,12 +12,22 @@ using ComputeSharp.Shaders.Translation.Models;
|
||||
|
||||
namespace ComputeSharp.Shaders.Translation
|
||||
{
|
||||
/// <inheritdoc cref="ShaderLoader"/>
|
||||
internal sealed partial class ShaderLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// The number of captured graphics resources (buffers)
|
||||
/// </summary>
|
||||
private int _ResourcesCount;
|
||||
|
||||
/// <summary>
|
||||
/// The size in bytes of the buffer with the captured variables
|
||||
/// </summary>
|
||||
private int _VariablesByteSize = 3 * sizeof(int); // X, Y and Z threads count
|
||||
|
||||
// The cached loader method
|
||||
private DispatchDataLoader? _DispatchDataLoader;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="List{T}"/> of <see cref="ReadableMember"/> instances mapping the captured variables in the current shader
|
||||
/// </summary>
|
||||
@@ -29,108 +39,18 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// <param name="obj">The shader closure instance to load the data from</param>
|
||||
/// <param name="r0">A reference to the buffer with all the captured <see cref="GraphicsResource"/> instances</param>
|
||||
/// <param name="r1">A reference to the buffer to use to store all the captured value types</param>
|
||||
private delegate void MembersLoader(object obj, ref GraphicsResource r0, ref byte r1);
|
||||
|
||||
// The cached loader method
|
||||
private MembersLoader? _MembersLoader;
|
||||
private delegate void DispatchDataLoader(object obj, ref GraphicsResource r0, ref byte r1);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a new <see cref="DispatchData"/> instance with all the captured data for the current shader instance
|
||||
/// </summary>
|
||||
/// <param name="action">The input <see cref="Action{T}"/> representing the compute shader to run</param>
|
||||
/// <param name="x">The number of iterations to run on the X axis</param>
|
||||
/// <param name="y">The number of iterations to run on the Y axis</param>
|
||||
/// <param name="z">The number of iterations to run on the Z axis</param>
|
||||
[Pure]
|
||||
public DispatchData GetDispatchData(Action<ThreadIds> action, int x, int y, int z)
|
||||
{
|
||||
if (_MembersLoader == null)
|
||||
{
|
||||
// Mapping of parent members to index of the relative local variable
|
||||
var map =
|
||||
_CapturedMembers
|
||||
.Where(m => !m.IsStatic && m.Parents != null)
|
||||
.SelectMany(m => m.Parents)
|
||||
.Distinct()
|
||||
.Select((m, i) => (Member: m, Index: i))
|
||||
.ToDictionary(p => (object)p.Member, p => p.Index + 1);
|
||||
object root = new object(); // Placeholder
|
||||
map.Add(root, 0);
|
||||
|
||||
// Set of indices of the loaded parent members
|
||||
HashSet<int> loaded = new HashSet<int>(new [] { 0 });
|
||||
|
||||
// Build the dynamic IL method
|
||||
_MembersLoader = DynamicMethod<MembersLoader>.New(il =>
|
||||
{
|
||||
// Loads a given member on the top of the execution stack
|
||||
void LoadMember(ReadableMember member)
|
||||
{
|
||||
if (!member.IsStatic)
|
||||
{
|
||||
// Load the parent instance on the execution stack if the member isn't static
|
||||
int index = map[member.Parents?.Last() ?? root];
|
||||
if (loaded.Contains(index)) il.EmitLoadLocal(index);
|
||||
else
|
||||
{
|
||||
// Seek upwards to find the most in depth loaded parent
|
||||
int i = member.Parents!.Count - 1;
|
||||
while (i > 0 && !loaded.Contains(map[member.Parents[i]])) i--;
|
||||
|
||||
// Load the local variables for all the parents of the current member
|
||||
if (i == 0) il.EmitLoadLocal(0);
|
||||
for (; i < member.Parents.Count; i++)
|
||||
{
|
||||
il.Emit(OpCodes.Ldfld, member.Parents[i]);
|
||||
il.EmitStoreLocal(map[member.Parents[i]]);
|
||||
il.EmitLoadLocal(map[member.Parents[i]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
il.EmitReadMember(member);
|
||||
}
|
||||
|
||||
// Declare the local variables
|
||||
il.DeclareLocal(ShaderType);
|
||||
foreach (ReadableMember member in map.OrderBy(p => p.Value).Skip(1).Select(p => p.Key))
|
||||
{
|
||||
il.DeclareLocal(member.MemberType);
|
||||
}
|
||||
|
||||
// Cast the closure instance and assign it to the local variable
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
il.EmitCastOrUnbox(ShaderType);
|
||||
il.Emit(OpCodes.Stloc_0);
|
||||
|
||||
// Handle all the captured members, both resources and variables
|
||||
foreach (ReadableMember member in _CapturedMembers)
|
||||
{
|
||||
if (HlslKnownTypes.IsKnownBufferType(member.MemberType))
|
||||
{
|
||||
// Load the offset address into the resource buffers
|
||||
il.Emit(OpCodes.Ldarg_1);
|
||||
if (_ResourcesCount > 0) il.EmitAddOffset<GraphicsResource>(_ResourcesCount);
|
||||
_ResourcesCount++;
|
||||
}
|
||||
else if (HlslKnownTypes.IsKnownScalarType(member.MemberType) || HlslKnownTypes.IsKnownVectorType(member.MemberType))
|
||||
{
|
||||
// Calculate the right offset with 16-bytes padding (HLSL constant buffer)
|
||||
int size = Marshal.SizeOf(member.MemberType);
|
||||
if (_VariablesByteSize % 16 > 16 - size) _VariablesByteSize += 16 - _VariablesByteSize % 16;
|
||||
|
||||
// Load the target address into the variables buffer
|
||||
il.Emit(OpCodes.Ldarg_2);
|
||||
il.EmitAddOffset(_VariablesByteSize);
|
||||
_VariablesByteSize += size;
|
||||
}
|
||||
else throw new InvalidOperationException($"Invalid captured member of type {member.MemberType}");
|
||||
|
||||
/* Load the current member accordingly.
|
||||
* When this method returns, the value of the current
|
||||
* member will be at the top of the execution stack */
|
||||
LoadMember(member);
|
||||
|
||||
il.EmitStoreToAddress(member.MemberType);
|
||||
}
|
||||
|
||||
il.Emit(OpCodes.Ret);
|
||||
});
|
||||
}
|
||||
|
||||
// Resources and variables buffers
|
||||
GraphicsResource[] resources = ArrayPool<GraphicsResource>.Shared.Rent(_ResourcesCount);
|
||||
byte[] variables = ArrayPool<byte>.Shared.Rent(_VariablesByteSize);
|
||||
@@ -143,9 +63,108 @@ namespace ComputeSharp.Shaders.Translation
|
||||
Unsafe.Add(ref Unsafe.As<byte, int>(ref r1), 2) = z;
|
||||
|
||||
// Invoke the dynamic method to extract the captured data
|
||||
_MembersLoader(action.Target, ref r0, ref r1);
|
||||
DispatchDataLoader loader = _DispatchDataLoader ??= BuildDispatchDataLoader();
|
||||
loader(action.Target, ref r0, ref r1);
|
||||
|
||||
return new DispatchData(resources, _ResourcesCount, variables, _VariablesByteSize);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds a new <see cref="DispatchDataLoader"/> instance that loads the dispatch data for the current shader type
|
||||
/// </summary>
|
||||
[Pure]
|
||||
private DispatchDataLoader BuildDispatchDataLoader()
|
||||
{
|
||||
// Mapping of parent members to index of the relative local variable
|
||||
var map =
|
||||
_CapturedMembers
|
||||
.Where(m => !m.IsStatic && m.Parents != null)
|
||||
.SelectMany(m => m.Parents)
|
||||
.Distinct()
|
||||
.Select((m, i) => (Member: m, Index: i))
|
||||
.ToDictionary(p => (object)p.Member, p => p.Index + 1);
|
||||
object root = new object(); // Placeholder
|
||||
map.Add(root, 0);
|
||||
|
||||
// Set of indices of the loaded parent members
|
||||
HashSet<int> loaded = new HashSet<int>(new[] { 0 });
|
||||
|
||||
// Build the dynamic IL method
|
||||
return DynamicMethod<DispatchDataLoader>.New(il =>
|
||||
{
|
||||
// Loads a given member on the top of the execution stack
|
||||
void LoadMember(ReadableMember member)
|
||||
{
|
||||
if (!member.IsStatic)
|
||||
{
|
||||
// Load the parent instance on the execution stack if the member isn't static
|
||||
int index = map[member.Parents?.Last() ?? root];
|
||||
if (loaded.Contains(index)) il.EmitLoadLocal(index);
|
||||
else
|
||||
{
|
||||
// Seek upwards to find the most in depth loaded parent
|
||||
int i = member.Parents!.Count - 1;
|
||||
while (i > 0 && !loaded.Contains(map[member.Parents[i]])) i--;
|
||||
|
||||
// Load the local variables for all the parents of the current member
|
||||
if (i == 0) il.EmitLoadLocal(0);
|
||||
for (; i < member.Parents.Count; i++)
|
||||
{
|
||||
il.Emit(OpCodes.Ldfld, member.Parents[i]);
|
||||
il.EmitStoreLocal(map[member.Parents[i]]);
|
||||
il.EmitLoadLocal(map[member.Parents[i]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
il.EmitReadMember(member);
|
||||
}
|
||||
|
||||
// Declare the local variables
|
||||
il.DeclareLocal(ShaderType);
|
||||
foreach (ReadableMember member in map.OrderBy(p => p.Value).Skip(1).Select(p => p.Key))
|
||||
{
|
||||
il.DeclareLocal(member.MemberType);
|
||||
}
|
||||
|
||||
// Cast the closure instance and assign it to the local variable
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
il.EmitCastOrUnbox(ShaderType);
|
||||
il.Emit(OpCodes.Stloc_0);
|
||||
|
||||
// Handle all the captured members, both resources and variables
|
||||
foreach (ReadableMember member in _CapturedMembers)
|
||||
{
|
||||
if (HlslKnownTypes.IsKnownBufferType(member.MemberType))
|
||||
{
|
||||
// Load the offset address into the resource buffers
|
||||
il.Emit(OpCodes.Ldarg_1);
|
||||
if (_ResourcesCount > 0) il.EmitAddOffset<GraphicsResource>(_ResourcesCount);
|
||||
_ResourcesCount++;
|
||||
}
|
||||
else if (HlslKnownTypes.IsKnownScalarType(member.MemberType) || HlslKnownTypes.IsKnownVectorType(member.MemberType))
|
||||
{
|
||||
// Calculate the right offset with 16-bytes padding (HLSL constant buffer)
|
||||
int size = Marshal.SizeOf(member.MemberType);
|
||||
if (_VariablesByteSize % 16 > 16 - size) _VariablesByteSize += 16 - _VariablesByteSize % 16;
|
||||
|
||||
// Load the target address into the variables buffer
|
||||
il.Emit(OpCodes.Ldarg_2);
|
||||
il.EmitAddOffset(_VariablesByteSize);
|
||||
_VariablesByteSize += size;
|
||||
}
|
||||
else throw new InvalidOperationException($"Invalid captured member of type {member.MemberType}");
|
||||
|
||||
/* Load the current member accordingly.
|
||||
* When this method returns, the value of the current
|
||||
* member will be at the top of the execution stack */
|
||||
LoadMember(member);
|
||||
|
||||
il.EmitStoreToAddress(member.MemberType);
|
||||
}
|
||||
|
||||
il.Emit(OpCodes.Ret);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using ComputeSharp.Shaders.Mappings;
|
||||
using ComputeSharp.Shaders.Renderer.Models.Fields;
|
||||
@@ -73,41 +72,6 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// </summary>
|
||||
public RootParameter1[] RootParameters => _RootParameters ??= DescriptorRanges.Select(range => new RootParameter1(new RootDescriptorTable1(range), ShaderVisibility.All)).ToArray();
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="List{T}"/> of <see cref="ReadableMember"/> instances mapping the captured buffers in the current shader
|
||||
/// </summary>
|
||||
private readonly List<ReadableMember> _BufferMembers = new List<ReadableMember>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ordered collection of buffers used as fields in the current shader
|
||||
/// </summary>
|
||||
/// <param name="action">The <see cref="Action{T}"/> to use to build the shader</param>
|
||||
public IEnumerable<(int Index, GraphicsResource Resource)> GetBuffers(Action<ThreadIds> action)
|
||||
{
|
||||
int index = 1;
|
||||
foreach (var item in _BufferMembers)
|
||||
{
|
||||
yield return (index++, (GraphicsResource)item.DangerousGetValue(action.Target));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="List{T}"/> of <see cref="ReadableMember"/> instances mapping the captured scalar/vector variables in the current shader
|
||||
/// </summary>
|
||||
private readonly List<ReadableMember> _VariableMembers = new List<ReadableMember>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of values of the captured fields for the current shader
|
||||
/// </summary>
|
||||
/// <param name="action">The <see cref="Action{T}"/> to use to build the shader</param>
|
||||
public IEnumerable<object> GetVariables(Action<ThreadIds> action)
|
||||
{
|
||||
foreach (var item in _VariableMembers)
|
||||
{
|
||||
yield return item.DangerousGetValue(action.Target);
|
||||
}
|
||||
}
|
||||
|
||||
private readonly List<HlslBufferInfo> _BuffersList = new List<HlslBufferInfo>();
|
||||
|
||||
/// <summary>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする