Beispiel #1
0
        public override byte[] ExportBinary()
        {
            StringBuilder sb = new StringBuilder();

            ParsedForm.ToString(sb, this);
            return(Encoding.UTF8.GetBytes(sb.ToString()));
        }
 public void ExportBinary(IExportContainer container, Stream stream, Func <Version, GPUPlatform, ShaderTextExporter> exporterInstantiator)
 {
     if (IsSerialized(container.Version))
     {
         using (ShaderWriter writer = new ShaderWriter(stream, this, exporterInstantiator))
         {
             ParsedForm.Export(writer);
         }
     }
     else if (HasBlob(container.Version))
     {
         using (ShaderWriter writer = new ShaderWriter(stream, this, exporterInstantiator))
         {
             string header = Encoding.UTF8.GetString(Script);
             if (Blobs.Length == 0)
             {
                 writer.Write(header);
             }
             else
             {
                 Blobs[0].Export(writer, header);
             }
         }
     }
     else
     {
         base.ExportBinary(container, stream);
     }
 }
Beispiel #3
0
        public override void Read(EndianStream stream)
        {
            base.Read(stream);

            ParsedForm.Read(stream);
            Name = ParsedForm.Name;

            m_platforms = stream.ReadEnum32Array((t) => (GPUPlatform)t);
            uint[] offsets             = stream.ReadUInt32Array();
            uint[] compressedLengths   = stream.ReadUInt32Array();
            uint[] decompressedLengths = stream.ReadUInt32Array();
            byte[] compressedBlob      = stream.ReadByteArray();
            stream.AlignStream(AlignType.Align4);

            m_subProgramBlobs = new ShaderSubProgramBlob[m_platforms.Length];
            using (MemoryStream memStream = new MemoryStream(compressedBlob))
            {
                for (int i = 0; i < m_platforms.Length; i++)
                {
                    uint offset             = offsets[i];
                    uint compressedLength   = compressedLengths[i];
                    uint decompressedLength = decompressedLengths[i];

                    memStream.Position = offset;
                    byte[] decompressedBuffer = new byte[decompressedLength];
                    using (Lz4Stream lz4Stream = new Lz4Stream(memStream, (int)compressedLength))
                    {
                        int read = lz4Stream.Read(decompressedBuffer, 0, decompressedBuffer.Length);
                        if (read != decompressedLength)
                        {
                            throw new Exception($"Can't properly decode shader blob. Read {read} but expected {decompressedLength}");
                        }
                    }

                    using (MemoryStream blobMem = new MemoryStream(decompressedBuffer))
                    {
                        using (EndianStream blobStream = new EndianStream(blobMem))
                        {
                            ShaderSubProgramBlob blob = new ShaderSubProgramBlob(AssetsFile);
                            blob.Read(blobStream);
                            m_subProgramBlobs[i] = blob;
                        }
                    }
                }
            }

            m_dependencies = stream.ReadArray(() => new PPtr <SShader>(AssetsFile));
            ShaderIsBaked  = stream.ReadBoolean();
            stream.AlignStream(AlignType.Align4);
        }
Beispiel #4
0
        public override void Read(EndianStream stream)
        {
            base.Read(stream);

            ParsedForm.Read(stream);
            Name                  = ParsedForm.Name.Replace("/", "_");
            m_platforms           = stream.ReadUInt32Array();
            m_offsets             = stream.ReadUInt32Array();
            m_compressedLengths   = stream.ReadUInt32Array();
            m_decompressedLengths = stream.ReadUInt32Array();
            m_compressedBlob      = stream.ReadByteArray();
            stream.AlignStream(AlignType.Align4);

            m_dependencies = stream.ReadArray(() => new PPtr <SShader>(AssetsFile));
            ShaderIsBaked  = stream.ReadBoolean();
            stream.AlignStream(AlignType.Align4);
        }
Beispiel #5
0
 public void ExportBinary(IExportContainer container, Stream stream, Func <ShaderGpuProgramType, ShaderTextExporter> exporterInstantiator)
 {
     if (IsSerialized(container.Version))
     {
         using (StreamWriter writer = new StreamWriter(stream))
         {
             ParsedForm.Export(writer, this, exporterInstantiator);
         }
     }
     else if (IsEncoded(container.Version))
     {
         using (StreamWriter writer = new StreamWriter(stream))
         {
             SubProgramBlob.Export(writer, Script, exporterInstantiator);
         }
     }
     else
     {
         base.ExportBinary(container, stream);
     }
 }
