Rename ComputeSharp.Dynamic to ComputeSharp.Dxc

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-17 03:10:38 +02:00
1 親 c7bbec8df0
コミット 862ea5a51b
24ファイル変更
+37 -37

検索条件に一致するファイルはありません

+54
ファイルの表示
@@ -0,0 +1,54 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Platforms>AnyCPU;x64;ARM64</Platforms>
<SupportedOSVersion>windows6.2</SupportedOSVersion>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="ComputeSharp.Tests.Internals, PublicKey=$(AssemblySignPublicKey)" />
</ItemGroup>
<!-- Reference to the main non-dynamic library-->
<ItemGroup>
<ProjectReference Include="..\ComputeSharp\ComputeSharp.csproj" />
</ItemGroup>
<Choose>
<!--
When building locally for x64/ARM64 (using the project through a project reference),
just copy the right native libraries to the output directory, with no subfolders.
-->
<When Condition="'$(Platform)' == 'x64' OR '$(CI_RUNNER_DOTNET_TEST_PLATFORM)' == 'x64'">
<ItemGroup>
<None Include="Libraries\x64\dxcompiler.dll" Link="dxcompiler.dll" CopyToOutputDirectory="PreserveNewest" Visible="False" />
<None Include="Libraries\x64\dxil.dll" Link="dxil.dll" CopyToOutputDirectory="PreserveNewest" Visible="False" />
</ItemGroup>
</When>
<When Condition="'$(Platform)' == 'ARM64' OR '$(CI_RUNNER_DOTNET_TEST_PLATFORM)' == 'x64'">
<ItemGroup>
<None Include="Libraries\arm64\dxcompiler.dll" Link="dxcompiler.dll" CopyToOutputDirectory="PreserveNewest" Visible="False" />
<None Include="Libraries\arm64\dxil.dll" Link="dxil.dll" CopyToOutputDirectory="PreserveNewest" Visible="False" />
</ItemGroup>
</When>
<!--
When no RID is specified, it means the NuGet package is being packed. In this
case, copy all native libraries to the right runtimes/<RID>/native directory,
which will automatically be used by NuGet to deploy the right libs to consumers.
Still keep the link target to get the libraries to be copied to the output, which
allows them to be found even when running the library locally in Any CPU. This will
make life easier for developers using a project reference with an incorrect configuration,
while still not causing any change in the final NuGet package (Link isn't looked up there).
-->
<Otherwise>
<ItemGroup>
<None Include="Libraries\x64\dxcompiler.dll" Link="runtimes\win-x64\native\dxcompiler.dll" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath="\runtimes\win-x64\native" Visible="False" />
<None Include="Libraries\x64\dxil.dll" Link="runtimes\win-x64\native\dxil.dll" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath="\runtimes\win-x64\native" Visible="False" />
<None Include="Libraries\arm64\dxcompiler.dll" Link="runtimes\win-arm64\native\dxcompiler.dll" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath="\runtimes\win-arm64\native" Visible="False" />
<None Include="Libraries\arm64\dxil.dll" Link="runtimes\win-arm64\native\dxil.dll" CopyToOutputDirectory="PreserveNewest" Pack="true" PackagePath="\runtimes\win-arm64\native" Visible="False" />
</ItemGroup>
</Otherwise>
</Choose>
</Project>
+77
ファイルの表示
@@ -0,0 +1,77 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
namespace ComputeSharp.Interop;
/// <inheritdoc/>
partial class ReflectionServices
{
/// <summary>
/// Initializes the DLL resolvers for the dxcompiler.dll and dxil.dll libraries.
/// </summary>
static ReflectionServices()
{
// 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\net8.0\MyApp.dll", which would crash at runtime.
NativeLibrary.SetDllImportResolver(typeof(ReflectionServices).Assembly, OnDllImport);
}
/// <summary>
/// The custom <see cref="DllImportResolver"/> for this assembly.
/// </summary>
/// <inheritdoc cref="DllImportResolver"/>
private static IntPtr OnDllImport(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
if (libraryName is not "dxcompiler")
{
return IntPtr.Zero;
}
string rid = RuntimeInformation.ProcessArchitecture switch
{
Architecture.X64 => "win-x64",
Architecture.Arm64 => "win-arm64",
_ => default(NotSupportedException).Throw<string>()
};
// 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\{rid}\native");
bool isNuGetRuntimeLibrariesDirectoryPresent = Directory.Exists(nugetNativeLibsPath);
if (isNuGetRuntimeLibrariesDirectoryPresent)
{
string dxcompilerPath = Path.Combine(AppContext.BaseDirectory, $@"runtimes\{rid}\native\dxcompiler.dll");
string dxilPath = Path.Combine(AppContext.BaseDirectory, $@"runtimes\{rid}\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;
}
}
+110
ファイルの表示
@@ -0,0 +1,110 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Descriptors;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
#if !NET6_0_OR_GREATER
using DirectX = TerraFX.Interop.DirectX.DirectX2;
#endif
namespace ComputeSharp.Interop;
/// <summary>
/// Provides methods to extract reflection info on compute shaders generated using this library.
/// </summary>
public static partial class ReflectionServices
{
/// <summary>
/// Gets the shader info associated with a given compute shader.
/// </summary>
/// <typeparam name="T">The type of compute shader to retrieve info for.</typeparam>
/// <returns>The resulting <see cref="ShaderInfo"/> instance.</returns>
public static ShaderInfo GetShaderInfo<T>()
where T : struct, IComputeShader, IComputeShaderDescriptor<T>
{
return GetNonGenericShaderInfo<T>();
}
/// <summary>
/// Gets the shader info associated with a given pixel shader.
/// </summary>
/// <typeparam name="T">The type of pixel shader to retrieve info for.</typeparam>
/// <typeparam name="TPixel">The type of pixels being processed by the shader.</typeparam>
/// <returns>The resulting <see cref="ShaderInfo"/> instance.</returns>
public static ShaderInfo GetShaderInfo<T, TPixel>()
where T : struct, IComputeShader<TPixel>, IComputeShaderDescriptor<T>
where TPixel : unmanaged
{
return GetNonGenericShaderInfo<T>();
}
/// <summary>
/// Gets the shader info associated with a given shader of any type.
/// </summary>
/// <typeparam name="T">The type of shader to retrieve info for.</typeparam>
/// <returns>The resulting <see cref="ShaderInfo"/> instance.</returns>
private static unsafe ShaderInfo GetNonGenericShaderInfo<T>()
where T : struct, IComputeShaderDescriptor<T>
{
Unsafe.SkipInit(out T shader);
ReadOnlyMemory<byte> hlslBytecode = shader.HlslBytecode;
using ComPtr<IDxcUtils> dxcUtils = default;
// Load the DXC compiler (the package will bundle the necessary .dll-s)
DirectX.DxcCreateInstance(
(Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_DxcLibrary)),
Windows.__uuidof<IDxcUtils>(),
(void**)dxcUtils.GetAddressOf()).Assert();
using ComPtr<ID3D12ShaderReflection> d3D12ShaderReflection = default;
// Reflect over the shader (this will fail if reflection metadata has been stripped).
// Since that's just opt-in from users, that is the expected behavior, so not an issue.
fixed (byte* hlslBytecodePtr = hlslBytecode.Span)
{
DxcBuffer dxcBuffer = default;
dxcBuffer.Ptr = hlslBytecodePtr;
dxcBuffer.Size = (uint)hlslBytecode.Length;
dxcUtils.Get()->CreateReflection(
&dxcBuffer,
Windows.__uuidof<ID3D12ShaderReflection>(),
(void**)d3D12ShaderReflection.GetAddressOf()).Assert();
}
D3D12_SHADER_DESC d3D12ShaderDescription;
d3D12ShaderReflection.Get()->GetDesc(&d3D12ShaderDescription).Assert();
return new(
CompilerVersion: new string(d3D12ShaderDescription.Creator),
HlslSource: shader.HlslSource,
HlslBytecode: hlslBytecode,
ConstantBufferCount: d3D12ShaderDescription.ConstantBuffers,
BoundResourceCount: d3D12ShaderDescription.BoundResources,
InstructionCount: d3D12ShaderDescription.InstructionCount,
TemporaryRegisterCount: d3D12ShaderDescription.TempRegisterCount,
TemporaryArrayCount: d3D12ShaderDescription.TempArrayCount,
ConstantDefineCount: d3D12ShaderDescription.DefCount,
DeclarationCount: d3D12ShaderDescription.DclCount,
TextureNormalInstructions: d3D12ShaderDescription.TextureNormalInstructions,
TextureLoadInstructionCount: d3D12ShaderDescription.TextureLoadInstructions,
TextureStoreInstructionCount: d3D12ShaderDescription.cTextureStoreInstructions,
FloatInstructionCount: d3D12ShaderDescription.FloatInstructionCount,
IntInstructionCount: d3D12ShaderDescription.IntInstructionCount,
UIntInstructionCount: d3D12ShaderDescription.UintInstructionCount,
StaticFlowControlInstructionCount: d3D12ShaderDescription.StaticFlowControlCount,
DynamicFlowControlInstructionCount: d3D12ShaderDescription.DynamicFlowControlCount,
EmitInstructionCount: d3D12ShaderDescription.EmitInstructionCount,
BarrierInstructionCount: d3D12ShaderDescription.cBarrierInstructions,
InterlockedInstructionCount: d3D12ShaderDescription.cInterlockedInstructions,
BitwiseInstructionCount: d3D12ShaderReflection.Get()->GetBitwiseInstructionCount(),
MovcInstructionCount: d3D12ShaderReflection.Get()->GetMovcInstructionCount(),
MovInstructionCount: d3D12ShaderReflection.Get()->GetMovInstructionCount(),
InterfaceSlotCount: d3D12ShaderReflection.Get()->GetNumInterfaceSlots(),
RequiresDoublePrecisionSupport: (d3D12ShaderReflection.Get()->GetRequiresFlags() & (D3D.D3D_SHADER_REQUIRES_DOUBLES | D3D.D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS)) != 0);
}
}
+60
ファイルの表示
@@ -0,0 +1,60 @@
using System;
namespace ComputeSharp.Interop;
/// <summary>
/// A model representing reflection info for a given compute shader.
/// </summary>
/// <param name="CompilerVersion">The compiler version used to build the shader.</param>
/// <param name="HlslSource">The HLSL source code the shader was built from.</param>
/// <param name="HlslBytecode">The HLSL bytecode for the shader.</param>
/// <param name="ConstantBufferCount">The number of shader constant buffers.</param>
/// <param name="BoundResourceCount">The number of resource (textures and buffers) bound to a shader.</param>
/// <param name="InstructionCount">The number of intermediate-language instructions in the compiled shader.</param>
/// <param name="TemporaryRegisterCount">The number of temporary registers in the compiled shader.</param>
/// <param name="TemporaryArrayCount">The number of temporary arrays used.</param>
/// <param name="ConstantDefineCount">The number of constant defines.</param>
/// <param name="DeclarationCount">The number of declarations (input + output).</param>
/// <param name="TextureNormalInstructions">The number of non-categorized texture instructions.</param>
/// <param name="TextureLoadInstructionCount"> The number of texture load instructions.</param>
/// <param name="TextureStoreInstructionCount">The number of texture write instructions.</param>
/// <param name="FloatInstructionCount">The number of floating point arithmetic instructions used.</param>
/// <param name="IntInstructionCount">The number of signed integer arithmetic instructions used.</param>
/// <param name="UIntInstructionCount">The number of unsigned integer arithmetic instructions used.</param>
/// <param name="StaticFlowControlInstructionCount">The number of static flow control instructions used.</param>
/// <param name="DynamicFlowControlInstructionCount">The number of dynamic flow control instructions used.</param>
/// <param name="EmitInstructionCount">The number of emit instructions used.</param>
/// <param name="BarrierInstructionCount">The number of barrier instructions used.</param>
/// <param name="InterlockedInstructionCount">The number of interlocked instructions used.</param>
/// <param name="BitwiseInstructionCount">The number of bitwise instructions used.</param>
/// <param name="MovcInstructionCount">The number of <c>movc</c> instructions used.</param>
/// <param name="MovInstructionCount">The number of <c>mov</c> instructions used.</param>
/// <param name="InterfaceSlotCount">The number of interface slots used.</param>
/// <param name="RequiresDoublePrecisionSupport">Indicates whether support for double precision floating point numbers is required.</param>
public sealed record ShaderInfo(
string CompilerVersion,
string HlslSource,
ReadOnlyMemory<byte> HlslBytecode,
uint ConstantBufferCount,
uint BoundResourceCount,
uint InstructionCount,
uint TemporaryRegisterCount,
uint TemporaryArrayCount,
uint ConstantDefineCount,
uint DeclarationCount,
uint TextureNormalInstructions,
uint TextureLoadInstructionCount,
uint TextureStoreInstructionCount,
uint FloatInstructionCount,
uint IntInstructionCount,
uint UIntInstructionCount,
uint StaticFlowControlInstructionCount,
uint DynamicFlowControlInstructionCount,
uint EmitInstructionCount,
uint BarrierInstructionCount,
uint InterlockedInstructionCount,
uint BitwiseInstructionCount,
uint MovcInstructionCount,
uint MovInstructionCount,
uint InterfaceSlotCount,
bool RequiresDoublePrecisionSupport);
バイナリファイルは表示されません。
バイナリファイルは表示されません。
バイナリファイルは表示されません。
バイナリファイルは表示されません。