Example #1
0
 void ExportSerializedProgram(SerializedProgram serializedProgram, ShaderWriter writer, ShaderType type)
 {
     if (serializedProgram.SubPrograms.Count > 0)
     {
         writer.WriteIndent(3);
         writer.Write("Program \"{0}\" {{\n", type.ToProgramTypeString());
         HashSet <ShaderGpuProgramType> isTierLookup = GetIsTierLookup(serializedProgram.SubPrograms);
         var subprograms = serializedProgram.SubPrograms;
         var best        = serializedProgram.SubPrograms
                           .OrderByDescending(subProgram =>
         {
             GPUPlatform platform      = subProgram.GpuProgramType.ToGPUPlatform(writer.Platform);
             int index                 = writer.Shader.Platforms.IndexOf(platform);
             ShaderSubProgramBlob blob = writer.Shader.SubProgramBlobs[index];
             var sp = blob.SubPrograms[(int)subProgram.BlobIndex];
             return(sp.ProgramData.Count);
             //return sp.GlobalKeywords.Sum(keyword => keyword.Contains("ON") ? 2 : 1);
         }).FirstOrDefault();
         if (FilterSubPrograms)
         {
             subprograms = new SerializedSubProgram[] { best };
         }
         foreach (SerializedSubProgram subProgram in subprograms)
         {
             Platform             uplatform = writer.Platform;
             GPUPlatform          platform  = subProgram.GpuProgramType.ToGPUPlatform(uplatform);
             int                  index     = writer.Shader.Platforms.IndexOf(platform);
             ShaderSubProgramBlob blob      = writer.Shader.SubProgramBlobs[index];
             bool                 isTier    = isTierLookup.Contains(subProgram.GpuProgramType);
             ExportSerializedSubProgram(subProgram, writer, blob, type, isTier, best.BlobIndex == subProgram.BlobIndex);
         }
         writer.WriteIndent(3);
         writer.Write("}\n");
     }
 }
Example #2
0
        public void WriteShaderData(ref ShaderSubProgram subProgram)
        {
            GPUPlatform        graphicApi = subProgram.ProgramType.ToGPUPlatform(Platform);
            ShaderTextExporter exporter   = m_exporterInstantiator.Invoke(Shader.File.Version, graphicApi);

            exporter.Export(this, ref subProgram);
        }
        public AsmExporter(GPUPlatform graphicApi)
        {
            m_graphicApi = graphicApi;
            var type = Type.GetType("D3DCompiler.D3DCompiler, uTinyRipperUtility");

            dissassembleMethod = type.GetMethod("D3DDisassemble");
        }
Example #4
0
        public static byte[] GetObjectData(Version m_version, GPUPlatform m_graphicApi, ShaderSubProgram shaderSubProgram)
        {
            var shaderData = shaderSubProgram.ProgramData.ToArray();

            int dataOffset = 0;

            if (IsOffset(m_graphicApi))
            {
                dataOffset = IsOffset5(m_version) ? 5 : 6;
                uint fourCC = BitConverter.ToUInt32(shaderData, dataOffset);
                if (fourCC != DXBCFourCC)
                {
                    throw new Exception("Magic number doesn't match");
                }
            }
            int length = shaderData.Length - dataOffset;

            var memoryStream = new MemoryStream(shaderData, dataOffset, length);
            var outStream    = new MemoryStream();

            using (var reader = new EndianReader(memoryStream))
                using (var writer = new EndianWriter(outStream))
                {
                    var magicBytes   = reader.ReadBytes(4);
                    var checksum     = reader.ReadBytes(16);
                    var unknown0     = reader.ReadUInt32();
                    var totalSize    = reader.ReadUInt32();
                    var chunkCount   = reader.ReadUInt32();
                    var chunkOffsets = new List <uint>();
                    for (int i = 0; i < chunkCount; i++)
                    {
                        chunkOffsets.Add(reader.ReadUInt32());
                    }
                    var offset            = (uint)memoryStream.Position + 4;
                    var resourceChunkData = GetResourceChunk(shaderSubProgram);
                    //Adjust for new chunk
                    totalSize += (uint)resourceChunkData.Length;
                    for (int i = 0; i < chunkCount; i++)
                    {
                        chunkOffsets[i] += (uint)resourceChunkData.Length + 4;
                    }
                    chunkOffsets.Insert(0, offset);
                    chunkCount += 1;
                    totalSize  += (uint)resourceChunkData.Length;

                    writer.Write(magicBytes);
                    writer.Write(checksum);
                    writer.Write(unknown0);
                    writer.Write(totalSize);
                    writer.Write(chunkCount);
                    foreach (var chunkOffset in chunkOffsets)
                    {
                        writer.Write(chunkOffset);
                    }
                    writer.Write(resourceChunkData);
                    var rest = reader.ReadBytes((int)memoryStream.Length - (int)memoryStream.Position);
                    writer.Write(rest);
                    return(outStream.ToArray());
                }
        }
 public static int GetDataOffset(Version version, GPUPlatform graphicApi)
 {
     if (HasHeader(graphicApi))
     {
         return(HasGSInputPrimitive(version) ? 6 : 5);
     }
     else
     {
         return(0);
     }
 }