Beispiel #6
0
 public override void ExportBinary(IAssetsExporter exporter, Stream stream)
 {
     if (IsSerialized(exporter.Version))
     {
         using (StreamWriter writer = new StreamWriter(stream))
         {
             ParsedForm.Export(writer, this);
         }
     }
     else if (IsEncoded(exporter.Version))
     {
         using (StreamWriter writer = new StreamWriter(stream))
         {
             SubProgramBlob.Export(writer, Script);
         }
     }
     else
     {
         base.ExportBinary(exporter, stream);
     }
 }
Beispiel #7
0
 public void ExportBinary(IExportContainer container, Stream stream, Func <Version, ShaderGpuProgramType, ShaderTextExporter> exporterInstantiator)
 {
     if (IsSerialized(container.Version))
     {
         using (ShaderWriter writer = new ShaderWriter(stream, this, exporterInstantiator))
         {
             ParsedForm.Export(writer);
         }
     }
     else if (IsEncoded(container.Version))
     {
         using (ShaderWriter writer = new ShaderWriter(stream, this, exporterInstantiator))
         {
             string header = Encoding.UTF8.GetString(Script);
             SubProgramBlob.Export(writer, header);
         }
     }
     else
     {
         base.ExportBinary(container, stream);
     }
 }
Beispiel #8
0
        public override void Read(AssetReader reader)
        {
            if (IsSerialized(reader.Version))
            {
                ReadBase(reader);

                ParsedForm.Read(reader);

                m_platforms = reader.ReadEnum32Array((t) => (GPUPlatform)t);
                uint[] offsets             = reader.ReadUInt32Array();
                uint[] compressedLengths   = reader.ReadUInt32Array();
                uint[] decompressedLengths = reader.ReadUInt32Array();
                byte[] compressedBlob      = reader.ReadByteArray();
                reader.AlignStream(AlignType.Align4);

                m_subProgramBlobs = new ShaderSubProgramBlob[m_platforms.Length];
                using (MemoryStream memStream = new MemoryStream(compressedBlob))
                {
                    for (int i = 0; i < m_platforms.Length; i++)
                    {
                        uint offset             = offsets[i];
                        uint compressedLength   = compressedLengths[i];
                        uint decompressedLength = decompressedLengths[i];

                        memStream.Position = offset;
                        byte[] decompressedBuffer = new byte[decompressedLength];
                        using (Lz4DecodeStream lz4Stream = new Lz4DecodeStream(memStream, (int)compressedLength))
                        {
                            int read = lz4Stream.Read(decompressedBuffer, 0, decompressedBuffer.Length);
                            if (read != decompressedLength)
                            {
                                throw new Exception($"Can't properly decode shader blob. Read {read} but expected {decompressedLength}");
                            }
                        }

                        using (MemoryStream blobMem = new MemoryStream(decompressedBuffer))
                        {
                            using (AssetReader blobReader = new AssetReader(blobMem, reader.Version, reader.Platform, reader.Flags))
                            {
                                ShaderSubProgramBlob blob = new ShaderSubProgramBlob();
                                blob.Read(blobReader);
                                m_subProgramBlobs[i] = blob;
                            }
                        }
                    }
                }
            }
            else
            {
                base.Read(reader);

                if (IsEncoded(reader.Version))
                {
                    uint decompressedSize = reader.ReadUInt32();
                    int  comressedSize    = reader.ReadInt32();

                    byte[] subProgramBlob = new byte[comressedSize];
                    reader.Read(subProgramBlob, 0, comressedSize);
                    reader.AlignStream(AlignType.Align4);

                    if (comressedSize > 0 && decompressedSize > 0)
                    {
                        byte[] decompressedBuffer = new byte[decompressedSize];
                        using (MemoryStream memStream = new MemoryStream(subProgramBlob))
                        {
                            using (Lz4DecodeStream lz4Stream = new Lz4DecodeStream(memStream))
                            {
                                int read = lz4Stream.Read(decompressedBuffer, 0, decompressedBuffer.Length);
                                if (read != decompressedSize)
                                {
                                    throw new Exception($"Can't properly decode sub porgram blob. Read {read} but expected {decompressedSize}");
                                }
                            }
                        }

                        using (MemoryStream memStream = new MemoryStream(decompressedBuffer))
                        {
                            using (AssetReader blobReader = new AssetReader(memStream, reader.Version, reader.Platform, reader.Flags))
                            {
                                SubProgramBlob.Read(blobReader);
                            }
                        }
                    }
                }

                if (IsReadFallback(reader.Version))
                {
                    Fallback.Read(reader);
                }
                if (IsReadDefaultProperties(reader.Version))
                {
                    DefaultProperties.Read(reader);
                }
                if (IsReadStaticProperties(reader.Version))
                {
                    StaticProperties.Read(reader);
                }
            }

            if (IsReadDependencies(reader.Version))
            {
                m_dependencies = reader.ReadAssetArray <PPtr <Shader> >();
            }
            if (IsReadNonModifiableTextures(reader.Version))
            {
                m_nonModifiableTextures = reader.ReadAssetArray <PPtr <Texture> >();
            }
            if (IsReadShaderIsBaked(reader.Version))
            {
                ShaderIsBaked = reader.ReadBoolean();
                reader.AlignStream(AlignType.Align4);
            }
        }
        public override void Read(AssetReader reader)
        {
            if (IsSerialized(reader.Version))
            {
                ReadNamedObject(reader);

                ParsedForm.Read(reader);
                Platforms = reader.ReadArray((t) => (GPUPlatform)t);
                if (IsDoubleArray(reader.Version))
                {
                    uint[][] offsets             = reader.ReadUInt32ArrayArray();
                    uint[][] compressedLengths   = reader.ReadUInt32ArrayArray();
                    uint[][] decompressedLengths = reader.ReadUInt32ArrayArray();
                    byte[]   compressedBlob      = reader.ReadByteArray();
                    reader.AlignStream();

                    UnpackSubProgramBlobs(reader.Layout, offsets, compressedLengths, decompressedLengths, compressedBlob);
                }
                else
                {
                    uint[] offsets             = reader.ReadUInt32Array();
                    uint[] compressedLengths   = reader.ReadUInt32Array();
                    uint[] decompressedLengths = reader.ReadUInt32Array();
                    byte[] compressedBlob      = reader.ReadByteArray();
                    reader.AlignStream();

                    UnpackSubProgramBlobs(reader.Layout, offsets, compressedLengths, decompressedLengths, compressedBlob);
                }
            }
            else
            {
                base.Read(reader);

                if (HasBlob(reader.Version))
                {
                    uint   decompressedSize = reader.ReadUInt32();
                    byte[] compressedBlob   = reader.ReadByteArray();
                    reader.AlignStream();

                    UnpackSubProgramBlobs(reader.Layout, 0, (uint)compressedBlob.Length, decompressedSize, compressedBlob);
                }

                if (HasFallback(reader.Version))
                {
                    Fallback.Read(reader);
                }
                if (HasDefaultProperties(reader.Version))
                {
                    DefaultProperties.Read(reader);
                }
                if (HasStaticProperties(reader.Version))
                {
                    StaticProperties.Read(reader);
                }
            }

            if (HasDependencies(reader.Version))
            {
                Dependencies = reader.ReadAssetArray <PPtr <Shader> >();
            }
            if (HasNonModifiableTextures(reader.Version))
            {
                NonModifiableTextures = new Dictionary <string, PPtr <Texture> >();
                NonModifiableTextures.Read(reader);
            }
            if (HasShaderIsBaked(reader.Version))
            {
                ShaderIsBaked = reader.ReadBoolean();
                reader.AlignStream();
            }

#if UNIVERSAL
            if (HasErrors(reader.Version, reader.Flags))
            {
                Errors = reader.ReadAssetArray <ShaderError>();
            }
            if (HasDefaultTextures(reader.Version, reader.Flags))
            {
                DefaultTextures = new Dictionary <string, PPtr <Texture> >();
                DefaultTextures.Read(reader);
            }
            if (HasCompileInfo(reader.Version, reader.Flags))
            {
                CompileInfo.Read(reader);
            }
#endif
        }
