Beispiel #1
0
        void ExportSerializedSubProgram(SerializedSubProgram subProgram, ShaderWriter writer, ShaderSubProgramBlob blob, ShaderType type, bool isTier, bool isBest)
        {
            writer.WriteIndent(4);
            writer.Write("SubProgram \"{0} ", subProgram.GpuProgramType.ToGPUPlatform(writer.Platform));
            if (isTier)
            {
                writer.Write("hw_tier{0} ", subProgram.ShaderHardwareTier.ToString("00"));
            }
            writer.Write("\" {\n");
            writer.WriteIndent(5);

            var    shaderSubProgram = blob.SubPrograms[(int)subProgram.BlobIndex];
            string hash             = Hash(shaderSubProgram.ProgramData.ToArray());
            var    filesteam        = writer.BaseStream as FileStream;
            var    folder           = Path.GetDirectoryName(filesteam.Name);

            DXShaderObjectExporter.FixShaderSubProgram(shaderSubProgram,
                                                       subProgram);
            ExportShaderSubProgram(shaderSubProgram, writer, type, isBest);

            writer.Write('\n');
            writer.WriteIndent(4);
            writer.Write("}\n");
        }
Beispiel #2
0
        void ExportGLSL(ShaderSubProgram subProgram, ShaderWriter writer, ShaderType type, bool isBest)
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            string hash = Hash(subProgram.ProgramData.ToArray());

            Logger.Log(LogType.Debug, LogCategory.Export, $"Exporting Subprogram {hash}");

            var data = DXShaderObjectExporter.GetObjectData(writer.Version, subProgram.ProgramType.ToGPUPlatform(writer.Platform), subProgram);

            var filesteam = writer.BaseStream as FileStream;
            var folder    = Path.GetDirectoryName(filesteam.Name);

            var dxExporter = new uTinyRipperGUI.Exporters.ShaderDXExporter(
                writer.Shader.File.Version, subProgram.ProgramType.ToGPUPlatform(writer.Platform));
            string asmPath = $"{folder}/{hash}.dxasm";

            if (!File.Exists(asmPath))
            {
                using (var sw = new StreamWriter($@"{folder}\{hash}.dxasm"))
                {
                    dxExporter.Export(subProgram.ProgramData.ToArray(), sw);
                }
                Logger.Log(LogType.Debug, LogCategory.Export, $"Exported asm {stopWatch.ElapsedMilliseconds} ms");
                stopWatch.Restart();
            }

            string objPath  = $"{folder}/{hash}.o";
            string glslPath = $"{folder}/{hash}.glsl";

            File.WriteAllBytes(objPath, data);
            writer.WriteLine($"// {hash}.glsl");
            writer.WriteLine($"// Objfile Length {data.Length}");
            var variableCount = subProgram.ConstantBuffers
                                .Select(cb => cb.MatrixParams.Count + cb.VectorParams.Count)
                                .Sum();

            writer.WriteLine($"// Variables {variableCount}");
            var keyWordScore = 0;

            keyWordScore += subProgram.GlobalKeywords.Sum(keyword => keyword.Contains("ON") ? 2 : 1);
            keyWordScore += subProgram.LocalKeywords == null ? 0 : subProgram.LocalKeywords.Sum(keyword => keyword.Contains("ON") ? 2 : 1);
            writer.WriteLine($"// KeywordScore {keyWordScore}");
            writer.WriteLine($"// Best {isBest}");
            if (!File.Exists(glslPath))
            {
                Process process = new Process();
                process.StartInfo.FileName               = "HLSLcc.exe";
                process.StartInfo.Arguments              = $"-in={objPath} -out={glslPath}";
                process.StartInfo.UseShellExecute        = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError  = true;
                var result = process.Start();
                //* Read the output (or the error)
                string output = process.StandardOutput.ReadToEnd();
                Console.WriteLine(output);
                string err = process.StandardError.ReadToEnd();
                Console.WriteLine(err);
                process.WaitForExit();

                Logger.Log(LogType.Debug, LogCategory.Export, $"Cross Compiled HLSL {stopWatch.ElapsedMilliseconds} ms");
                if (process.ExitCode != 0)
                {
                    writer.WriteLine($"//Error with {hash}");
                    writer.WriteLine($"//Output: {output}");
                    writer.WriteLine($"//Error: {err}");
                }
            }
            stopWatch.Stop();
            //File.Delete(objPath);
        }