コード例 #1
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);
            }
        }
        internal DxilLibrary(IDxcBlob blob, string[] entryPoints)
        {
            var shaderBytes = Dxc.GetBytesFromBlob(blob);

            ExportDescription[] exportDesc = new ExportDescription[entryPoints.Length];
            for (int i = 0; i < exportDesc.Length; i++)
            {
                exportDesc[i]                = new ExportDescription();
                exportDesc[i].Name           = entryPoints[i];
                exportDesc[i].Flags          = ExportFlags.None;
                exportDesc[i].ExportToRename = null;
            }

            libraryDesc = new DxilLibraryDescription(new ShaderBytecode(shaderBytes), exportDesc);

            stateSubObject = new StateSubObject(libraryDesc);
        }
コード例 #3
0
        public DxilLibrary(IDxcBlob pBlob, string[] entryPoint)
        {
            var pShaderBytecode = Dxc.GetBytesFromBlob(pBlob);

            ExportDescription[] exportDesc = new ExportDescription[entryPoint.Length];
            
            for (int i = 0; i < exportDesc.Length; i++)
            {
                exportDesc[i] = new ExportDescription();
                exportDesc[i].Name = entryPoint[i];
                exportDesc[i].Flags = ExportFlags.None;
                exportDesc[i].ExportToRename = null;
            }

            DxilLibraryDescription dxilLibDesc = new DxilLibraryDescription(new ShaderBytecode(pShaderBytecode), exportDesc);
            this.stateSubObject = new StateSubObject(dxilLibDesc);
        }
コード例 #4
0
        private static byte[] Compile(ShaderStage shaderStage, string source, string entryPoint, string sourceName, DxcCompilerOptions options)
        {
            //IDxcOperationResult result = DxcCompiler.Compile((DxcShaderStage)shaderStage, source, entryPoint, sourceName, options);

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

            List <string> arguments = new List <string>();

            if (options.PackMatrixInColumnMajor)
            {
                arguments.Add("-Zpc");
            }
            else if (options.PackMatrixInRowMajor)
            {
                arguments.Add("-Zpr");
            }

            IDxcLibrary        library        = Dxc.CreateDxcLibrary();
            IDxcBlobEncoding   sourceBlob     = Dxc.CreateBlobForText(library, source);
            IDxcIncludeHandler includeHandler = library.CreateIncludeHandler();

            IDxcCompiler        compiler = Dxc.CreateDxcCompiler();
            IDxcOperationResult result   = compiler.Compile(
                sourceBlob,
                sourceName,
                entryPoint,
                shaderProfile,
                arguments.ToArray(),
                arguments.Count,
                null,
                0,
                includeHandler);

            if (result.GetStatus() == 0)
            {
                IDxcBlob blob = result.GetResult();
                return(Dxc.GetBytesFromBlob(blob));
            }
            else
            {
                string resultText = Dxc.GetStringFromBlob(library, result.GetErrors());
                throw new Exception(resultText);
            }
        }
コード例 #5
0
        public ShaderBytecode CompileShader(string source)
        {
            var result = DxcCompiler.Compile(DxcShaderStage.ComputeShader, source, "CSMain", string.Empty, new DxcCompilerOptions {
                ShaderModel = DxcShaderModel.Model6_1
            });

            // Get the compiled bytecode in case of success
            if (result.GetStatus() == 0)
            {
                IDxcBlob blob     = result.GetResult();
                byte[]   bytecode = Dxc.GetBytesFromBlob(blob);

                return(bytecode);
            }

            // Compile error
            string resultText = Dxc.GetStringFromBlob(Library, result.GetErrors());

            throw new Exception(resultText);
        }
コード例 #6
0
ファイル: ShaderCompiler.cs プロジェクト: coslor/ComputeSharp
 // Loads the dxil.dll library, needed to create a pipeline state for a shader to dispatch
 static ShaderCompiler()
 {
     Dxil.LoadLibrary();
     Library = Dxc.CreateDxcLibrary(); // This needs to be loaded after dxil.dll
 }
