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)) { ReadBase(reader); ParsedForm.Read(reader); 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); 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, 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, reader.Version, reader.Platform, reader.Flags)) { SubProgramBlob.Read(blobReader); } } } reader.AlignStream(AlignType.Align4); } 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)) { Dependencies = reader.ReadAssetArray <PPtr <Shader> >(); } if (IsReadNonModifiableTextures(reader.Version)) { NonModifiableTextures = new Dictionary <string, PPtr <Texture> >(); NonModifiableTextures.Read(reader); } if (IsReadShaderIsBaked(reader.Version)) { ShaderIsBaked = reader.ReadBoolean(); reader.AlignStream(AlignType.Align4); } #if UNIVERSAL if (IsReadErrors(reader.Version, reader.Flags)) { Errors = reader.ReadAssetArray <ShaderError>(); } if (IsReadDefaultTextures(reader.Version, reader.Flags)) { DefaultTextures = new Dictionary <string, PPtr <Texture> >(); DefaultTextures.Read(reader); } if (IsReadCompileInfo(reader.Version, reader.Flags)) { CompileInfo.Read(reader); } #endif }