Example #1
0
        public IDxcBlob CompileLibrary(string filename, DxcShaderModel targetString)
        {
            // Open and read the file
            string pTextBlob = File.ReadAllText(filename);

            // Compile
            var pResult = DxcCompiler.Compile(DxcShaderStage.Library, pTextBlob, string.Empty, string.Empty, new DxcCompilerOptions()
            {
                ShaderModel = targetString
            });

            // Verify the result
            int resultCode;

            resultCode = pResult.GetStatus();
            if (resultCode != 0)
            {
                Debug.WriteLine(pResult.GetStatus());
            }

            IDxcBlob pBlob;

            pBlob = pResult.GetResult();
            return(pBlob);
        }
Example #2
0
        public unsafe IDxcBlob CompileLibrary(string filename, DxcShaderModel targetString)
        {
            // Open and read the file
            string pTextBlob = File.ReadAllText(filename);

            // Compile
            var pResult = DxcCompiler.Compile(DxcShaderStage.Library, pTextBlob, string.Empty, string.Empty, new DxcCompilerOptions()
            {
                ShaderModel = targetString
            });

            // Verify the result
            int resultCode;

            resultCode = pResult.GetStatus();
            if (resultCode != 0)
            {
                var    error   = pResult.GetErrors();
                string message = Marshal.PtrToStringAnsi((IntPtr)error.GetBufferPointer(), (int)error.GetBufferSize());
                Debug.WriteLine(message);
            }

            IDxcBlob pBlob;

            pBlob = pResult.GetResult();
            return(pBlob);
        }
Example #3
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());
        }
        public void ErrorTest()
        {
            string shaderSource = File.ReadAllText(Path.Combine(AssetsPath, "TriangleError.hlsl"));

            using IDxcResult results = DxcCompiler.Compile(DxcShaderStage.Vertex, shaderSource, "VSMain");

            Assert.True(results.GetStatus().Failure);

            var error = results.GetErrors();

            Assert.Contains("error: no member named 'ThisIsAnError' in 'PSInput'", error);
        }
        private IDxcBlob CompileLibrary(string filename)
        {
            string content = File.ReadAllText(filename);

            var res = DxcCompiler.Compile(DxcShaderStage.Library, content, "", "", new DxcCompilerOptions()
            {
                ShaderModel = new DxcShaderModel(6, 3)
            });

            Console.WriteLine(res.GetStatus());

            return(res.GetResult());
        }
        public void SingleFileTest()
        {
            string shaderSource = File.ReadAllText(Path.Combine(AssetsPath, "TriangleSingleFile.hlsl"));

            using IDxcResult results = DxcCompiler.Compile(DxcShaderStage.Vertex, shaderSource, "VSMain");

            Assert.True(results.GetStatus().Success);

            var shaderCode = results.GetObjectBytecodeArray();

            Assert.True(shaderCode.Length > 0);

            Assert.True(ShaderCodeHelper.IsCodeSigned(shaderCode), ShaderCodeNotSignedMessage);
        }
Example #7
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);
            }
        }
Example #8
0
        public void DxcDefineTest()
        {
            string shaderSource = File.ReadAllText(Path.Combine(AssetsPath, "TriangleSingleFile.hlsl"));

            var defines = new DxcDefine[] { new DxcDefine {
                                                Name = "TEST", Value = "1"
                                            } };

            using IDxcResult? results = DxcCompiler.Compile(DxcShaderStage.Vertex, shaderSource, "VSMain", defines: defines);

            Assert.True(results !.GetStatus().Success);

            var shaderCode = results.GetObjectBytecodeArray();

            Assert.True(shaderCode.Length > 0);

            Assert.True(ShaderCodeHelper.IsCodeSigned(shaderCode), ShaderCodeNotSignedMessage);
        }
Example #9
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);
        }
Example #10
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);
        }
Example #11
0
        public static Alimer.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)
            {
                var dxcShaderStage = GetDxcShaderStage(stage);
                var options        = new DxcCompilerOptions
                {
                };

                var result = DxcCompiler.Compile(dxcShaderStage, source, entryPoint, fileName, options);

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

                    var containReflection = CreateDxcContainerReflection();
                    containReflection.Load(blob);
                    int hr = containReflection.FindFirstPartKind(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 Alimer.Graphics.ShaderBytecode(stage, bytecode));
                }
                else
                {
                    //var resultText = GetStringFromBlob(DxcCompiler, result.GetErrors());
                }
            }
            else
            {
                var shaderProfile = $"{GetShaderProfile(stage)}_5_0";
                var result        = Compiler.Compile(source,
                                                     entryPoint,
                                                     fileName,
                                                     shaderProfile,
                                                     out var blob,
                                                     out var errorMsgs);

                if (result.Failure)
                {
                    if (errorMsgs != null)
                    {
                        var errorText = errorMsgs.ConvertToString();
                    }
                }
                else
                {
                    var bytecode = blob.GetBytes();
                    return(new Alimer.Graphics.ShaderBytecode(stage, bytecode));
                }
            }

            return(default);