Example #6
0
        public static byte[] RestoreProgramData(Version version, GPUPlatform graphicApi, ShaderSubProgram shaderSubProgram)
        {
            int dataOffset = GetDataOffset(version, graphicApi, shaderSubProgram);

            using (MemoryStream src = new MemoryStream(shaderSubProgram.ProgramData, dataOffset, shaderSubProgram.ProgramData.Length - dataOffset))
            {
                using (EndianReader reader = new EndianReader(src))
                {
                    using (MemoryStream dest = new MemoryStream())
                    {
                        using (EndianWriter writer = new EndianWriter(dest))
                        {
                            byte[]      magicBytes   = reader.ReadBytes(4);
                            byte[]      checksum     = reader.ReadBytes(16);
                            uint        unknown0     = reader.ReadUInt32();
                            uint        totalSize    = reader.ReadUInt32();
                            uint        chunkCount   = reader.ReadUInt32();
                            List <uint> chunkOffsets = new List <uint>();
                            for (int i = 0; i < chunkCount; i++)
                            {
                                chunkOffsets.Add(reader.ReadUInt32());
                            }
                            uint   offset            = (uint)src.Position + 4;
                            byte[] resourceChunkData = GetResourceChunk(shaderSubProgram);
                            //Adjust for new chunk
                            totalSize += (uint)resourceChunkData.Length;
                            for (int i = 0; i < chunkCount; i++)
                            {
                                chunkOffsets[i] += (uint)resourceChunkData.Length + 4;
                            }
                            chunkOffsets.Insert(0, offset);
                            chunkCount += 1;
                            totalSize  += (uint)resourceChunkData.Length;

                            writer.Write(magicBytes);
                            writer.Write(checksum);
                            writer.Write(unknown0);
                            writer.Write(totalSize);
                            writer.Write(chunkCount);
                            foreach (uint chunkOffset in chunkOffsets)
                            {
                                writer.Write(chunkOffset);
                            }
                            writer.Write(resourceChunkData);
                            byte[] rest = reader.ReadBytes((int)src.Length - (int)src.Position);
                            writer.Write(rest);
                            return(dest.ToArray());
                        }
                    }
                }
            }
        }
Example #7
0
        public static int GetDataOffset(Version version, GPUPlatform graphicApi, ShaderSubProgram shaderSubProgram)
        {
            int dataOffset = 0;

            if (IsOffset(graphicApi))
            {
                dataOffset = IsOffset5(version) ? 5 : 6;
                uint fourCC = BitConverter.ToUInt32(shaderSubProgram.ProgramData, dataOffset);
                if (fourCC != DXBCFourCC)
                {
                    throw new Exception("Magic number doesn't match");
                }
            }
            return(dataOffset);
        }