Beispiel #10
0
        public override void Read(AssetReader reader)
        {
            if (IsSerialized(reader.Version))
            {
                ReadNamedObject(reader);

                ParsedForm.Read(reader);

                Platforms = reader.ReadArray((t) => (GPUPlatform)t);
                uint[] offsets             = reader.ReadUInt32Array();
                uint[] compressedLengths   = reader.ReadUInt32Array();
                uint[] decompressedLengths = reader.ReadUInt32Array();
                byte[] compressedBlob      = reader.ReadByteArray();
                reader.AlignStream();

                SubProgramBlobs = new ShaderSubProgramBlob[Platforms.Length];
                using (MemoryStream memStream = new MemoryStream(compressedBlob))
                {
                    for (int i = 0; i < Platforms.Length; i++)
                    {
                        uint offset             = offsets[i];
                        uint compressedLength   = compressedLengths[i];
                        uint decompressedLength = decompressedLengths[i];

                        memStream.Position = offset;
                        byte[] decompressedBuffer = new byte[decompressedLength];
                        using (Lz4DecodeStream lz4Stream = new Lz4DecodeStream(memStream, (int)compressedLength))
                        {
                            lz4Stream.ReadBuffer(decompressedBuffer, 0, decompressedBuffer.Length);
                        }

                        using (MemoryStream blobMem = new MemoryStream(decompressedBuffer))
                        {
                            using (AssetReader blobReader = new AssetReader(blobMem, EndianType.LittleEndian, reader.Version, reader.Platform, reader.Flags))
                            {
                                ShaderSubProgramBlob blob = new ShaderSubProgramBlob();
                                blob.Read(blobReader);
                                SubProgramBlobs[i] = blob;
                            }
                        }
                    }
                }
            }
            else
            {
                base.Read(reader);

                if (IsEncoded(reader.Version))
                {
                    uint decompressedSize = reader.ReadUInt32();
                    int  comressedSize    = reader.ReadInt32();
                    if (comressedSize > 0 && decompressedSize > 0)
                    {
                        byte[] subProgramBlob = new byte[comressedSize];
                        reader.ReadBuffer(subProgramBlob, 0, comressedSize);

                        byte[] decompressedBuffer = new byte[decompressedSize];
                        using (MemoryStream memStream = new MemoryStream(subProgramBlob))
                        {
                            using (Lz4DecodeStream lz4Stream = new Lz4DecodeStream(memStream))
                            {
                                lz4Stream.ReadBuffer(decompressedBuffer, 0, decompressedBuffer.Length);
                            }
                        }

                        using (MemoryStream memStream = new MemoryStream(decompressedBuffer))
                        {
                            using (AssetReader blobReader = new AssetReader(memStream, EndianType.LittleEndian, reader.Version, reader.Platform, reader.Flags))
                            {
                                SubProgramBlob.Read(blobReader);
                            }
                        }
                    }
                    reader.AlignStream();
                }

                if (HasFallback(reader.Version))
                {
                    Fallback.Read(reader);
                }
                if (HasDefaultProperties(reader.Version))
                {
                    DefaultProperties.Read(reader);
                }
                if (HasStaticProperties(reader.Version))
                {
                    StaticProperties.Read(reader);
                }
            }

            if (HasDependencies(reader.Version))
            {
                Dependencies = reader.ReadAssetArray <PPtr <Shader> >();
            }
            if (HasNonModifiableTextures(reader.Version))
            {
                NonModifiableTextures = new Dictionary <string, PPtr <Texture> >();
                NonModifiableTextures.Read(reader);
            }
            if (HasShaderIsBaked(reader.Version))
            {
                ShaderIsBaked = reader.ReadBoolean();
                reader.AlignStream();
            }

#if UNIVERSAL
            if (HasErrors(reader.Version, reader.Flags))
            {
                Errors = reader.ReadAssetArray <ShaderError>();
            }
            if (HasDefaultTextures(reader.Version, reader.Flags))
            {
                DefaultTextures = new Dictionary <string, PPtr <Texture> >();
                DefaultTextures.Read(reader);
            }
            if (HasCompileInfo(reader.Version, reader.Flags))
            {
                CompileInfo.Read(reader);
            }
#endif
        }