Ejemplo n.º 1
0
        public unsafe IDxcOperationResult Compile(IDxcBlob source,
                                                  string sourceName,
                                                  string entryPoint,
                                                  string targetProfile,
                                                  string[] arguments,
                                                  int argumentsCount,
                                                  DxcDefine[] defines,
                                                  IDxcIncludeHandler includeHandler)
        {
            IntPtr *argumentsPtr = (IntPtr *)0;

            try
            {
                if (arguments != null && argumentsCount > 0)
                {
                    argumentsPtr = Interop.AllocToPointers(arguments, argumentsCount);
                }

                Compile(source, sourceName,
                        entryPoint, targetProfile,
                        (IntPtr)argumentsPtr, argumentsCount,
                        defines,
                        includeHandler,
                        out IDxcOperationResult? result).CheckError();
                return(result !);
            }
            finally
            {
                if (argumentsPtr != null)
                {
                    Interop.Free(argumentsPtr);
                }
            }
        }
Ejemplo n.º 2
0
        public unsafe IDxcOperationResult?Preprocess(IDxcBlob source,
                                                     string sourceName,
                                                     string[] arguments,
                                                     int argumentsCount,
                                                     DxcDefine[] defines,
                                                     IDxcIncludeHandler includeHandler)
        {
            IntPtr *argumentsPtr = (IntPtr *)0;

            try
            {
                if (arguments != null && argumentsCount > 0)
                {
                    argumentsPtr = Interop.AllocToPointers(arguments, argumentsCount);
                }

                Result hr = Preprocess(source, sourceName,
                                       (IntPtr)argumentsPtr, argumentsCount,
                                       defines,
                                       includeHandler,
                                       out IDxcOperationResult? result);

                if (hr.Failure)
                {
                    result = default;
                    return(default);
Ejemplo n.º 3
0
        public unsafe IDxcOperationResult Compile(IDxcBlob source,
                                                  string sourceName,
                                                  string entryPoint,
                                                  string targetProfile,
                                                  string[] arguments,
                                                  int argumentsCount,
                                                  DxcDefine[] defines,
                                                  int defineCount,
                                                  IDxcIncludeHandler includeHandler)
        {
            IntPtr *argumentsPtr = (IntPtr *)0;

            try
            {
                if (arguments != null && argumentsCount > 0)
                {
                    argumentsPtr = Interop.AllocToPointers(arguments, argumentsCount);
                }

                Result hr = Compile(source, sourceName,
                                    entryPoint, targetProfile,
                                    (IntPtr)argumentsPtr, argumentsCount,
                                    defines, defineCount,
                                    includeHandler,
                                    out IDxcOperationResult result);

                if (hr.Failure)
                {
                    result = default;
                    return(default);
Ejemplo n.º 4
0
        public unsafe Result CompileWithDebug(IDxcBlob source,
                                              string sourceName,
                                              string entryPoint,
                                              string targetProfile,
                                              string[] arguments,
                                              int argumentsCount,
                                              DxcDefine[] defines,
                                              IDxcIncludeHandler includeHandler,
                                              out IDxcOperationResult?result,
                                              out string?debugBlobName, out IDxcBlob?debugBlob)
        {
            IntPtr *argumentsPtr = (IntPtr *)0;

            try
            {
                IntPtr debugBlobNameOut = default;

                if (arguments != null && argumentsCount > 0)
                {
                    argumentsPtr = Interop.AllocToPointers(arguments, argumentsCount);
                }

                Result hr = CompileWithDebug(source, sourceName,
                                             entryPoint, targetProfile,
                                             (IntPtr)argumentsPtr, argumentsCount,
                                             defines,
                                             includeHandler,
                                             out result,
                                             new IntPtr(&debugBlobNameOut),
                                             out debugBlob);

                if (debugBlobNameOut != IntPtr.Zero)
                {
                    debugBlobName = Marshal.PtrToStringUni(debugBlobNameOut);
                }
                else
                {
                    debugBlobName = string.Empty;
                }

                if (hr.Failure)
                {
                    result = default;
                    return(hr);
                }

                return(hr);
            }
            finally
            {
                if (argumentsPtr != null)
                {
                    Interop.Free(argumentsPtr);
                }
            }
        }
Ejemplo n.º 5
0
            private static unsafe int LoadSource(IntPtr thisObject, void *param0, void *param1)
            {
                try
                {
                    IntPtr             filenameRef = (IntPtr)param0;
                    IDxcIncludeHandler @this       = (IDxcIncludeHandler)ToShadow <Vortice.Dxc.IDxcIncludeHandlerShadow>(thisObject).Callback;
                    string             fileName    = Marshal.PtrToStringUni(filenameRef);
                    Result             result      = @this.LoadSource(fileName, out IDxcBlob includeSource);

                    ref IntPtr includeSourceOut = ref Unsafe.AsRef <IntPtr>(param1);
                    includeSourceOut = CppObject.ToCallbackPtr <IDxcBlob>(includeSource);
                    return(result.Code);
                }
Ejemplo n.º 6
0
 public IDxcOperationResult?Preprocess(IDxcBlob source,
                                       string sourceName,
                                       string[] arguments,
                                       DxcDefine[] defines,
                                       IDxcIncludeHandler includeHandler)
 {
     return(Preprocess(
                source,
                sourceName,
                arguments,
                arguments?.Length ?? 0,
                defines,
                includeHandler));
 }
Ejemplo n.º 7
0
 public static IDxcResult Compile(string shaderSource, string[] arguments, IDxcIncludeHandler includeHandler = null)
 {
     if (includeHandler == null)
     {
         using (includeHandler = Utils.CreateDefaultIncludeHandler())
         {
             return(Compiler.Compile(shaderSource, arguments, includeHandler));
         }
     }
     else
     {
         return(Compiler.Compile(shaderSource, arguments, includeHandler));
     }
 }
Ejemplo n.º 8
0
 public IDxcOperationResult?Compile(IDxcBlob source,
                                    string sourceName,
                                    string entryPoint,
                                    string targetProfile,
                                    string[] arguments,
                                    DxcDefine[] defines,
                                    IDxcIncludeHandler includeHandler)
 {
     return(Compile(
                source,
                sourceName,
                entryPoint,
                targetProfile,
                arguments,
                arguments?.Length ?? 0,
                defines,
                includeHandler));
 }
Ejemplo n.º 9
0
        public unsafe Result Compile <T>(string source, string[] arguments, IDxcIncludeHandler includeHandler, out T?result) where T : ComObject
        {
            IntPtr  shaderSourcePtr = Marshal.StringToHGlobalAnsi(source);
            IntPtr *argumentsPtr    = (IntPtr *)0;

            DxcBuffer buffer = new()
            {
                Ptr      = shaderSourcePtr,
                Size     = source.Length,
                Encoding = Dxc.DXC_CP_ACP
            };

            try
            {
                int argumentsCount = 0;
                if (arguments != null && arguments.Length > 0)
                {
                    argumentsCount = arguments.Length;
                    argumentsPtr   = Interop.AllocToPointers(arguments);
                }

                Result hr = Compile(ref buffer, (IntPtr)argumentsPtr, argumentsCount, includeHandler, typeof(T).GUID, out IntPtr nativePtr);
                if (hr.Failure)
                {
                    result = default;
                    return(hr);
                }

                result = MarshallingHelpers.FromPointer <T>(nativePtr);
                return(hr);
            }
            finally
            {
                if (shaderSourcePtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(shaderSourcePtr);
                }

                if (argumentsPtr != null)
                {
                    Interop.Free(argumentsPtr);
                }
            }
        }
Ejemplo n.º 10
0
 public IDxcOperationResult?CompileWithDebug(IDxcBlob source,
                                             string sourceName,
                                             string entryPoint,
                                             string targetProfile,
                                             string[] arguments,
                                             DxcDefine[] defines,
                                             IDxcIncludeHandler includeHandler,
                                             out string?debugBlobName, out IDxcBlob?debugBlob)
 {
     return(CompileWithDebug(
                source,
                sourceName,
                entryPoint,
                targetProfile,
                arguments,
                arguments?.Length ?? 0,
                defines,
                includeHandler,
                out debugBlobName, out debugBlob));
 }
Ejemplo n.º 11
0
 public IDxcOperationResult CompileWithDebug(IDxcBlob source,
                                             string sourceName,
                                             string entryPoint,
                                             string targetProfile,
                                             string[] arguments,
                                             int argumentsCount,
                                             DxcDefine[] defines,
                                             IDxcIncludeHandler includeHandler,
                                             out string debugBlobName, out IDxcBlob debugBlob)
 {
     return(CompileWithDebug(
                source,
                sourceName,
                entryPoint,
                targetProfile,
                arguments,
                argumentsCount,
                defines,
                defines != null ? defines.Length : 0,
                includeHandler,
                out debugBlobName, out debugBlob));
 }
Ejemplo n.º 12
0
 public unsafe IDxcResult Compile(string source, string[] arguments, IDxcIncludeHandler includeHandler)
 {
     Compile(source, arguments, includeHandler, out IDxcResult? result).CheckError();
     return(result !);
 }
Ejemplo n.º 13
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));
            }
        }
Ejemplo n.º 14
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
                       ));
        }