diff --git a/src/ComputeSharp/Core/Attributes/CompileOptionsAttribute.cs b/src/ComputeSharp/Core/Attributes/CompileOptionsAttribute.cs new file mode 100644 index 00000000..4348a06f --- /dev/null +++ b/src/ComputeSharp/Core/Attributes/CompileOptionsAttribute.cs @@ -0,0 +1,28 @@ +using System; + +namespace ComputeSharp; + +/// +/// An attribute that indicates the compile options that should be used when compiling a given compute shader. +/// +/// This attribute can be used to annotate shader types as follows: +/// +/// [CompileOptions(CompileOptions.IeeeStrictness | CompileOptions.OptimizationLevel3)] +/// struct MyShader : IComputeShader +/// { +/// } +/// +/// +/// +/// This attribute can also be added to a whole assembly, and will be used by default if not overridden by a shader type. +/// +/// +/// The compiler options to use to compile the shader. +[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = false)] +public sealed class CompileOptionsAttribute(CompileOptions options) : Attribute +{ + /// + /// Gets the compile options to use to compile the shader. + /// + public CompileOptions Options { get; } = options; +} \ No newline at end of file diff --git a/src/ComputeSharp/Core/Attributes/Enums/CompileOptions.cs b/src/ComputeSharp/Core/Attributes/Enums/CompileOptions.cs new file mode 100644 index 00000000..10cd0175 --- /dev/null +++ b/src/ComputeSharp/Core/Attributes/Enums/CompileOptions.cs @@ -0,0 +1,147 @@ +using System; + +namespace ComputeSharp; + +/// +/// Flags to control the options that can be used to compile a compute shader. +/// +[Flags] +public enum CompileOptions +{ + /// + /// Enables agressive flattening. + /// + /// + /// This flag maps to -all-resources-bound. + /// + AllResourcesBound = 1 << 0, + + /// + /// Directs the compiler not to validate the generated code against known capabilities and constraints. + /// This option should only be used with shaders that have been successfully compiled in the past. + /// DirectX always validates shaders before it sets them to a device. + /// + /// + /// This flag maps to -Vd. + /// + DisableValidation = 1 << 1, + + /// + /// Directs the compiler to skip optimization steps during code generation. + /// This option should only be used for debug purposes. + /// + /// + /// This flag maps to -Od. + /// + DisableOptimization = 1 << 2, + + /// + /// Directs the compiler to not use flow-control constructs where possible. + /// + /// + /// This flag maps to -Gfa. + /// + AvoidFlowControl = 1 << 3, + + /// + /// Directs the compiler to prefer using flow-control constructs where possible. + /// + /// + /// This flag maps to -Gfp. + /// + PreferFlowControl = 1 << 4, + + /// + /// Forces strict compile, which might not allow for legacy syntax. + /// By default, the compiler disables strictness on deprecated syntax. + /// + /// + /// This flag maps to -Ges.W + /// + EnableStrictness = 1 << 5, + + /// + /// Forces the IEEE strict compile which avoids optimizations that may break IEEE rules. + /// + /// + /// This flag maps to -Gis. + /// + IeeeStrictness = 1 << 6, + + /// + /// Enables backward compatibility mode. + /// + /// + /// This flag maps to -Gec. + /// + EnableBackwardsCompatibility = 1 << 7, + + /// + /// Directs the compiler to use the lowest optimization level. If this option is set, the compiler + /// might produce slower code but produces the code more quickly. This option should be set when + /// shaders are developed iteratively, as it can reduce compilation times. + /// + /// + /// This flag maps to -O0. + /// + OptimizationLevel0 = 1 << 8, + + /// + /// Directs the compiler to use the second lowest optimization level. + /// + /// + /// This flag maps to -O1. + /// + OptimizationLevel1 = OptimizationLevel0 | (1 << 9), + + /// + /// Directs the compiler to use the second highest optimization level. + /// + /// + /// This flag maps to -O2. + /// + OptimizationLevel2 = OptimizationLevel1 | (1 << 10), + + /// + /// Directs the compiler to use the highest optimization level. If this option is set, the compiler + /// produces the best possible code but might take significantly longer to do so. This option should + /// be set for final builds of an application when performance is the most important factor. + /// + /// + /// This flag maps to -O3. + /// + OptimizationLevel3 = OptimizationLevel2 | (1 << 11), + + /// + /// Directs the compiler to treat all warnings as errors when it compiles the shader code. This + /// option is recommended for new shader code, so that warnings can be resolved and the + /// number of hard-to-find code defects can be minimized. + /// + /// + /// This flag maps to -WX. + /// + WarningsAreErrors = 1 << 12, + + /// + /// Directs the compiler that UAVs (uniform access views) and SRVs (shader resource views) may alias. + /// + /// + /// This flag maps to -res-may-alias. + /// + ResourcesMayAlias = 1 << 13, + + /// + /// Strips the reflection data from the generated shader bytecode. The bytecode size will be significantly lower, + /// but trying to perform reflection on the shader will fail at runtime. Recommend if not reflection is done. + /// + StripReflectionData = 1 << 14, + + /// + /// The default options for shaders compiled by ComputeSharp. + /// + /// + /// This option does not include a flag to pack matrices in column-major order on input and output from the shader, + /// as that is always enabled by default (it is necessary to ensure the constant buffer matches the managed layout). + /// + Default = OptimizationLevel3 | WarningsAreErrors +} \ No newline at end of file