Example #1
0
        private void ApplyChangesButton_Click(object sender, EventArgs e)
        {
            // Turn the text into the expected encoding.
            IDxcBlobEncoding sourceBlob = EditorForm.CreateBlobForText(this.Library, this.CodeBox.Text);

            sourceBlob = this.Library.GetBlobAstUf8(sourceBlob);
            IDxcBlob bitcodeBlob = sourceBlob;

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

            passes.Add("hlsl-passes-resume");
            for (int i = PassesListBox.SelectedIndex; i < PassesListBox.Items.Count; ++i)
            {
                passes.Add(((TextSection)PassesListBox.Items[i]).Title);
            }
            string[] options = EditorForm.CreatePassOptions(passes, false, true);
            EditorForm.OptimizeResult opt = EditorForm.RunOptimize(this.Library, options, bitcodeBlob);
            if (!opt.Succeeded)
            {
                MessageBox.Show("Failed to optimize: " + opt.ResultText);
                return;
            }

            OptEditorForm form = new OptEditorForm();

            form.CodeFont        = this.CodeBox.Font;
            form.Library         = this.Library;
            form.HighLevelSource = this.HighLevelSource;
            form.Sections        = TextSection.EnumerateSections(new string[] { "MODULE-PRINT", "Phase:" }, opt.ResultText).ToArray();
            form.StartPosition   = FormStartPosition.CenterParent;
            form.Show(this);
        }
        public Result LoadSource(string fileName, out IDxcBlob includeSource)
        {
            if (fileName.StartsWith("./"))
            {
                fileName = fileName.Substring(2);
            }

            var includeFile = GetFilePath(fileName);

            if (string.IsNullOrEmpty(includeFile))
            {
                includeSource = default;

                return(Result.Fail);
            }

            if (!_sourceFiles.TryGetValue(includeFile, out SourceCodeBlob sourceCodeBlob))
            {
                byte[] data = NewMethod(includeFile);

                sourceCodeBlob = new SourceCodeBlob(data);
                _sourceFiles.Add(includeFile, sourceCodeBlob);
            }

            includeSource = sourceCodeBlob.Blob;

            return(Result.Ok);
        }
        public override void DoExport(string filePath, uTinyRipper.Version version, ref ShaderSubProgram subProgram)
        {
            byte[] exportData = subProgram.ProgramData;
            int    dataOffset = 0;

            if (DXDataHeader.HasHeader(m_graphicApi))
            {
                dataOffset = DXDataHeader.GetDataOffset(version, m_graphicApi);
                uint fourCC = BitConverter.ToUInt32(exportData, dataOffset);
                if (fourCC != DXBCFourCC)
                {
                    throw new Exception("Magic number doesn't match");
                }
            }

            int    dataLength       = exportData.Length - dataOffset;
            IntPtr unmanagedPointer = Marshal.AllocHGlobal(dataLength);

            Marshal.Copy(exportData, dataOffset, unmanagedPointer, dataLength);

            var parameters = new object[] { unmanagedPointer, (uint)dataLength, (uint)0, null, null };

            dissassembleMethod.Invoke(null, parameters);
            IDxcBlob disassembly     = (IDxcBlob)parameters[4];
            string   disassemblyText = GetStringFromBlob(disassembly);

            File.WriteAllText(filePath, disassemblyText);
            Marshal.FreeHGlobal(unmanagedPointer);
        }
    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);
            }
        }
    }
Example #5
0
        public DxilLibrary CreateDxilLibrary()
        {
            // Compile the shader
            IDxcBlob pDxilLib = this.CompileLibrary("Data/14-Shaders.hlsl", new DxcShaderModel(6, 3));

            string[] entryPoints = new string[] { kRayGenShader, kMissShader, kPlaneChs, kTriangleChs, kShadowMiss, kShadowChs };
            return(new DxilLibrary(pDxilLib, entryPoints));
        }
 internal static string GetStringFromBlob(IDxcLibrary library, IDxcBlob blob)
 {
     unsafe
     {
         blob = library.GetBlobAstUf16(blob);
         return(new string(blob.GetBufferPointer(), 0, (int)(blob.GetBufferSize() / 2) - 1));
     }
 }