Example #8
0
        public void Export(ShaderWriter writer, ShaderType type)
        {
            writer.WriteIndent(3);
            writer.Write("Program \"{0}\" {{\n", type.ToProgramTypeString());
            int tierCount = GetTierCount(SubPrograms);

            for (int i = 0; i < SubPrograms.Length; i++)
            {
                SerializedSubProgram subProgram = SubPrograms[i];
                Platform             uplatform  = writer.Platform;
                GPUPlatform          platform   = subProgram.GpuProgramType.ToGPUPlatform(uplatform);
                int index = writer.Shader.Platforms.IndexOf(platform);
                ref ShaderSubProgramBlob blob = ref writer.Shader.SubProgramBlobs[index];
                subProgram.Export(writer, ref blob, type, tierCount > 1);
            }
Example #9
0
 public void Export(TextWriter writer, Shader shader, ShaderType type, Func <ShaderGpuProgramType, ShaderTextExporter> exporterInstantiator)
 {
     if (SubPrograms.Count > 0)
     {
         writer.WriteIntent(3);
         writer.Write("Program \"{0}\" {{\n", type.ToProgramTypeString());
         foreach (SerializedSubProgram subProgram in SubPrograms)
         {
             Platform             uplatform = shader.File.Platform;
             GPUPlatform          platform  = subProgram.GpuProgramType.ToGPUPlatform(uplatform);
             int                  index     = shader.Platforms.IndexOf(platform);
             ShaderSubProgramBlob blob      = shader.SubProgramBlobs[index];
             int                  count     = SubPrograms.Where(t => t.GpuProgramType == subProgram.GpuProgramType).Select(t => t.ShaderHardwareTier).Distinct().Count();
             subProgram.Export(writer, blob, uplatform, count > 1, exporterInstantiator);
         }
         writer.WriteIntent(3);
         writer.Write("}\n");
     }
 }
Example #10
0
 public StringBuilder ToString(StringBuilder sb, SShader shader, ShaderType type)
 {
     if (SubPrograms.Count > 0)
     {
         sb.AppendIntent(3).Append("Program").Append(' ');
         sb.Append('"').Append(type.ToProgramTypeString()).Append('"');
         sb.Append(' ').Append('{').Append('\n');
         foreach (SerializedSubProgram subProgram in SubPrograms)
         {
             GPUPlatform          platform = subProgram.GpuProgramType.ToGPUPlatform(m_assetsFile.Platform);
             int                  index    = shader.Platforms.IndexOf(platform);
             ShaderSubProgramBlob blob     = shader.SubProgramBlobs[index];
             int                  count    = SubPrograms.Where(t => t.GpuProgramType == subProgram.GpuProgramType).Select(t => t.ShaderHardwareTier).Distinct().Count();
             subProgram.ToString(sb, blob, count > 1);
         }
         sb.AppendIntent(3).Append('}').Append('\n');
     }
     return(sb);
 }
Example #11
0
 public void Export(ShaderWriter writer, ShaderType type)
 {
     if (SubPrograms.Count > 0)
     {
         writer.WriteIntent(3);
         writer.Write("Program \"{0}\" {{\n", type.ToProgramTypeString());
         HashSet <ShaderGpuProgramType> isTierLookup = GetIsTierLookup(SubPrograms);
         foreach (SerializedSubProgram subProgram in SubPrograms)
         {
             Platform             uplatform = writer.Platform;
             GPUPlatform          platform  = subProgram.GpuProgramType.ToGPUPlatform(uplatform);
             int                  index     = writer.Shader.Platforms.IndexOf(platform);
             ShaderSubProgramBlob blob      = writer.Shader.SubProgramBlobs[index];
             bool                 isTier    = isTierLookup.Contains(subProgram.GpuProgramType);
             subProgram.Export(writer, blob, isTier);
         }
         writer.WriteIntent(3);
         writer.Write("}\n");
     }
 }
        public void Export(ShaderWriter writer, ShaderType type, bool isTier)
        {
            writer.WriteIndent(4);
#warning TODO: convertion (DX to HLSL)
            writer.Write("SubProgram \"{0} ", GpuProgramType.ToGPUPlatform(writer.Platform));
            if (isTier)
            {
                writer.Write("hw_tier{0} ", ShaderHardwareTier.ToString("00"));
            }
            writer.Write("\" {\n");
            writer.WriteIndent(5);

            GPUPlatform gpuPlatform   = GpuProgramType.ToGPUPlatform(writer.Platform);
            int         platformIndex = writer.Shader.Platforms.IndexOf(gpuPlatform);
            writer.Shader.Blobs[platformIndex].SubPrograms[BlobIndex].Export(writer, type);

            writer.Write('\n');
            writer.WriteIndent(4);
            writer.Write("}\n");
        }
        private ShaderTextExporter HLSLShaderExporterInstantiator(Version version, GPUPlatform graphicApi)
        {
            switch (graphicApi)
            {
            case GPUPlatform.d3d11:
                var glLang = settings.ShaderExportMode == ShaderExportMode.Metal ?
                             HLSLccWrapper.WrappedGLLang.LANG_METAL :
                             HLSLccWrapper.WrappedGLLang.LANG_DEFAULT;
                return(new HLSLccExporter(graphicApi, glLang));

            case GPUPlatform.d3d9:
            case GPUPlatform.d3d11_9x:
                return(new AsmExporter(graphicApi));

            case GPUPlatform.vulkan:
                return(new ShaderVulkanExporter());

            default:
                return(Shader.DefaultShaderExporterInstantiator(version, graphicApi));
            }
        }
        public static ShaderTextExporter DefaultShaderExporterInstantiator(Version version, GPUPlatform graphicApi)
        {
            switch (graphicApi)
            {
            case GPUPlatform.unknown:
                return(new ShaderTextExporter());

            case GPUPlatform.openGL:
            case GPUPlatform.gles:
            case GPUPlatform.gles3:
            case GPUPlatform.glcore:
                return(new ShaderGLESExporter());

            case GPUPlatform.metal:
                return(new ShaderMetalExporter());

            default:
                return(new ShaderUnknownExporter(graphicApi));
            }
        }
Example #15
0
 public ShaderDXExporter(Version version, GPUPlatform graphicApi)
 {
     m_version    = version;
     m_graphicApi = graphicApi;
 }
Example #16
0
        public void WriteShaderData(GPUPlatform graphicApi, byte[] shaderData)
        {
            ShaderTextExporter exporter = m_exporterInstantiator.Invoke(Shader.File.Version, graphicApi);

            exporter.Export(shaderData, this);
        }
Example #17
0
 public HLSLccExporter(GPUPlatform graphicApi, WrappedGLLang lang)
 {
     m_graphicApi = graphicApi;
     m_GLLang     = lang;
 }
 /// <summary>
 /// Not D3D9
 /// </summary>
 public static bool HasHeader(GPUPlatform graphicApi) => graphicApi != GPUPlatform.d3d9;
Example #19
0
 public ShaderHLSLccExporter(GPUPlatform graphicApi) :
     base(graphicApi)
 {
 }
Example #20
0
 public ShaderDXExporter(GPUPlatform graphicApi)
 {
     m_graphicApi = graphicApi;
 }
Example #21
0
 public ShaderUnknownExporter(GPUPlatform graphicApi)
 {
     m_graphicApi = graphicApi;
 }
Example #22
0
 private static bool IsOffset(GPUPlatform graphicApi)
 {
     return(graphicApi != GPUPlatform.d3d9);
 }
Example #23
0
 /// <summary>
 /// Not D3D9
 /// </summary>
 private static bool IsOffset(GPUPlatform graphicApi) => graphicApi != GPUPlatform.d3d9;
Example #24
0
        private static ShaderTextExporter ShaderExporterInstantiator(Version version, GPUPlatform graphicApi)
        {
            switch (graphicApi)
            {
            case GPUPlatform.d3d9:
                return(new ShaderDXExporter(graphicApi));

            case GPUPlatform.d3d11_9x:
            case GPUPlatform.d3d11:
                return(new ShaderHLSLccExporter(graphicApi));

            case GPUPlatform.vulkan:
                return(new ShaderVulkanExporter());

            default:
                return(Shader.DefaultShaderExporterInstantiator(version, graphicApi));
            }
        }
        private ShaderTextExporter AssemblyShaderExporterInstantiator(Version version, GPUPlatform graphicApi)
        {
            switch (graphicApi)
            {
            case GPUPlatform.d3d9:
            case GPUPlatform.d3d11_9x:
            case GPUPlatform.d3d11:
                return(new AsmExporter(graphicApi));

            case GPUPlatform.vulkan:
                return(new ShaderVulkanExporter());

            default:
                return(Shader.DefaultShaderExporterInstantiator(version, graphicApi));
            }
        }