From 92b293422909da7c35bc7c381e978d84d9631d19 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 23 Nov 2021 19:22:58 +0100 Subject: [PATCH] Add [EmbeddedBytecode] attribute --- .../ComputeSharp.NetStandard.projitems | 1 + .../CompilerServices}/IsExternalInit.cs | 6 +- .../Attributes/EmbeddedBytecodeAttribute.cs | 58 +++++++++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) rename src/{ComputeSharp.SourceGenerators/System.Runtime.CompilerServices => ComputeSharp.NetStandard/System/Runtime/CompilerServices}/IsExternalInit.cs (54%) create mode 100644 src/ComputeSharp/Core/Attributes/EmbeddedBytecodeAttribute.cs diff --git a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems index 7c78a112..5e1d264c 100644 --- a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems +++ b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems @@ -18,6 +18,7 @@ + diff --git a/src/ComputeSharp.SourceGenerators/System.Runtime.CompilerServices/IsExternalInit.cs b/src/ComputeSharp.NetStandard/System/Runtime/CompilerServices/IsExternalInit.cs similarity index 54% rename from src/ComputeSharp.SourceGenerators/System.Runtime.CompilerServices/IsExternalInit.cs rename to src/ComputeSharp.NetStandard/System/Runtime/CompilerServices/IsExternalInit.cs index 8c84cb00..a914bf06 100644 --- a/src/ComputeSharp.SourceGenerators/System.Runtime.CompilerServices/IsExternalInit.cs +++ b/src/ComputeSharp.NetStandard/System/Runtime/CompilerServices/IsExternalInit.cs @@ -1,8 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.ComponentModel; +using System.ComponentModel; namespace System.Runtime.CompilerServices; diff --git a/src/ComputeSharp/Core/Attributes/EmbeddedBytecodeAttribute.cs b/src/ComputeSharp/Core/Attributes/EmbeddedBytecodeAttribute.cs new file mode 100644 index 00000000..8dbbf759 --- /dev/null +++ b/src/ComputeSharp/Core/Attributes/EmbeddedBytecodeAttribute.cs @@ -0,0 +1,58 @@ +using System; + +namespace ComputeSharp; + +/// +/// An attribute that indicates that a given shader should be precompiled at build time and embedded +/// directly into the containing assembly as static bytecode, to avoid compiling it at runtime. +/// +/// This attribute can be used to annotate shader types as follows: +/// +/// [EmbeddedBytecode(8, 8, 1)] +/// struct MyShader : IComputeShader +/// { +/// } +/// +/// +/// +/// This attribute can only be used when the shader type being annotated does not require dynamic +/// features. Specifically, this attribute is not supported if the shader captures any delegates. +/// +/// The runtime compilation will automatically be skipped if the shader is invoked using matching +/// thread count values, otherwise the usual runtime compilation will be used as fallback. In case +/// the fallback support is not needed and all shaders in use are being precompiled, it is possible +/// to skip bundling the native dxcompiler.dll and dxil.dll libraries included with +/// ComputeSharp entirely, resulting in a smaller binary size in consuming applications. To do this, +/// the PackageReference item for the library should be annotated with ExcludeAssets="native". +/// +[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)] +public sealed class EmbeddedBytecodeAttribute : Attribute +{ + /// + /// Creates a new instance with the specified parameters. + /// + /// The number of threads in each thread group for the X axis. + /// The number of threads in each thread group for the Y axis. + /// The number of threads in each thread group for the Z axis. + public EmbeddedBytecodeAttribute(int threadsX, int threadsY, int threadsZ) + { + ThreadsX = threadsX; + ThreadsY = threadsY; + ThreadsZ = threadsZ; + } + + /// + /// Gets the number of threads in each thread group for the X axis + /// + public int ThreadsX { get; init; } + + /// + /// Gets the number of threads in each thread group for the Y axis + /// + public int ThreadsY { get; init; } + + /// + /// Gets the number of threads in each thread group for the Z axis + /// + public int ThreadsZ { get; init; } +}