Example #7
0
 private static string GetStringFromBlob(IDxcBlob blob)
 {
     unsafe
     {
         blob = Library.GetBlobAstUf16(blob);
         return(new string(blob.GetBufferPointer(), 0, (int)(blob.GetBufferSize() / 2)));
     }
 }
Example #8
0
        public DxilLibrary CreateDxilLibrary()
        {
            // Compile the shader
            IDxcBlob pDxilLib = this.CompileLibrary("Data/Shaders.hlsl", new DxcShaderModel(6, 3));

            string[] entryPoints = new string[] { kRayGenShader, kMissShader, kClosestHitShader, kShadowMiss };
            return(new DxilLibrary(pDxilLib, entryPoints));
        }
    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);
            }
        }
    }
Example #10
0
 public extern static Int32 D3DCompile(
     [MarshalAs(UnmanagedType.LPStr)] string srcData, int srcDataSize,
     [MarshalAs(UnmanagedType.LPStr)] string sourceName,
     [MarshalAs(UnmanagedType.LPArray)] D3D_SHADER_MACRO[] defines,
     int pInclude,
     [MarshalAs(UnmanagedType.LPStr)] string entryPoint,
     [MarshalAs(UnmanagedType.LPStr)] string target,
     UInt32 Flags1,
     UInt32 Flags2,
     out IDxcBlob code, out IDxcBlob errorMsgs);
Example #11
0
 public static byte[] BlobToBytes(IDxcBlob blob)
 {
     byte[] bytes;
     unsafe
     {
         char *pBuffer = blob.GetBufferPointer();
         uint  size    = blob.GetBufferSize();
         bytes = new byte[size];
         IntPtr ptr = new IntPtr(pBuffer);
         System.Runtime.InteropServices.Marshal.Copy(ptr, bytes, 0, (int)size);
     }
     return(bytes);
 }
 public IDxcOperationResult Preprocess(IDxcBlob source,
                                       string sourceName,
                                       string[] arguments,
                                       DxcDefine[] defines,
                                       IDxcIncludeHandler includeHandler)
 {
     return(Preprocess(
                source,
                sourceName,
                arguments,
                arguments?.Length ?? 0,
                defines,
                includeHandler));
 }
Example #13
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 #14
0
    private static unsafe byte[] GetBytesFromBlob(IDxcBlob blob)
    {
        byte *pMem = (byte *)blob.GetBufferPointer();
        uint  size = blob.GetBufferSize();

        byte[] result = new byte[size];
        fixed(byte *pTarget = result)
        {
            for (uint i = 0; i < size; ++i)
            {
                pTarget[i] = pMem[i];
            }
        }

        return(result);
    }
Example #15
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);
        }
        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);
        }
