Ejemplo n.º 1
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));
        }
Ejemplo n.º 2
0
        public static IDxcOperationResult Compile(
            DxcShaderStage shaderStage,
            string source,
            string entryPoint,
            string sourceName,
            DxcCompilerOptions options,
            DXCDefine[] defines        = null,
            IDxcIncludeHandler include = null)
        {
            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,
                       defines,
                       defines != null ? defines.Length : 0,
                       include ?? DefaultIncludeHandler
                       ));
        }
Ejemplo n.º 3
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));
            }
        }