Пример #1
0
        byte[] LoadShader(DxcShaderStage shaderStage, string path, string entryPoint)
        {
            var result = DxcCompiler.Compile(shaderStage, File.ReadAllText(path), entryPoint);

            if (result.GetStatus() != SharpGen.Runtime.Result.Ok)
            {
                throw new Exception(result.GetErrors());
            }
            return(result.GetResult().ToArray());
        }
Пример #2
0
        public static byte[] Compile(DxcShaderStage shaderStage, string source, string entryPoint, string sourceName, DxcCompilerOptions options)
        {
            IDxcOperationResult result = DxcCompiler.Compile(shaderStage, source, entryPoint, sourceName, options);

            if (result.GetStatus() == 0)
            {
                IDxcBlob blob = result.GetResult();
                return(Dxc.GetBytesFromBlob(blob));
            }
            else
            {
                string resultText = Dxc.GetStringFromBlob(DxcCompiler.Library, result.GetErrors());
                throw new Exception(resultText);
            }
        }
Пример #3
0
        static bool CompileShader(string shader, string shaderName, DxcShaderStage stage, DxcCompilerOptions options, out IDxcBlob blob, out string error)
        {
            error = "";
            blob  = null;
            IDxcOperationResult result = DxcCompiler.Compile(stage, shader, "main", shaderName, options);

            if (result.GetStatus() != 0)
            {
                unsafe
                {
                    IntPtr errorPtr = (IntPtr)result.GetErrors().GetBufferPointer();
                    error = Marshal.PtrToStringAnsi(errorPtr);
                }

                return(false);
            }

            blob = result.GetResult();
            return(true);
        }
Пример #4
0
        private static string GetShaderProfile(DxcShaderStage shaderStage, DxcShaderModel shaderModel)
        {
            var shaderProfile = "";

            switch (shaderStage)
            {
            case DxcShaderStage.VertexShader:
                shaderProfile = "vs";
                break;

            case DxcShaderStage.PixelShader:
                shaderProfile = "ps";
                break;

            case DxcShaderStage.GeometryShader:
                shaderProfile = "gs";
                break;

            case DxcShaderStage.HullShader:
                shaderProfile = "hs";
                break;

            case DxcShaderStage.DomainShader:
                shaderProfile = "ds";
                break;

            case DxcShaderStage.ComputeShader:
                shaderProfile = "cs";
                break;

            case DxcShaderStage.Library:
                shaderProfile = "lib";
                break;

            default:
                return(string.Empty);
            }

            shaderProfile += $"_{shaderModel.Major}_{shaderModel.Minor}";
            return(shaderProfile);
        }
Пример #5
0
 public static byte[] Compile(DxcShaderStage shaderStage, string source, string entryPoint, string sourceName = "")
 {
     return(Compile(shaderStage, source, entryPoint, sourceName, DxcShaderModel.Model6_1));
 }
Пример #6
0
 public static byte[] Compile(DxcShaderStage shaderStage, string source, string entryPoint, string sourceName, DxcShaderModel shaderModel)
 {
     return(Compile(shaderStage, source, entryPoint, sourceName, new DxcCompilerOptions {
         ShaderModel = shaderModel, PackMatrixInRowMajor = true
     }));
 }
Пример #7
0
        public static IDxcOperationResult Compile(
            DxcShaderStage shaderStage,
            string source,
            string entryPoint,
            string sourceName,
            DxcCompilerOptions options)
        {
            var shaderProfile = GetShaderProfile(shaderStage, options.ShaderModel);

            if (options == null)
            {
                options = new DxcCompilerOptions();
            }

            var arguments = new List <string>();

            if (options.IEEEStrictness)
            {
                arguments.Add("-Gis");
            }

            // HLSL matrices are translated into SPIR-V OpTypeMatrixs in a transposed manner,
            // See also https://antiagainst.github.io/post/hlsl-for-vulkan-matrices/
            if (options.PackMatrixInColumnMajor)
            {
                arguments.Add("-Zpc");
            }
            else if (options.PackMatrixInRowMajor)
            {
                arguments.Add("-Zpr");
            }

            if (options.Enable16bitTypes)
            {
                if (options.ShaderModel.Major >= 6 &&
                    options.ShaderModel.Minor >= 2)
                {
                    arguments.Add("-enable-16bit-types");
                }
                else
                {
                    throw new InvalidOperationException("16-bit types requires shader model 6.2 or up.");
                }
            }

            if (options.EnableDebugInfo)
            {
                arguments.Add("-Zi");
            }

            if (options.DisableOptimizations)
            {
                arguments.Add("-Od");
            }
            else
            {
                if (options.OptimizationLevel < 4)
                {
                    arguments.Add($"-O{options.OptimizationLevel}");
                }
                else
                {
                    throw new InvalidOperationException("Invalid optimization level.");
                }
            }

            if (options.AllResourcesBound)
            {
                arguments.Add("-all_resources_bound");
            }

            if (options.GenerateSPIRV)
            {
                arguments.Add("-spirv");
            }

            if (options.ShiftAllConstantBuffersBindings > 0)
            {
                arguments.Add("-fvk-b-shift");
                arguments.Add($"{options.ShiftAllConstantBuffersBindings}");
                arguments.Add($"all");
            }

            if (options.ShiftAllTexturesBindings > 0)
            {
                arguments.Add("-fvk-t-shift");
                arguments.Add($"{options.ShiftAllTexturesBindings}");
                arguments.Add($"all");
            }

            if (options.ShiftAllSamplersBindings > 0)
            {
                arguments.Add("-fvk-s-shift");
                arguments.Add($"{options.ShiftAllSamplersBindings}");
                arguments.Add($"all");
            }

            if (options.ShiftAllUAVBuffersBindings > 0)
            {
                arguments.Add("-fvk-u-shift");
                arguments.Add($"{options.ShiftAllUAVBuffersBindings}");
                arguments.Add($"all");
            }

            var compiler = Dxc.CreateDxcCompiler();

            return(compiler.Compile(
                       Dxc.CreateBlobForText(Library, source),
                       sourceName,
                       entryPoint,
                       shaderProfile,
                       arguments.ToArray(),
                       arguments.Count,
                       null,
                       0,
                       Library.CreateIncludeHandler()
                       ));
        }
