Added .Shaders support for .NET Standard 2.0
このコミットが含まれているのは:
1 親
e27547fb43
コミット
880f501b55
8ファイル変更
+146
-90
検索条件に一致するファイルはありません
@@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<TargetFrameworks>netstandard2.0;net5.0</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
#if NETSTANDARD2_0
|
||||
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace System.Runtime.CompilerServices
|
||||
{
|
||||
/// <summary>
|
||||
/// Reserved to be used by the compiler for tracking metadata.
|
||||
/// </summary>
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
internal static class IsExternalInit
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
namespace System.Reflection
|
||||
{
|
||||
/// <summary>
|
||||
/// Extensions for the <see cref="MethodInfo"/> type.
|
||||
/// </summary>
|
||||
internal static class MethodInfoExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a delegate of type T from a given method.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of delegate to create.</typeparam>
|
||||
/// <param name="method">The input <see cref="MethodInfo"/>.</param>
|
||||
/// <returns>The delegate for this method.</returns>
|
||||
public static T CreateDelegate<T>(this MethodInfo method)
|
||||
where T : Delegate
|
||||
{
|
||||
return (T)method.CreateDelegate(typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -163,11 +163,11 @@ namespace ComputeSharp.Shaders.Renderer
|
||||
/// Appends the input sequence of characters to the current buffer.
|
||||
/// </summary>
|
||||
/// <param name="value">The input characters to write.</param>
|
||||
public void Append(ReadOnlySpan<char> value)
|
||||
public void Append(string value)
|
||||
{
|
||||
EnsureCapacity(value.Length);
|
||||
|
||||
value.CopyTo(this.array.AsSpan(this.index));
|
||||
value.AsSpan().CopyTo(this.array.AsSpan(this.index));
|
||||
|
||||
this.index += value.Length;
|
||||
}
|
||||
@@ -186,11 +186,11 @@ namespace ComputeSharp.Shaders.Renderer
|
||||
/// Appends the input sequence of characters to the current buffer, with a trailing new line.
|
||||
/// </summary>
|
||||
/// <param name="value">The input characters to write.</param>
|
||||
public void AppendLine(ReadOnlySpan<char> value)
|
||||
public void AppendLine(string value)
|
||||
{
|
||||
EnsureCapacity(value.Length + 1);
|
||||
|
||||
value.CopyTo(this.array.AsSpan(this.index));
|
||||
value.AsSpan().CopyTo(this.array.AsSpan(this.index));
|
||||
|
||||
this.index += value.Length;
|
||||
|
||||
|
||||
@@ -54,10 +54,16 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
#if NET5_0
|
||||
ref ulong r0 = ref MemoryMarshal.GetArrayDataReference(this.resourcesArray);
|
||||
ref D3D12_GPU_DESCRIPTOR_HANDLE r1 = ref Unsafe.As<ulong, D3D12_GPU_DESCRIPTOR_HANDLE>(ref r0);
|
||||
|
||||
return MemoryMarshal.CreateReadOnlySpan(ref r1, this.resourcesCount);
|
||||
#else
|
||||
Span<ulong> span = this.resourcesArray.AsSpan(0, this.resourcesCount);
|
||||
|
||||
return MemoryMarshal.Cast<ulong, D3D12_GPU_DESCRIPTOR_HANDLE>(span);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,11 +75,17 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
#if NET5_0
|
||||
ref byte r0 = ref MemoryMarshal.GetArrayDataReference(this.variablesArray);
|
||||
ref Int4 r1 = ref Unsafe.As<byte, Int4>(ref r0);
|
||||
int length = (int)((uint)this.variablesByteSize / (uint)sizeof(Int4));
|
||||
|
||||
return MemoryMarshal.CreateReadOnlySpan(ref r1, length);
|
||||
#else
|
||||
Span<byte> span = this.variablesArray.AsSpan(0, this.variablesByteSize);
|
||||
|
||||
return MemoryMarshal.Cast<byte, Int4>(span);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
#if NET5_0
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using FX = TerraFX.Interop.Windows;
|
||||
#endif
|
||||
|
||||
namespace ComputeSharp.Shaders.Translation
|
||||
{
|
||||
/// <inheritdoc cref="ShaderCompiler"/>
|
||||
internal static unsafe partial class ShaderCompiler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the DLL resolvers for the dxcompiler.dll and dxil.dll libraries.
|
||||
/// </summary>
|
||||
private static void InitializeDxcLibrariesLoading()
|
||||
{
|
||||
#if NET5_0
|
||||
// Test whether the native libraries are present in the same folder of the executable
|
||||
// (which is the case when the program was built with a runtime identifier), or whether
|
||||
// they are in the "runtimes\win-x64\native" folder in the executable directory.
|
||||
string nugetNativeLibsPath = Path.Combine(AppContext.BaseDirectory, @"runtimes\win-x64\native");
|
||||
bool isNuGetRuntimeLibrariesDirectoryPresent = Directory.Exists(nugetNativeLibsPath);
|
||||
|
||||
// Register a custom library resolver for the two DXC libraries. We need to either manually load the two
|
||||
// libraries from the NuGet directory, if an RID is not in use, or we need to ensure that dxil.dll is
|
||||
// loaded correctly in case the program was executed with the host being in another directory.
|
||||
// This happens when doing eg. "dotnet bin\Debug\net5.0\MyApp.dll", which would crash at runtime.
|
||||
FX.ResolveLibrary += (libraryName, assembly, searchPath) =>
|
||||
{
|
||||
if (libraryName is not "dxcompiler") return IntPtr.Zero;
|
||||
|
||||
if (isNuGetRuntimeLibrariesDirectoryPresent)
|
||||
{
|
||||
string
|
||||
dxcompilerPath = Path.Combine(AppContext.BaseDirectory, @"runtimes\win-x64\native\dxcompiler.dll"),
|
||||
dxilPath = Path.Combine(AppContext.BaseDirectory, @"runtimes\win-x64\native\dxil.dll");
|
||||
|
||||
// Load DXIL first so that DXC doesn't fail to load it, and then DXIL, both from the NuGet path
|
||||
if (NativeLibrary.TryLoad(dxilPath, out _) && NativeLibrary.TryLoad(dxcompilerPath, out IntPtr handle))
|
||||
{
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Even when the two libraries are correctly copied next to the executable in use, we load them
|
||||
// manually to ensure the operation is successful. This is to avoid failures in cases such as when
|
||||
// doing "dotnet bin\MyApp.dll", ie. when the host is in another path than the executable in use.
|
||||
// This is probably because DXIL is a native dependency for DXC, but the way Windows loads these
|
||||
// libraries doesn't take into account the .NET concepts of "app directory": neither the current "bin"
|
||||
// directory nor the "process directory", which is "C:\Program Files\dotnet", actually contain the
|
||||
// native library we need, hence the runtime crash. Manually loading the library this way solves this.
|
||||
if (NativeLibrary.TryLoad("dxil", assembly, searchPath, out _) &&
|
||||
NativeLibrary.TryLoad("dxcompiler", assembly, searchPath, out IntPtr handle))
|
||||
{
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
|
||||
return IntPtr.Zero;
|
||||
};
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,6 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Core.Extensions;
|
||||
using ComputeSharp.Exceptions;
|
||||
using TerraFX.Interop;
|
||||
@@ -15,7 +11,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that uses the DXC APIs to compile compute shaders.
|
||||
/// </summary>
|
||||
internal static unsafe class ShaderCompiler
|
||||
internal static unsafe partial class ShaderCompiler
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="IDxcCompiler"/> instance to use to create the bytecode for HLSL sources.
|
||||
@@ -56,64 +52,6 @@ namespace ComputeSharp.Shaders.Translation
|
||||
DxcIncludeHandler = dxcIncludeHandler.Move();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the DLL resolvers for the dxcompiler.dll and dxil.dll libraries.
|
||||
/// </summary>
|
||||
private static void InitializeDxcLibrariesLoading()
|
||||
{
|
||||
// Test whether the native libraries are present in the same folder of the executable
|
||||
// (which is the case when the program was built with a runtime identifier), or whether
|
||||
// they are in the "runtimes\win-x64\native" folder in the executable directory.
|
||||
string nugetNativeLibsPath = Path.Combine(AppContext.BaseDirectory, @"runtimes\win-x64\native");
|
||||
bool isNuGetRuntimeLibrariesDirectoryPresent = Directory.Exists(nugetNativeLibsPath);
|
||||
|
||||
// Register a custom library resolver for the two DXC libraries. We need to either manually load the two
|
||||
// libraries from the NuGet directory, if an RID is not in use, or we need to ensure that dxil.dll is
|
||||
// loaded correctly in case the program was executed with the host being in another directory.
|
||||
// This happens when doing eg. "dotnet bin\Debug\net5.0\MyApp.dll", which would crash at runtime.
|
||||
FX.ResolveLibrary += (n, a, s) => ResolveLibrary(n, a, s, isNuGetRuntimeLibrariesDirectoryPresent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A custom resolver to override the default library resolution behavior for the DXC and DXIL libraries.
|
||||
/// This either loads the libraries from the NuGet directory, or just ensures that DXIL is always loaded.
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="DllImportResolver"/>
|
||||
private static IntPtr ResolveLibrary(string libraryName, Assembly assembly, DllImportSearchPath? searchPath, bool isNuGetRuntimeLibrariesDirectoryPresent)
|
||||
{
|
||||
if (libraryName is not "dxcompiler") return IntPtr.Zero;
|
||||
|
||||
if (isNuGetRuntimeLibrariesDirectoryPresent)
|
||||
{
|
||||
string
|
||||
dxcompilerPath = Path.Combine(AppContext.BaseDirectory, @"runtimes\win-x64\native\dxcompiler.dll"),
|
||||
dxilPath = Path.Combine(AppContext.BaseDirectory, @"runtimes\win-x64\native\dxil.dll");
|
||||
|
||||
// Load DXIL first so that DXC doesn't fail to load it, and then DXIL, both from the NuGet path
|
||||
if (NativeLibrary.TryLoad(dxilPath, out _) && NativeLibrary.TryLoad(dxcompilerPath, out IntPtr handle))
|
||||
{
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Even when the two libraries are correctly copied next to the executable in use, we load them
|
||||
// manually to ensure the operation is successful. This is to avoid failures in cases such as when
|
||||
// doing "dotnet bin\MyApp.dll", ie. when the host is in another path than the executable in use.
|
||||
// This is probably because DXIL is a native dependency for DXC, but the way Windows loads these
|
||||
// libraries doesn't take into account the .NET concepts of "app directory": neither the current "bin"
|
||||
// directory nor the "process directory", which is "C:\Program Files\dotnet", actually contain the
|
||||
// native library we need, hence the runtime crash. Manually loading the library this way solves this.
|
||||
if (NativeLibrary.TryLoad("dxil", assembly, searchPath, out _) &&
|
||||
NativeLibrary.TryLoad("dxcompiler", assembly, searchPath, out IntPtr handle))
|
||||
{
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compiles a new HLSL shader from the input source code.
|
||||
/// </summary>
|
||||
@@ -175,7 +113,6 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// </summary>
|
||||
/// <param name="dxcOperationResult">The input (faulting) operation.</param>
|
||||
/// <returns>This method always throws and never actually returs.</returns>
|
||||
[DoesNotReturn]
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static ComPtr<IDxcBlob> ThrowHslsCompilationException(IDxcOperationResult* dxcOperationResult)
|
||||
{
|
||||
|
||||
@@ -4,10 +4,14 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Graphics;
|
||||
using ComputeSharp.Shaders.Translation.Models;
|
||||
using TerraFX.Interop;
|
||||
#if NET5_0
|
||||
using MemoryMarshal = System.Runtime.InteropServices.MemoryMarshal;
|
||||
#else
|
||||
using MemoryMarshal = ComputeSharp.System.Runtime.InteropServices.MemoryMarshal;
|
||||
#endif
|
||||
|
||||
namespace ComputeSharp.Shaders.Translation
|
||||
{
|
||||
|
||||
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.__Internals;
|
||||
using ComputeSharp.Shaders.Mappings;
|
||||
using ComputeSharp.Shaders.Renderer.Models;
|
||||
@@ -43,9 +42,9 @@ namespace ComputeSharp.Shaders.Translation
|
||||
private uint readWriteBuffersCount;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="List{T}"/> of <see cref="D3D12_DESCRIPTOR_RANGE1"/> items that are required to load the captured values.
|
||||
/// The array of <see cref="D3D12_DESCRIPTOR_RANGE1"/> items that are required to load the captured values.
|
||||
/// </summary>
|
||||
private readonly List<D3D12_DESCRIPTOR_RANGE1> d3D12DescriptorRanges1 = new();
|
||||
private D3D12_DESCRIPTOR_RANGE1[] d3D12DescriptorRanges1;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="List{T}"/> with the <see cref="Renderer.Models.HlslResourceInfo"/> items for the shader fields.
|
||||
@@ -79,7 +78,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// </summary>
|
||||
public ReadOnlySpan<D3D12_DESCRIPTOR_RANGE1> D3D12DescriptorRanges1
|
||||
{
|
||||
get => CollectionsMarshal.AsSpan(this.d3D12DescriptorRanges1);
|
||||
get => this.d3D12DescriptorRanges1;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -107,12 +106,10 @@ namespace ComputeSharp.Shaders.Translation
|
||||
{
|
||||
ShaderLoader<T> @this = new();
|
||||
|
||||
// Reading members through reflection requires an object parameter,
|
||||
// so here we're just boxing the input shader once to avoid allocating
|
||||
// it multiple times in the managed heap while processing the shader.
|
||||
object box = shader;
|
||||
// Reading members through reflection requires an object parameter, so here we're just boxing
|
||||
// the input shader once to avoid allocating it multiple times while processing the shader.
|
||||
@this.d3D12DescriptorRanges1 = @this.LoadFieldsInfo(shader);
|
||||
|
||||
@this.LoadFieldsInfo(box);
|
||||
@this.LoadMethodMetadata();
|
||||
@this.InitializeDispatchDataLoader();
|
||||
|
||||
@@ -123,7 +120,8 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// Loads the fields info for the current shader being loaded.
|
||||
/// </summary>
|
||||
/// <param name="shader">The boxed <typeparamref name="T"/> instance to use to build the shader.</param>
|
||||
private void LoadFieldsInfo(object shader)
|
||||
/// <returns>The array of <see cref="D3D12_DESCRIPTOR_RANGE1"/> items that are required to load the captured values.</returns>
|
||||
private D3D12_DESCRIPTOR_RANGE1[] LoadFieldsInfo(object shader)
|
||||
{
|
||||
IReadOnlyList<FieldInfo> shaderFields = typeof(T).GetFields(
|
||||
BindingFlags.Instance |
|
||||
@@ -135,51 +133,53 @@ namespace ComputeSharp.Shaders.Translation
|
||||
ThrowHelper.ThrowInvalidOperationException("The shader body must contain at least one field");
|
||||
}
|
||||
|
||||
List<D3D12_DESCRIPTOR_RANGE1> d3D12DescriptorRanges1 = new();
|
||||
|
||||
// Descriptor for the buffer for captured scalar/vector variables
|
||||
D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, this.constantBuffersCount++);
|
||||
|
||||
this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1);
|
||||
d3D12DescriptorRanges1.Add(d3D12DescriptorRange1);
|
||||
|
||||
// Inspect the captured fields
|
||||
foreach (FieldInfo fieldInfo in shaderFields)
|
||||
{
|
||||
LoadFieldInfo(shader, fieldInfo);
|
||||
LoadFieldInfo(shader, fieldInfo, d3D12DescriptorRanges1);
|
||||
}
|
||||
|
||||
return d3D12DescriptorRanges1.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a specified <see cref="ReadableMember"/> and adds it to the shader model.
|
||||
/// </summary>
|
||||
/// <param name="shader">The boxed <typeparamref name="T"/> instance to use to build the shader.</param>
|
||||
/// <param name="memberInfo">The target <see cref="FieldInfo"/> to load.</param>
|
||||
private void LoadFieldInfo(object shader, FieldInfo fieldInfo)
|
||||
/// <param name="fieldInfo">The target <see cref="FieldInfo"/> to load.</param>
|
||||
/// <param name="d3D12DescriptorRanges1">The list of discovered <see cref="D3D12_DESCRIPTOR_RANGE1"/> values.</param>
|
||||
private void LoadFieldInfo(object shader, FieldInfo fieldInfo, List<D3D12_DESCRIPTOR_RANGE1> d3D12DescriptorRanges1)
|
||||
{
|
||||
Type fieldType = fieldInfo.FieldType;
|
||||
(string hlslName, string hlslType) = Attribute.Fields[fieldInfo.Name];
|
||||
|
||||
if (HlslKnownTypes.IsConstantBufferType(fieldType))
|
||||
{
|
||||
D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, this.constantBuffersCount);
|
||||
d3D12DescriptorRanges1.Add(new D3D12_DESCRIPTOR_RANGE1(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, this.constantBuffersCount));
|
||||
|
||||
this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1);
|
||||
this.capturedFields.Add(fieldInfo);
|
||||
this.hlslResourceInfo.Add(new HlslResourceInfo.Constant(hlslType, hlslName, (int)this.constantBuffersCount++));
|
||||
this.totalResourceCount++;
|
||||
}
|
||||
else if (HlslKnownTypes.IsReadOnlyResourceType(fieldType))
|
||||
{
|
||||
D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, this.readOnlyBuffersCount);
|
||||
d3D12DescriptorRanges1.Add(new D3D12_DESCRIPTOR_RANGE1(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, this.readOnlyBuffersCount));
|
||||
|
||||
this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1);
|
||||
this.capturedFields.Add(fieldInfo);
|
||||
this.hlslResourceInfo.Add(new HlslResourceInfo.ReadOnly(hlslType, hlslName, (int)this.readOnlyBuffersCount++));
|
||||
this.totalResourceCount++;
|
||||
}
|
||||
else if (HlslKnownTypes.IsReadWriteResourceType(fieldType))
|
||||
{
|
||||
D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, this.readWriteBuffersCount);
|
||||
d3D12DescriptorRanges1.Add(new D3D12_DESCRIPTOR_RANGE1(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, this.readWriteBuffersCount));
|
||||
|
||||
this.d3D12DescriptorRanges1.Add(d3D12DescriptorRange1);
|
||||
this.capturedFields.Add(fieldInfo);
|
||||
this.hlslResourceInfo.Add(new HlslResourceInfo.ReadWrite(hlslType, hlslName, (int)this.readWriteBuffersCount++));
|
||||
this.totalResourceCount++;
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする