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
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
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
        }