Пример #8
0
        public static IDxcResult Compile(DxcShaderStage shaderStage, string source, string entryPoint,
                                         DxcCompilerOptions options        = null,
                                         string fileName                   = null,
                                         DxcDefine[] defines               = null,
                                         IDxcIncludeHandler includeHandler = null)
        {
            if (options == null)
            {
                options = new DxcCompilerOptions();
            }

            string profile = GetShaderProfile(shaderStage, options.ShaderModel);

            var arguments = new List <string>();

            if (!string.IsNullOrEmpty(fileName))
            {
                arguments.Add(fileName);
            }

            arguments.Add("-E");
            arguments.Add(entryPoint);

            arguments.Add("-T");
            arguments.Add(profile);

            if (options.IEEEStrictness)
            {
                arguments.Add("-Gis");
            }

            // HLSL matrices are translated into SPIR-V OpTypeMatrixs in a transposed manner,
            // See also https://antiagainst.github.io/post/hlsl-for-vulkan-matrices/
            if (options.PackMatrixInColumnMajor)
            {
                arguments.Add("-Zpc");
            }
            else if (options.PackMatrixInRowMajor)
            {
                arguments.Add("-Zpr");
            }

            if (options.Enable16bitTypes)
            {
                if (options.ShaderModel.Major >= 6 &&
                    options.ShaderModel.Minor >= 2)
                {
                    arguments.Add("-enable-16bit-types");
                }
                else
                {
                    throw new InvalidOperationException("16-bit types requires shader model 6.2 or up.");
                }
            }

            if (options.EnableDebugInfo)
            {
                arguments.Add("-Zi");
            }

            if (options.DisableOptimizations)
            {
                arguments.Add("-Od");
            }
            else
            {
                if (options.OptimizationLevel < 4)
                {
                    arguments.Add($"-O{options.OptimizationLevel}");
                }
                else
                {
                    throw new InvalidOperationException("Invalid optimization level.");
                }
            }

            if (options.WarningsAreErrors)
            {
                arguments.Add("-WX");
            }

            if (options.AllResourcesBound)
            {
                arguments.Add("-all_resources_bound");
            }

            if (options.StripReflectionIntoSeparateBlob)
            {
                arguments.Add("-Qstrip_reflect");
            }

            if (options.GenerateSPIRV)
            {
                arguments.Add("-spirv");
            }

            if (options.ShiftAllConstantBuffersBindings > 0)
            {
                arguments.Add("-fvk-b-shift");
                arguments.Add($"{options.ShiftAllConstantBuffersBindings}");
                arguments.Add($"all");
            }

            if (options.ShiftAllTexturesBindings > 0)
            {
                arguments.Add("-fvk-t-shift");
                arguments.Add($"{options.ShiftAllTexturesBindings}");
                arguments.Add($"all");
            }

            if (options.ShiftAllSamplersBindings > 0)
            {
                arguments.Add("-fvk-s-shift");
                arguments.Add($"{options.ShiftAllSamplersBindings}");
                arguments.Add($"all");
            }

            if (options.ShiftAllUAVBuffersBindings > 0)
            {
                arguments.Add("-fvk-u-shift");
                arguments.Add($"{options.ShiftAllUAVBuffersBindings}");
                arguments.Add($"all");
            }

            if (defines != null && defines.Length > 0)
            {
                foreach (DxcDefine define in defines)
                {
                    string defineValue = define.Value;
                    if (string.IsNullOrEmpty(defineValue))
                    {
                        defineValue = "1";
                    }

                    arguments.Add("-D");
                    arguments.Add($"{define.Name}={defineValue}");
                }
            }

            if (includeHandler == null)
            {
                using (includeHandler = Utils.CreateDefaultIncludeHandler())
                {
                    return(Compiler.Compile(source, arguments.ToArray(), includeHandler));
                }
            }
            else
            {
                return(Compiler.Compile(source, arguments.ToArray(), includeHandler));
            }
        }