Example #17
0
        private void ApplyChangesButton_Click(object sender, EventArgs e)
        {
            // Turn the text into a container.
            IDxcBlobEncoding sourceBlob = EditorForm.CreateBlobForText(this.Library, this.CodeBox.Text);

            EditorForm.AssembleResult assembleResult = EditorForm.RunAssembly(this.Library, sourceBlob);
            if (assembleResult.Blob == null)
            {
                MessageBox.Show("Failed to assemble: " + assembleResult.ResultText);
                return;
            }

            // Extract the bitcode portion.
            const uint DxilKind = 0x4c495844; // 'LIXD' - DXIL
            uint       index;
            IDxcContainerReflection reflection = HlslDxcLib.CreateDxcContainerReflection();

            reflection.Load(assembleResult.Blob);
            reflection.FindFirstPartKind(DxilKind, out index);
            IDxcBlob bitcodeBlob = reflection.GetPartContent(index);

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

            passes.Add("hlsl-passes-resume");
            for (int i = PassesListBox.SelectedIndex; i < PassesListBox.Items.Count; ++i)
            {
                passes.Add(((TextSection)PassesListBox.Items[i]).Title);
            }
            string[] options = EditorForm.CreatePassOptions(passes, false, true);
            EditorForm.OptimizeResult opt = EditorForm.RunOptimize(this.Library, options, bitcodeBlob);
            if (!opt.Succeeded)
            {
                MessageBox.Show("Failed to optimize: " + opt.ResultText);
                return;
            }

            OptEditorForm form = new OptEditorForm();

            form.CodeFont        = this.CodeBox.Font;
            form.Library         = this.Library;
            form.HighLevelSource = this.HighLevelSource;
            form.Sections        = TextSection.EnumerateSections(new string[] { "MODULE-PRINT", "Phase:" }, opt.ResultText).ToArray();
            form.StartPosition   = FormStartPosition.CenterParent;
            form.Show(this);
        }
        public override void DoExport(string filePath, uTinyRipper.Version version, ref ShaderSubProgram subProgram)
        {
            byte[] exportData       = subProgram.ProgramData;
            int    dataOffset       = DXShaderProgramRestorer.GetDataOffset(version, m_graphicApi, subProgram);
            int    dataLength       = exportData.Length - dataOffset;
            IntPtr unmanagedPointer = Marshal.AllocHGlobal(dataLength);

            Marshal.Copy(exportData, dataOffset, unmanagedPointer, dataLength);

            var parameters = new object[] { unmanagedPointer, (uint)dataLength, (uint)0, null, null };

            dissassembleMethod.Invoke(null, parameters);
            IDxcBlob disassembly     = (IDxcBlob)parameters[4];
            string   disassemblyText = GetStringFromBlob(disassembly);

            File.WriteAllText(filePath, disassemblyText);
            Marshal.FreeHGlobal(unmanagedPointer);
        }
 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));
 }
Example #20
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);
            }
        }
    public unsafe Result Compile(IDxcBlob source,
                                 string sourceName,
                                 string entryPoint,
                                 string targetProfile,
                                 string[] arguments,
                                 int argumentsCount,
                                 DxcDefine[] defines,
                                 IDxcIncludeHandler includeHandler,
                                 out IDxcOperationResult?result)
    {
        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,
                                includeHandler,
                                out result);

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

            return(hr);
        }
        finally
        {
            if (argumentsPtr != null)
            {
                Interop.Free(argumentsPtr);
            }
        }
    }
 public Result CompileWithDebug(IDxcBlob source,
                                string sourceName,
                                string entryPoint,
                                string targetProfile,
                                string[] arguments,
                                DxcDefine[] defines,
                                IDxcIncludeHandler includeHandler,
                                out IDxcOperationResult?result, out string?debugBlobName, out IDxcBlob?debugBlob)
 {
     return(CompileWithDebug(
                source,
                sourceName,
                entryPoint,
                targetProfile,
                arguments,
                arguments?.Length ?? 0,
                defines,
                includeHandler,
                out result, out debugBlobName, out debugBlob));
 }
Example #23
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 #24
0
 public static string BlobToBase64(IDxcBlob blob)
 {
     return(System.Convert.ToBase64String(BlobToBytes(blob)));
 }
Example #25
0
 public extern static Int32 D3DDisassemble(
     IntPtr ptr, uint ptrSize, uint flags,
     [MarshalAs(UnmanagedType.LPStr)] string szComments,
     out IDxcBlob disassembly);
Example #26
0
 private string GetStringFromBlob(IDxcBlob blob)
 {
     return(Marshal.PtrToStringAnsi(blob.GetBufferPointer()));
 }
 public IDxcBlobEncoding GetBlobAsUtf16(IDxcBlob blob)
 {
     GetBlobAsUtf16(blob, out IDxcBlobEncoding result).CheckError();
     return(result);
 }
Example #28
0
 public IDxcOperationResult SerializeContainer(IDxcBlob source)
 {
     SerializeContainer(out IDxcOperationResult result).CheckError();
     return(result);
 }
 public IDxcBlob CreateBlobFromBlob(IDxcBlob blob, int offset, int length)
 {
     CreateBlobFromBlob(blob, offset, length, out IDxcBlob result).CheckError();
     return(result);
 }
Example #30
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);
        }