コード例 #7
0
ファイル: ShaderCompiler.cs プロジェクト: cagr301/engine
        public static Vortice.Graphics.ShaderBytecode Compile(
            GraphicsBackend backend,
            string source,
            ShaderStages stage,
            string entryPoint = "",
            string fileName   = "")
        {
            if (string.IsNullOrEmpty(entryPoint))
            {
                entryPoint = GetDefaultEntryPoint(stage);
            }

            // We use legacy compiler for D3D11.
            bool isDxil = backend != GraphicsBackend.Direct3D11;

            if (isDxil)
            {
                RuntimeHelpers.RunClassConstructor(typeof(Dxc).TypeHandle);
                var shaderProfile = $"{GetShaderProfile(stage)}_6_0";

                var arguments = new string[]
                {
                    "-T", shaderProfile,
                    "-E", entryPoint,
                };

                var compiler = Dxc.CreateDxcCompiler();
                var result   = compiler.Compile(
                    Dxc.CreateBlobForText(source),
                    fileName,
                    entryPoint,
                    shaderProfile,
                    arguments,
                    arguments.Length,
                    null,
                    0,
                    Dxc.Library.CreateIncludeHandler()
                    );

                if (result.GetStatus() == 0)
                {
                    var blob     = result.GetResult();
                    var bytecode = Dxc.GetBytesFromBlob(blob);

                    var containReflection = Dxc.CreateDxcContainerReflection();
                    containReflection.Load(blob);
                    int hr = containReflection.FindFirstPartKind(Dxc.DFCC_DXIL, out uint dxilPartIndex);
                    if (hr < 0)
                    {
                        //MessageBox.Show("Debug information not found in container.");
                        //return;
                    }

                    var f = containReflection.GetPartReflection(dxilPartIndex, typeof(ID3D12ShaderReflection).GUID, out var nativePtr);
                    using (var shaderReflection = new ID3D12ShaderReflection(nativePtr))
                    {
                        var shaderReflectionDesc = shaderReflection.Description;

                        foreach (var parameterDescription in shaderReflection.InputParameters)
                        {
                        }

                        foreach (var resource in shaderReflection.Resources)
                        {
                        }
                    }

                    unsafe
                    {
                        var   part = containReflection.GetPartContent(dxilPartIndex);
                        uint *p    = (uint *)part.GetBufferPointer();
                        var   v    = DescribeProgramVersion(*p);
                    }

                    // Disassemble
                    var    disassembleBlob = compiler.Disassemble(blob);
                    string disassemblyText = Dxc.GetStringFromBlob(disassembleBlob);

                    return(new Vortice.Graphics.ShaderBytecode(stage, bytecode));
                }
                else
                {
                    var resultText = Dxc.GetStringFromBlob(result.GetErrors());
                }
            }
            else
            {
                const uint flags         = 0;
                var        shaderProfile = $"{GetShaderProfile(stage)}_5_0";
                int        hr            = D3DCompiler.D3DCompiler.D3DCompile(
                    source,
                    source.Length,
                    fileName,
                    null,
                    0,
                    entryPoint,
                    shaderProfile,
                    flags,
                    0,
                    out var blob,
                    out var errorMsgs);

                if (hr != 0)
                {
                    if (errorMsgs != null)
                    {
                        var errorText = Dxc.GetStringFromBlob(errorMsgs);
                    }
                }
                else
                {
                    var bytecode = Dxc.GetBytesFromBlob(blob);
                    return(new Vortice.Graphics.ShaderBytecode(stage, bytecode));
                }
            }

            return(default);
コード例 #8
0
ファイル: Program.cs プロジェクト: Aminator/Vortice.Windows
 private static void TestDxc()
 {
     var library = Dxc.CreateDxcLibrary();
 }