Пример #9
0
    public static IDxcResult Compile(DxcShaderStage shaderStage, string source, string entryPoint,
                                     DxcCompilerOptions?options        = null,
                                     string?fileName                   = null,
                                     DxcDefine[]?defines               = null,
                                     IDxcIncludeHandler?includeHandler = null,
                                     string[]?additionalArguments      = null)
    {
        if (options == null)
        {
            options = new DxcCompilerOptions();
        }

        string profile = GetShaderProfile(shaderStage, options.ShaderModel);

        var arguments = new List <string>();

        if (!string.IsNullOrEmpty(fileName))
        {
            arguments.Add(fileName !);
        }

        arguments.Add("-E");
        arguments.Add(entryPoint);

        arguments.Add("-T");
        arguments.Add(profile);

        // Defines
        if (defines != null && defines.Length > 0)
        {
            foreach (DxcDefine define in defines)
            {
                string defineValue = define.Value;
                if (string.IsNullOrEmpty(defineValue))
                {
                    defineValue = "1";
                }

                arguments.Add("-D");
                arguments.Add($"{define.Name}={defineValue}");
            }
        }

        if (options.EnableDebugInfo)
        {
            arguments.Add("-Zi");
        }

        if (options.SkipValidation)
        {
            arguments.Add("-Vd");
        }

        if (options.SkipOptimizations)
        {
            arguments.Add("-Od");
        }
        else
        {
            if (options.OptimizationLevel < 4)
            {
                arguments.Add($"-O{options.OptimizationLevel}");
            }
            else
            {
                throw new InvalidOperationException("Invalid optimization level.");
            }
        }

        // HLSL matrices are translated into SPIR-V OpTypeMatrixs in a transposed manner,
        // See also https://antiagainst.github.io/post/hlsl-for-vulkan-matrices/
        if (options.PackMatrixRowMajor)
        {
            arguments.Add("-Zpr");
        }
        if (options.PackMatrixColumnMajor)
        {
            arguments.Add("-Zpc");
        }
        if (options.AvoidFlowControl)
        {
            arguments.Add("-Gfa");
        }
        if (options.PreferFlowControl)
        {
            arguments.Add("-Gfp");
        }

        if (options.EnableStrictness)
        {
            arguments.Add("-Ges");
        }

        if (options.EnableBackwardCompatibility)
        {
            arguments.Add("-Gec");
        }

        if (options.IEEEStrictness)
        {
            arguments.Add("-Gis");
        }

        if (options.WarningsAreErrors)
        {
            arguments.Add("-WX");
        }

        if (options.ResourcesMayAlias)
        {
            arguments.Add("-res_may_alias");
        }

        if (options.AllResourcesBound)
        {
            arguments.Add("-all_resources_bound");
        }

        if (options.Enable16bitTypes)
        {
            if (options.ShaderModel.Major >= 6 &&
                options.ShaderModel.Minor >= 2)
            {
                arguments.Add("-enable-16bit-types");
            }
            else
            {
                throw new InvalidOperationException("16-bit types requires shader model 6.2 or up.");
            }
        }

        if (options.StripReflectionIntoSeparateBlob)
        {
            arguments.Add("-Qstrip_reflect");
        }

        if (options.GenerateSPIRV)
        {
            arguments.Add("-spirv");
        }

        // HLSL version, default 2018.
        arguments.Add("-HV");
        arguments.Add($"{options.HLSLVersion}");

        if (options.ShiftAllConstantBuffersBindings > 0)
        {
            arguments.Add("-fvk-b-shift");
            arguments.Add($"{options.ShiftAllConstantBuffersBindings}");
            arguments.Add($"all");
        }

        if (options.ShiftAllTexturesBindings > 0)
        {
            arguments.Add("-fvk-t-shift");
            arguments.Add($"{options.ShiftAllTexturesBindings}");
            arguments.Add($"all");
        }

        if (options.ShiftAllSamplersBindings > 0)
        {
            arguments.Add("-fvk-s-shift");
            arguments.Add($"{options.ShiftAllSamplersBindings}");
            arguments.Add($"all");
        }

        if (options.ShiftAllUAVBuffersBindings > 0)
        {
            arguments.Add("-fvk-u-shift");
            arguments.Add($"{options.ShiftAllUAVBuffersBindings}");
            arguments.Add($"all");
        }

        if (options.UseOpenGLLayout)
        {
            arguments.Add("-fvk-use-gl-layout");
        }
        if (options.UseDirectXLayout)
        {
            arguments.Add("-fvk-use-dx-layout");
        }
        if (options.UseScalarLayout)
        {
            arguments.Add("-fvk-use-scalar-layout");
        }

        if (options.SPIRVFlattenResourceArrays)
        {
            arguments.Add("-fspv-flatten-resource-arrays");
        }
        if (options.SPIRVReflect)
        {
            arguments.Add("-fspv-reflect");
        }

        arguments.Add("-fspv-target-env=vulkan1.1");

        if (additionalArguments != null && additionalArguments.Length > 0)
        {
            arguments.AddRange(additionalArguments);
        }

        return(Compile(source, arguments.ToArray(), includeHandler));
    }