protected internal override SoundEffect Read(ContentReader input, SoundEffect existingInstance)
        {
            byte[] header     = input.ReadBytes(input.ReadInt32());
            byte[] data       = input.ReadBytes(input.ReadInt32());
            int    loopStart  = input.ReadInt32();
            int    loopLength = input.ReadInt32();
            int    num        = input.ReadInt32();

            using (MemoryStream mStream = new MemoryStream(20 + header.Length + 8 + data.Length))
            {
                using (BinaryWriter writer = new BinaryWriter(mStream))
                {
                    writer.Write("RIFF".ToCharArray());
                    writer.Write((int)(20 + header.Length + data.Length));
                    writer.Write("WAVE".ToCharArray());

                    //header can be written as-is
                    writer.Write("fmt ".ToCharArray());
                    writer.Write(header.Length);
                    writer.Write(header);

                    writer.Write("data".ToCharArray());
                    writer.Write((int)data.Length);
                    writer.Write(data);

                    writer.Close();
                    mStream.Close();

                    return(new SoundEffect(input.AssetName, mStream.ToArray()));
                }
            }
        }
Exemple #2
0
        protected internal override SoundEffect Read(ContentReader input, SoundEffect existingInstance)
        {
            byte[] buffer1 = input.ReadBytes(input.ReadInt32());
            byte[] buffer2 = input.ReadBytes(input.ReadInt32());
            input.ReadInt32();
            input.ReadInt32();
            input.ReadInt32();
            byte[]       data         = (byte[])null;
            MemoryStream memoryStream = new MemoryStream(20 + buffer1.Length + 8 + buffer2.Length);

            using (BinaryWriter binaryWriter = new BinaryWriter((Stream)memoryStream))
            {
                binaryWriter.Write("RIFF".ToCharArray());
                binaryWriter.Write(20 + buffer1.Length + buffer2.Length);
                binaryWriter.Write("WAVE".ToCharArray());
                binaryWriter.Write("fmt ".ToCharArray());
                binaryWriter.Write(buffer1.Length);
                binaryWriter.Write(buffer1);
                binaryWriter.Write("data".ToCharArray());
                binaryWriter.Write(buffer2.Length);
                binaryWriter.Write(buffer2);
                data = memoryStream.ToArray();
            }
            if (data == null)
            {
                throw new ContentLoadException("Failed to load SoundEffect");
            }
            else
            {
                return(new SoundEffect(input.AssetName, data));
            }
        }
Exemple #3
0
        /// <summary>
        /// Read and create a SilverlightEffect
        /// </summary>
        protected override SilverlightEffect Read(ContentReader input, SilverlightEffect existingInstance)
        {
            int techniquesCount = input.ReadInt32();

            EffectTechnique[] techniques = new EffectTechnique[techniquesCount];

            for (int techniqueIndex = 0; techniqueIndex < techniquesCount; techniqueIndex++)
            {
                int          passesCount = input.ReadInt32();
                EffectPass[] passes      = new EffectPass[passesCount];

                for (int passIndex = 0; passIndex < passesCount; passIndex++)
                {
                    string passName = input.ReadString();

                    // Vertex shader
                    int    vertexShaderByteCodeLength   = input.ReadInt32();
                    byte[] vertexShaderByteCode         = input.ReadBytes(vertexShaderByteCodeLength);
                    int    vertexShaderParametersLength = input.ReadInt32();
                    byte[] vertexShaderParameters       = input.ReadBytes(vertexShaderParametersLength);

                    // Pixel shader
                    int    pixelShaderByteCodeLength   = input.ReadInt32();
                    byte[] pixelShaderByteCode         = input.ReadBytes(pixelShaderByteCodeLength);
                    int    pixelShaderParametersLength = input.ReadInt32();
                    byte[] pixelShaderParameters       = input.ReadBytes(pixelShaderParametersLength);

                    MemoryStream vertexShaderCodeStream       = new MemoryStream(vertexShaderByteCode);
                    MemoryStream pixelShaderCodeStream        = new MemoryStream(pixelShaderByteCode);
                    MemoryStream vertexShaderParametersStream = new MemoryStream(vertexShaderParameters);
                    MemoryStream pixelShaderParametersStream  = new MemoryStream(pixelShaderParameters);

                    // Instanciate pass
                    SilverlightEffectPass currentPass = new SilverlightEffectPass(passName, GraphicsDeviceManager.Current.GraphicsDevice, vertexShaderCodeStream, pixelShaderCodeStream, vertexShaderParametersStream, pixelShaderParametersStream);
                    passes[passIndex] = currentPass;

                    vertexShaderCodeStream.Dispose();
                    pixelShaderCodeStream.Dispose();
                    vertexShaderParametersStream.Dispose();
                    pixelShaderParametersStream.Dispose();

                    // Render states
                    int renderStatesCount = input.ReadInt32();

                    for (int renderStateIndex = 0; renderStateIndex < renderStatesCount; renderStateIndex++)
                    {
                        currentPass.AppendState(input.ReadString(), input.ReadString());
                    }
                }

                // Instanciate technique
                techniques[techniqueIndex] = new EffectTechnique(passes);
            }

            return(new SilverlightEffect(techniques));
        }
        protected internal override SoundEffect Read(
            ContentReader input,
            SoundEffect existingInstance
            )
        {
            // Format block length
            uint formatLength = input.ReadUInt32();

            // Wavedata format
            ushort format = input.ReadUInt16();

            // Number of channels
            ushort channels = input.ReadUInt16();

            // Sample rate
            uint sampleRate = input.ReadUInt32();

            // Averate bytes per second, unused
            input.ReadUInt32();

            // Block alignment, needed for MSADPCM
            ushort blockAlign = input.ReadUInt16();

            // Bit depth
            ushort bitDepth = input.ReadUInt16();

            // cbSize, unused
            input.ReadUInt16();

            // Seek past the rest of this crap (cannot seek though!)
            input.ReadBytes((int)(formatLength - 18));

            // Wavedata
            byte[] data = input.ReadBytes(input.ReadInt32());

            // Loop information
            uint loopStart  = input.ReadUInt32();
            uint loopLength = input.ReadUInt32();

            // Sound duration in milliseconds, unused
            input.ReadUInt32();

            return(new SoundEffect(
                       input.AssetName,
                       data,
                       sampleRate,
                       channels,
                       loopStart,
                       loopLength,
                       format == 2,
                       (uint)((format == 2) ? (((blockAlign / channels) - 6) * 2) : (bitDepth / 16))
                       ));
        }
Exemple #5
0
        protected internal override SoundEffect Read(
            ContentReader input,
            SoundEffect existingInstance
            )
        {
            /* Swap endian - this is one of the very few places requiring this!
             * Note: This only affects the fmt chunk that's glued into the file.
             */
            bool se = input.platform == 'x';

            // Format block length
            uint formatLength = input.ReadUInt32();

            // WaveFormatEx data
            ushort wFormatTag      = Swap(se, input.ReadUInt16());
            ushort nChannels       = Swap(se, input.ReadUInt16());
            uint   nSamplesPerSec  = Swap(se, input.ReadUInt32());
            uint   nAvgBytesPerSec = Swap(se, input.ReadUInt32());
            ushort nBlockAlign     = Swap(se, input.ReadUInt16());
            ushort wBitsPerSample  = Swap(se, input.ReadUInt16());

            /* ushort cbSize =*/ input.ReadUInt16();

            // Seek past the rest of this crap (cannot seek though!)
            input.ReadBytes((int)(formatLength - 18));

            // Wavedata
            byte[] data = input.ReadBytes(input.ReadInt32());

            // Loop information
            int loopStart  = input.ReadInt32();
            int loopLength = input.ReadInt32();

            // Sound duration in milliseconds, unused
            input.ReadUInt32();

            return(new SoundEffect(
                       input.AssetName,
                       data,
                       0,
                       data.Length,
                       wFormatTag,
                       nChannels,
                       nSamplesPerSec,
                       nAvgBytesPerSec,
                       nBlockAlign,
                       wBitsPerSample,
                       loopStart,
                       loopLength
                       ));
        }
Exemple #6
0
        protected internal override Texture2D Read(ContentReader reader, Texture2D existingInstance)
        {
            Texture2D texture = null;

            SurfaceFormat surfaceFormat = (SurfaceFormat)reader.ReadInt32();
            int           width         = reader.ReadInt32();
            int           height        = reader.ReadInt32();
            int           levelCount    = reader.ReadInt32();
            int           imageLength   = reader.ReadInt32();

            byte[] imageData = reader.ReadBytes(imageLength);

            switch (surfaceFormat)
            {
            case SurfaceFormat.Dxt1: imageData = DxtUtil.DecompressDxt1(imageData, width, height); break;

            case SurfaceFormat.Dxt3: imageData = DxtUtil.DecompressDxt3(imageData, width, height); break;
            }

            IntPtr imagePtr = IntPtr.Zero;

            try
            {
                imagePtr = Marshal.AllocHGlobal(imageData.Length);
                Marshal.Copy(imageData, 0, imagePtr, imageData.Length);
                ESTexture2D esTexture = new ESTexture2D(imagePtr, surfaceFormat, width, height, new Size(width, height), All.Linear);
                texture = new Texture2D(new ESImage(esTexture));
            }
            finally
            {
                Marshal.FreeHGlobal(imagePtr);
            }

            return(texture);
        }
        protected internal override Texture3D Read(ContentReader reader, Texture3D existingInstance)
        {
            Texture3D texture = null;

            SurfaceFormat format     = (SurfaceFormat)reader.ReadInt32();
            int           width      = reader.ReadInt32();
            int           height     = reader.ReadInt32();
            int           depth      = reader.ReadInt32();
            int           levelCount = reader.ReadInt32();

            if (existingInstance == null)
            {
                texture = new Texture3D(reader.GraphicsDevice, width, height, depth, levelCount > 1, format);
            }
            else
            {
                texture = existingInstance;
            }

            for (int i = 0; i < levelCount; i++)
            {
                int    dataSize = reader.ReadInt32();
                byte[] data     = reader.ReadBytes(dataSize);
                texture.SetData(i, 0, 0, width, height, 0, depth, data, 0, dataSize);

                // Calculate dimensions of next mip level.
                width  = Math.Max(width >> 1, 1);
                height = Math.Max(height >> 1, 1);
                depth  = Math.Max(depth >> 1, 1);
            }

            return(texture);
        }
		/// <summary></summary><param name="reader"></param>
		public ParticleSystemCompiledShaderData(ContentReader reader)
		{
			this.CompressedShaderCode = reader.ReadBytes(reader.ReadInt32());
			this.ColourSamplerIndex = reader.ReadInt32();
			this.UserSamplerIndex = reader.ReadInt32();
			this.LifeSamplerIndex = reader.ReadInt32();
		}
Exemple #9
0
        protected internal override Texture2D Read(ContentReader reader, Texture2D existingInstance)
        {
            Texture2D texture = null;

            SurfaceFormat  surfaceFormat   = (SurfaceFormat)reader.ReadInt32();
            int            width           = (reader.ReadInt32());
            int            height          = (reader.ReadInt32());
            int            levelCount      = (reader.ReadInt32());
            SetDataOptions compressionType = (SetDataOptions)reader.ReadInt32();
            int            imageLength     = width * height * 4;

            if (surfaceFormat == SurfaceFormat.Dxt3)
            {
                ESTexture2D temp = ESTexture2D.InitiFromDxt3File(reader, imageLength, width, height);
                texture = new Texture2D(new ESImage(temp));
            }
            else
            {
                byte[] imageBytes = reader.ReadBytes(imageLength);
                IntPtr ptr        = Marshal.AllocHGlobal(imageLength);
                try
                {
                    Marshal.Copy(imageBytes, 0, ptr, imageLength);
                    ESTexture2D temp = new ESTexture2D(ptr, SurfaceFormat.Rgba32, width, height, new Size(width, height), All.Linear);
                    texture = new Texture2D(new ESImage(temp));
                }
                finally
                {
                    Marshal.FreeHGlobal(ptr);
                }
            }

            return(texture);
        }
Exemple #10
0
        protected internal override TextureCube Read(ContentReader reader, TextureCube existingInstance)
        {
            TextureCube textureCube = null;

            SurfaceFormat surfaceFormat = (SurfaceFormat)reader.ReadInt32();
            int           size          = reader.ReadInt32();
            int           levels        = reader.ReadInt32();

            if (existingInstance == null)
            {
                textureCube = new TextureCube(reader.GraphicsDevice, size, levels > 1, surfaceFormat);
            }
            else
            {
                textureCube = existingInstance;
            }

            for (int face = 0; face < 6; face++)
            {
                for (int i = 0; i < levels; i++)
                {
                    int    faceSize = reader.ReadInt32();
                    byte[] faceData = reader.ReadBytes(faceSize);
                    textureCube.SetData <byte>((CubeMapFace)face, i, null, faceData, 0, faceSize);
                }
            }

            return(textureCube);
        }
Exemple #11
0
        protected internal override Texture2D Read(ContentReader reader, Texture2D existingInstance)
        {
            Texture2D texture = null;

            SurfaceFormat surfaceFormat = (SurfaceFormat)reader.ReadInt32();
            int           width         = reader.ReadInt32();
            int           height        = reader.ReadInt32();
            int           levelCount    = reader.ReadInt32();
            int           imageLength   = reader.ReadInt32();

            byte[] imageData = reader.ReadBytes(imageLength);

            switch (surfaceFormat)
            {
            case SurfaceFormat.Dxt1: imageData = DxtUtil.DecompressDxt1(imageData, width, height); break;

            case SurfaceFormat.Dxt3: imageData = DxtUtil.DecompressDxt3(imageData, width, height); break;
            }

            IntPtr imagePtr = IntPtr.Zero;


            // Changed to load the raw data
            // currentony only ARGB8888 is supported
            ESTexture2D esTexture = new ESTexture2D(imageData, surfaceFormat, width, height, new Size(width, height), All.Linear);

            texture = new Texture2D(reader.GraphicsDevice, new ESImage(esTexture));


            return(texture);
        }
Exemple #12
0
        protected internal override IndexBuffer Read(
            ContentReader input,
            IndexBuffer existingInstance
            )
        {
            IndexBuffer indexBuffer = existingInstance;
            bool        sixteenBits = input.ReadBoolean();
            int         dataSize    = input.ReadInt32();

            byte[] data = input.ReadBytes(dataSize);
            if (indexBuffer == null)
            {
                if (sixteenBits)
                {
                    indexBuffer = new IndexBuffer(
                        input.GraphicsDevice,
                        IndexElementSize.SixteenBits,
                        dataSize / 2,
                        BufferUsage.None
                        );
                }
                else
                {
                    indexBuffer = new IndexBuffer(
                        input.GraphicsDevice,
                        IndexElementSize.ThirtyTwoBits,
                        dataSize / 4,
                        BufferUsage.None
                        );
                }
            }

            indexBuffer.SetData(data);
            return(indexBuffer);
        }
Exemple #13
0
        protected internal override Effect Read(ContentReader input, Effect existingInstance)
        {
            int    count  = input.ReadInt32();
            Effect effect = new Effect(input.GraphicsDevice, input.ReadBytes(count));

            effect.Name = input.AssetName;
            return(effect);
        }
Exemple #14
0
        protected internal override SoundEffect Read(
            ContentReader input,
            SoundEffect existingInstance
            )
        {
            // Format block length
            uint formatLength = input.ReadUInt32();

            // WaveFormatEx data
            ushort wFormatTag      = input.ReadUInt16();
            ushort nChannels       = input.ReadUInt16();
            uint   nSamplesPerSec  = input.ReadUInt32();
            uint   nAvgBytesPerSec = input.ReadUInt32();
            ushort nBlockAlign     = input.ReadUInt16();
            ushort wBitsPerSample  = input.ReadUInt16();

            /* ushort cbSize =*/ input.ReadUInt16();

            // Seek past the rest of this crap (cannot seek though!)
            input.ReadBytes((int)(formatLength - 18));

            // Wavedata
            byte[] data = input.ReadBytes(input.ReadInt32());

            // Loop information
            int loopStart  = input.ReadInt32();
            int loopLength = input.ReadInt32();

            // Sound duration in milliseconds, unused
            input.ReadUInt32();

            return(new SoundEffect(
                       input.AssetName,
                       data,
                       0,
                       data.Length,
                       wFormatTag,
                       nChannels,
                       nSamplesPerSec,
                       nAvgBytesPerSec,
                       nBlockAlign,
                       wBitsPerSample,
                       loopStart,
                       loopLength
                       ));
        }
Exemple #15
0
        protected internal override VertexBuffer Read(ContentReader input, VertexBuffer existingInstance)
        {
            VertexDeclaration vertexDeclaration = input.ReadRawObject <VertexDeclaration>();
            int vertexCount = (int)input.ReadUInt32();

            byte[]       data         = input.ReadBytes(vertexCount * vertexDeclaration.VertexStride);
            VertexBuffer vertexBuffer = new VertexBuffer(input.GraphicsDevice, vertexDeclaration, vertexCount, BufferUsage.None);

            vertexBuffer.SetData <byte>(data);
            return(vertexBuffer);
        }
        protected internal override VertexBuffer Read(ContentReader input, VertexBuffer existingInstance)
        {
            var declaration = input.ReadExternalReference <VertexDeclaration>();
            var vertexCount = (int)input.ReadUInt32();
            var data        = input.ReadBytes(vertexCount * declaration.VertexStride);

            var buffer = new VertexBuffer(input.GraphicsDevice, declaration, vertexCount, BufferUsage.WriteOnly);

            buffer.SetData(data);
            return(buffer);
        }
Exemple #17
0
        protected internal override IndexBuffer Read(ContentReader input, IndexBuffer existingInstance)
        {
            bool flag  = input.ReadBoolean();
            int  count = input.ReadInt32();

            byte[]      data        = input.ReadBytes(count);
            IndexBuffer indexBuffer = new IndexBuffer(input.GraphicsDevice, flag ? IndexElementSize.SixteenBits : IndexElementSize.ThirtyTwoBits, count / (flag ? 2 : 4), BufferUsage.None);

            indexBuffer.SetData <byte>(data);
            return(indexBuffer);
        }
Exemple #18
0
        protected internal override Effect Read(
            ContentReader input,
            Effect existingInstance
            )
        {
            int    length = input.ReadInt32();
            Effect effect = new Effect(input.ReadBytes(length)
                                       );

            effect.Name = input.AssetName;
            return(effect);
        }
        protected internal override IndexBuffer Read(ContentReader input, IndexBuffer existingInstance)
        {
            var sixteenBits = input.ReadBoolean();
            var dataSize    = input.ReadInt32();
            var data        = input.ReadBytes(dataSize);

            var buffer = new IndexBuffer(input.GraphicsDevice,
                                         sixteenBits ? IndexElementSize.SixteenBits : IndexElementSize.ThirtyTwoBits,
                                         dataSize / (sixteenBits ? 2 : 4), BufferUsage.None);

            buffer.SetData(data);
            return(buffer);
        }
Exemple #20
0
        protected internal override SoundEffect Read(ContentReader input, SoundEffect existingInstance)
        {
            // XNB format for SoundEffect...
            //
            // Byte [format size]	Format	WAVEFORMATEX structure
            // UInt32	Data size
            // Byte [data size]	Data	Audio waveform data
            // Int32	Loop start	In bytes (start must be format block aligned)
            // Int32	Loop length	In bytes (length must be format block aligned)
            // Int32	Duration	In milliseconds

            // The header containss the WAVEFORMATEX header structure
            // defined as the following...
            //
            //  WORD  wFormatTag;       // byte[0]  +2
            //  WORD  nChannels;        // byte[2]  +2
            //  DWORD nSamplesPerSec;   // byte[4]  +4
            //  DWORD nAvgBytesPerSec;  // byte[8]  +4
            //  WORD  nBlockAlign;      // byte[12] +2
            //  WORD  wBitsPerSample;   // byte[14] +2
            //  WORD  cbSize;           // byte[16] +2
            //
            // We let the sound effect deal with parsing this based
            // on what format the audio data actually is.

            var headerSize = input.ReadInt32();
            var header     = input.ReadBytes(headerSize);

            // Read the audio data buffer.
            var dataSize = input.ReadInt32();
            var data     = ContentManager.ScratchBufferPool.Get(dataSize);

            input.Read(data, 0, dataSize);

            var loopStart  = input.ReadInt32();
            var loopLength = input.ReadInt32();
            var durationMs = input.ReadInt32();

            // Create the effect.
            var effect = new SoundEffect(header, data, dataSize, durationMs, loopStart, loopLength);

            // Store the original asset name for debugging later.
            effect.Name = input.AssetName;

            ContentManager.ScratchBufferPool.Return(data);

            return(effect);
        }
Exemple #21
0
        protected internal override Texture2D Read(ContentReader reader, Texture2D existingInstance)
        {
            Texture2D texture = null;

            SurfaceFormat surfaceFormat = (SurfaceFormat)reader.ReadInt32();
            int           width         = reader.ReadInt32();
            int           height        = reader.ReadInt32();

            /*int levelCount =*/ reader.ReadInt32();
            int imageLength = reader.ReadInt32();

            byte[] imageData = reader.ReadBytes(imageLength);

#if NO_DXT35
            System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch();
            sw2.Reset();
            sw2.Start();
            // GG TODO need to remove this manual decompression KTHX
            switch (surfaceFormat)
            {
            case SurfaceFormat.Dxt1: imageData = DxtUtil.DecompressDxt1(imageData, width, height); surfaceFormat = SurfaceFormat.Rgba32; break;

            case SurfaceFormat.Dxt3: imageData = DxtUtil.DecompressDxt3(imageData, width, height); surfaceFormat = SurfaceFormat.Rgba32; break;
            }
            if (sw2.ElapsedMilliseconds > 16)
            {
                GSGE.Debug.logMessage("Surface Format decompression took " + sw2.ElapsedMilliseconds);
            }
#endif

            unsafe
            {
                fixed(byte *pData = imageData)
                {
                    ESTexture2D esTexture = new ESTexture2D((IntPtr)pData,
                                                            imageData.Length,
                                                            surfaceFormat,
                                                            width, height,
                                                            new Size(width, height),
                                                            All.Linear);

                    texture = new Texture2D(new ESImage(esTexture));
                }
            }

            return(texture);
        }
Exemple #22
0
        protected internal override TextureCube Read(ContentReader reader, TextureCube existingInstance)
        {
            SurfaceFormat format      = (SurfaceFormat)reader.ReadInt32();
            int           size        = reader.ReadInt32();
            int           num1        = reader.ReadInt32();
            TextureCube   textureCube = new TextureCube(reader.GraphicsDevice, size, num1 > 1, format);

            for (int index = 0; index < 6; ++index)
            {
                for (int level = 0; level < num1; ++level)
                {
                    int    num2 = reader.ReadInt32();
                    byte[] data = reader.ReadBytes(num2);
                    textureCube.SetData <byte>((CubeMapFace)index, level, new Rectangle?(), data, 0, num2);
                }
            }
            return(textureCube);
        }
Exemple #23
0
        protected internal override Texture3D Read(ContentReader reader, Texture3D existingInstance)
        {
            SurfaceFormat format    = (SurfaceFormat)reader.ReadInt32();
            int           num1      = reader.ReadInt32();
            int           num2      = reader.ReadInt32();
            int           num3      = reader.ReadInt32();
            int           num4      = reader.ReadInt32();
            Texture3D     texture3D = new Texture3D(reader.GraphicsDevice, num1, num2, num3, num4 > 1, format);

            for (int level = 0; level < num4; ++level)
            {
                int    num5 = reader.ReadInt32();
                byte[] data = reader.ReadBytes(num5);
                texture3D.SetData <byte>(level, 0, 0, num1, num2, 0, num3, data, 0, num5);
                num1 = Math.Max(num1 >> 1, 1);
                num2 = Math.Max(num2 >> 1, 1);
                num3 = Math.Max(num3 >> 1, 1);
            }
            return(texture3D);
        }
		internal CpuParticleProcessorData(ContentReader reader)
		{
			byte[] runtimeAssemblyData = reader.ReadBytes(reader.ReadInt32());
			this.runtimeClassName = namespaceName + "." + reader.ReadString();
			this.assembly = System.Reflection.Assembly.Load(runtimeAssemblyData);
		}
Exemple #25
0
        protected internal override Texture2D Read(ContentReader reader, Texture2D existingInstance)
        {
            Texture2D texture = null;

            SurfaceFormat surfaceFormat;

            if (reader.version < 5)
            {
                SurfaceFormatLegacy legacyFormat = (SurfaceFormatLegacy)reader.ReadInt32();
                switch (legacyFormat)
                {
                case SurfaceFormatLegacy.Dxt1:
                    surfaceFormat = SurfaceFormat.Dxt1;
                    break;

                case SurfaceFormatLegacy.Dxt3:
                    surfaceFormat = SurfaceFormat.Dxt3;
                    break;

                case SurfaceFormatLegacy.Dxt5:
                    surfaceFormat = SurfaceFormat.Dxt5;
                    break;

                case SurfaceFormatLegacy.Color:
                    surfaceFormat = SurfaceFormat.Color;
                    break;

                default:
                    throw new NotSupportedException("Unsupported legacy surface format.");
                }
            }
            else
            {
                surfaceFormat = (SurfaceFormat)reader.ReadInt32();
            }

            int width            = (reader.ReadInt32());
            int height           = (reader.ReadInt32());
            int levelCount       = (reader.ReadInt32());
            int levelCountOutput = levelCount;

            // If the system does not fully support Power of Two textures,
            // skip any mip maps supplied with any non PoT textures.
            if (levelCount > 1 && !reader.GraphicsDevice.GraphicsCapabilities.SupportsNonPowerOfTwo &&
                (!MathHelper.IsPowerOfTwo(width) || !MathHelper.IsPowerOfTwo(height)))
            {
                levelCountOutput = 1;
                System.Diagnostics.Debug.WriteLine(
                    "Device does not support non Power of Two textures. Skipping mipmaps.");
            }

            SurfaceFormat convertedFormat = surfaceFormat;

            switch (surfaceFormat)
            {
#if IOS
            // At the moment. If a DXT Texture comes in on iOS, it's really a PVR compressed
            // texture. We need to use this hack until the content pipeline is implemented.
            // For now DXT5 means we're using 4bpp PVRCompression and DXT3 means 2bpp. Look at
            // PvrtcBitmapContent.cs for more information.:
            case SurfaceFormat.Dxt3:
                convertedFormat = SurfaceFormat.RgbaPvrtc2Bpp;
                break;

            case SurfaceFormat.Dxt5:
                convertedFormat = SurfaceFormat.RgbaPvrtc4Bpp;
                break;
#else
            case SurfaceFormat.Dxt1:
            case SurfaceFormat.Dxt1a:
                if (!reader.GraphicsDevice.GraphicsCapabilities.SupportsDxt1)
                {
                    convertedFormat = SurfaceFormat.Color;
                }
                break;

            case SurfaceFormat.Dxt3:
            case SurfaceFormat.Dxt5:
                if (!reader.GraphicsDevice.GraphicsCapabilities.SupportsS3tc)
                {
                    convertedFormat = SurfaceFormat.Color;
                }
                break;
#endif
            case SurfaceFormat.NormalizedByte4:
                convertedFormat = SurfaceFormat.Color;
                break;
            }

            if (existingInstance == null)
            {
                texture = new Texture2D(reader.GraphicsDevice, width, height, levelCountOutput > 1, convertedFormat);
            }
            else
            {
                texture = existingInstance;
            }

            for (int level = 0; level < levelCount; level++)
            {
                int    levelDataSizeInBytes = (reader.ReadInt32());
                byte[] levelData            = reader.ReadBytes(levelDataSizeInBytes);
                int    levelWidth           = width >> level;
                int    levelHeight          = height >> level;

                if (level >= levelCountOutput)
                {
                    continue;
                }

                //Convert the image data if required
                switch (surfaceFormat)
                {
#if !IOS
                case SurfaceFormat.Dxt1:
                case SurfaceFormat.Dxt1a:
                    if (!reader.GraphicsDevice.GraphicsCapabilities.SupportsDxt1)
                    {
                        levelData = DxtUtil.DecompressDxt1(levelData, levelWidth, levelHeight);
                    }
                    break;

                case SurfaceFormat.Dxt3:
                    if (!reader.GraphicsDevice.GraphicsCapabilities.SupportsS3tc)
                    {
                        levelData = DxtUtil.DecompressDxt3(levelData, levelWidth, levelHeight);
                    }
                    break;

                case SurfaceFormat.Dxt5:
                    if (!reader.GraphicsDevice.GraphicsCapabilities.SupportsS3tc)
                    {
                        levelData = DxtUtil.DecompressDxt5(levelData, levelWidth, levelHeight);
                    }
                    break;
#endif
                case SurfaceFormat.Bgr565:
                {
                    /*
                     * // BGR -> BGR
                     * int offset = 0;
                     * for (int y = 0; y < levelHeight; y++)
                     * {
                     *      for (int x = 0; x < levelWidth; x++)
                     *      {
                     *              ushort pixel = BitConverter.ToUInt16(levelData, offset);
                     *              pixel = (ushort)(((pixel & 0x0FFF) << 4) | ((pixel & 0xF000) >> 12));
                     *              levelData[offset] = (byte)(pixel);
                     *              levelData[offset + 1] = (byte)(pixel >> 8);
                     *              offset += 2;
                     *      }
                     * }
                     */
                }
                break;

                case SurfaceFormat.Bgra5551:
                {
#if OPENGL
                    // Shift the channels to suit OPENGL
                    int offset = 0;
                    for (int y = 0; y < levelHeight; y++)
                    {
                        for (int x = 0; x < levelWidth; x++)
                        {
                            ushort pixel = BitConverter.ToUInt16(levelData, offset);
                            pixel                 = (ushort)(((pixel & 0x7FFF) << 1) | ((pixel & 0x8000) >> 15));
                            levelData[offset]     = (byte)(pixel);
                            levelData[offset + 1] = (byte)(pixel >> 8);
                            offset               += 2;
                        }
                    }
#endif
                }
                break;

                case SurfaceFormat.Bgra4444:
                {
#if OPENGL
                    // Shift the channels to suit OPENGL
                    int offset = 0;
                    for (int y = 0; y < levelHeight; y++)
                    {
                        for (int x = 0; x < levelWidth; x++)
                        {
                            ushort pixel = BitConverter.ToUInt16(levelData, offset);
                            pixel                 = (ushort)(((pixel & 0x0FFF) << 4) | ((pixel & 0xF000) >> 12));
                            levelData[offset]     = (byte)(pixel);
                            levelData[offset + 1] = (byte)(pixel >> 8);
                            offset               += 2;
                        }
                    }
#endif
                }
                break;

                case SurfaceFormat.NormalizedByte4:
                {
                    int bytesPerPixel = surfaceFormat.GetSize();
                    int pitch         = levelWidth * bytesPerPixel;
                    for (int y = 0; y < levelHeight; y++)
                    {
                        for (int x = 0; x < levelWidth; x++)
                        {
                            int color = BitConverter.ToInt32(levelData, y * pitch + x * bytesPerPixel);
                            levelData[y * pitch + x * 4]     = (byte)(((color >> 16) & 0xff));                                       //R:=W
                            levelData[y * pitch + x * 4 + 1] = (byte)(((color >> 8) & 0xff));                                        //G:=V
                            levelData[y * pitch + x * 4 + 2] = (byte)(((color) & 0xff));                                             //B:=U
                            levelData[y * pitch + x * 4 + 3] = (byte)(((color >> 24) & 0xff));                                       //A:=Q
                        }
                    }
                }
                break;
                }

                texture.SetData(level, null, levelData, 0, levelData.Length);
            }

            return(texture);
        }
Exemple #26
0
        protected internal override Texture2D Read(
            ContentReader reader,
            Texture2D existingInstance
            )
        {
            Texture2D texture = null;

            SurfaceFormat surfaceFormat;

            if (reader.version < 5)
            {
                /* These integer values are based on the enum values
                 * from previous XNA versions.
                 * -flibit
                 */
                int legacyFormat = reader.ReadInt32();
                if (legacyFormat == 1)
                {
                    surfaceFormat = SurfaceFormat.ColorBgraEXT;
                }
                else if (legacyFormat == 28)
                {
                    surfaceFormat = SurfaceFormat.Dxt1;
                }
                else if (legacyFormat == 30)
                {
                    surfaceFormat = SurfaceFormat.Dxt3;
                }
                else if (legacyFormat == 32)
                {
                    surfaceFormat = SurfaceFormat.Dxt5;
                }
                else
                {
                    throw new NotSupportedException(
                              "Unsupported legacy surface format."
                              );
                }
            }
            else
            {
                surfaceFormat = (SurfaceFormat)reader.ReadInt32();
            }
            int width            = reader.ReadInt32();
            int height           = reader.ReadInt32();
            int levelCount       = reader.ReadInt32();
            int levelCountOutput = levelCount;

            GraphicsDevice device = reader.ContentManager.GetGraphicsDevice();

            // Check to see if we need to convert the surface data
            SurfaceFormat convertedFormat = surfaceFormat;

            if (surfaceFormat == SurfaceFormat.Dxt1 &&
                !device.GLDevice.SupportsDxt1)
            {
                convertedFormat = SurfaceFormat.Color;
            }
            else if ((surfaceFormat == SurfaceFormat.Dxt3 ||
                      surfaceFormat == SurfaceFormat.Dxt5) &&
                     !device.GLDevice.SupportsS3tc)
            {
                convertedFormat = SurfaceFormat.Color;
            }

            // Check for duplicate instances
            if (existingInstance == null)
            {
                texture = new Texture2D(
                    device,
                    width,
                    height,
                    levelCountOutput > 1,
                    convertedFormat
                    );
            }
            else
            {
                texture = existingInstance;
            }

            for (int level = 0; level < levelCount; level += 1)
            {
                int    levelDataSizeInBytes = reader.ReadInt32();
                byte[] levelData            = null;      // Don't assign this quite yet...
                int    levelWidth           = width >> level;
                int    levelHeight          = height >> level;
                if (level >= levelCountOutput)
                {
                    continue;
                }

                // Swap the image data if required.
                if (reader.platform == 'x')
                {
                    if (surfaceFormat == SurfaceFormat.Color ||
                        surfaceFormat == SurfaceFormat.ColorBgraEXT)
                    {
                        levelData = X360TexUtil.SwapColor(
                            reader.ReadBytes(levelDataSizeInBytes)
                            );
                        levelDataSizeInBytes = levelData.Length;
                    }
                    else if (surfaceFormat == SurfaceFormat.Dxt1)
                    {
                        levelData = X360TexUtil.SwapDxt1(
                            reader.ReadBytes(levelDataSizeInBytes),
                            levelWidth,
                            levelHeight
                            );
                        levelDataSizeInBytes = levelData.Length;
                    }
                    else if (surfaceFormat == SurfaceFormat.Dxt3)
                    {
                        levelData = X360TexUtil.SwapDxt3(
                            reader.ReadBytes(levelDataSizeInBytes),
                            levelWidth,
                            levelHeight
                            );
                        levelDataSizeInBytes = levelData.Length;
                    }
                    else if (surfaceFormat == SurfaceFormat.Dxt5)
                    {
                        levelData = X360TexUtil.SwapDxt5(
                            reader.ReadBytes(levelDataSizeInBytes),
                            levelWidth,
                            levelHeight
                            );
                        levelDataSizeInBytes = levelData.Length;
                    }
                }

                // Convert the image data if required
                if (convertedFormat != surfaceFormat)
                {
                    // May already be read in by 'x' conversion
                    if (levelData == null)
                    {
                        levelData = reader.ReadBytes(levelDataSizeInBytes);
                    }
                    if (surfaceFormat == SurfaceFormat.Dxt1)
                    {
                        levelData = DxtUtil.DecompressDxt1(
                            levelData,
                            levelWidth,
                            levelHeight
                            );
                    }
                    else if (surfaceFormat == SurfaceFormat.Dxt3)
                    {
                        levelData = DxtUtil.DecompressDxt3(
                            levelData,
                            levelWidth,
                            levelHeight
                            );
                    }
                    else if (surfaceFormat == SurfaceFormat.Dxt5)
                    {
                        levelData = DxtUtil.DecompressDxt5(
                            levelData,
                            levelWidth,
                            levelHeight
                            );
                    }
                    levelDataSizeInBytes = levelData.Length;
                }

                int levelDataByteOffset = 0;
                if (levelData == null)
                {
                    if (reader.BaseStream is MemoryStream &&
                        ((MemoryStream)reader.BaseStream).TryGetBuffer(out levelData))
                    {
                        /* Ideally, we didn't have to perform any conversion or
                         * unnecessary reading. Just throw the buffer directly
                         * into SetData, skipping a redundant byte[] copy.
                         */
                        levelDataByteOffset = (int)reader.BaseStream.Seek(0, SeekOrigin.Current);
                        reader.BaseStream.Seek(
                            levelDataSizeInBytes,
                            SeekOrigin.Current
                            );
                    }
                    else
                    {
                        /* If we don't have to perform any conversion and
                         * the ContentReader is not backed by a MemoryStream
                         * with a public buffer, we have to read the data in.
                         */
                        levelData = reader.ReadBytes(levelDataSizeInBytes);
                    }
                }
                texture.SetData(
                    level,
                    null,
                    levelData,
                    levelDataByteOffset,
                    levelDataSizeInBytes
                    );
            }

            return(texture);
        }
Exemple #27
0
        protected internal override Texture2D Read(ContentReader reader, Texture2D existingInstance)
        {
            SurfaceFormat surfaceFormat;

            if (reader.version < 5)
            {
                switch ((SurfaceFormat_Legacy)reader.ReadInt32())
                {
                case SurfaceFormat_Legacy.Color:
                    surfaceFormat = SurfaceFormat.Color;
                    break;

                case SurfaceFormat_Legacy.Dxt1:
                    surfaceFormat = SurfaceFormat.Dxt1;
                    break;

                case SurfaceFormat_Legacy.Dxt3:
                    surfaceFormat = SurfaceFormat.Dxt3;
                    break;

                case SurfaceFormat_Legacy.Dxt5:
                    surfaceFormat = SurfaceFormat.Dxt5;
                    break;

                default:
                    throw new NotImplementedException();
                }
            }
            else
            {
                surfaceFormat = (SurfaceFormat)reader.ReadInt32();
            }
            int width1  = reader.ReadInt32();
            int height1 = reader.ReadInt32();
            int num1    = reader.ReadInt32();
            int num2    = num1;

            if (num1 > 1 && !GraphicsCapabilities.NonPowerOfTwo && (!MathHelper.IsPowerOfTwo(width1) || !MathHelper.IsPowerOfTwo(height1)))
            {
                num2 = 1;
            }
            SurfaceFormat format = surfaceFormat;

            if (surfaceFormat == SurfaceFormat.NormalizedByte4)
            {
                format = SurfaceFormat.Color;
            }
            if (!GraphicsExtensions.UseDxtCompression)
            {
                switch (surfaceFormat)
                {
                case SurfaceFormat.Dxt1:
                case SurfaceFormat.Dxt3:
                case SurfaceFormat.Dxt5:
                    format = SurfaceFormat.Color;
                    break;
                }
            }
            Texture2D texture2D = existingInstance != null ? existingInstance : new Texture2D(reader.GraphicsDevice, width1, height1, num2 > 1, format);

            for (int level = 0; level < num1; ++level)
            {
                int    count    = reader.ReadInt32();
                byte[] numArray = reader.ReadBytes(count);
                int    width2   = width1 >> level;
                int    height2  = height1 >> level;
                if (level < num2)
                {
                    if (!GraphicsExtensions.UseDxtCompression)
                    {
                        switch (surfaceFormat)
                        {
                        case SurfaceFormat.Dxt1:
                            numArray = DxtUtil.DecompressDxt1(numArray, width2, height2);
                            break;

                        case SurfaceFormat.Dxt3:
                            numArray = DxtUtil.DecompressDxt3(numArray, width2, height2);
                            break;

                        case SurfaceFormat.Dxt5:
                            numArray = DxtUtil.DecompressDxt5(numArray, width2, height2);
                            break;
                        }
                    }
                    switch (surfaceFormat)
                    {
                    case SurfaceFormat.Bgra5551:
                        int startIndex1 = 0;
                        for (int index1 = 0; index1 < height2; ++index1)
                        {
                            for (int index2 = 0; index2 < width2; ++index2)
                            {
                                ushort num3 = BitConverter.ToUInt16(numArray, startIndex1);
                                ushort num4 = (ushort)(((int)num3 & (int)short.MaxValue) << 1 | ((int)num3 & 32768) >> 15);
                                numArray[startIndex1]     = (byte)num4;
                                numArray[startIndex1 + 1] = (byte)((uint)num4 >> 8);
                                startIndex1 += 2;
                            }
                        }
                        break;

                    case SurfaceFormat.Bgra4444:
                        int startIndex2 = 0;
                        for (int index1 = 0; index1 < height2; ++index1)
                        {
                            for (int index2 = 0; index2 < width2; ++index2)
                            {
                                ushort num3 = BitConverter.ToUInt16(numArray, startIndex2);
                                ushort num4 = (ushort)(((int)num3 & 4095) << 4 | ((int)num3 & 61440) >> 12);
                                numArray[startIndex2]     = (byte)num4;
                                numArray[startIndex2 + 1] = (byte)((uint)num4 >> 8);
                                startIndex2 += 2;
                            }
                        }
                        break;

                    case SurfaceFormat.NormalizedByte4:
                        int num5 = GraphicsExtensions.Size(surfaceFormat);
                        int num6 = width2 * num5;
                        for (int index1 = 0; index1 < height2; ++index1)
                        {
                            for (int index2 = 0; index2 < width2; ++index2)
                            {
                                int num3 = BitConverter.ToInt32(numArray, index1 * num6 + index2 * num5);
                                numArray[index1 * num6 + index2 * 4]     = (byte)(num3 >> 16 & (int)byte.MaxValue);
                                numArray[index1 * num6 + index2 * 4 + 1] = (byte)(num3 >> 8 & (int)byte.MaxValue);
                                numArray[index1 * num6 + index2 * 4 + 2] = (byte)(num3 & (int)byte.MaxValue);
                                numArray[index1 * num6 + index2 * 4 + 3] = (byte)(num3 >> 24 & (int)byte.MaxValue);
                            }
                        }
                        break;
                    }
                    texture2D.SetData <byte>(level, new Rectangle?(), numArray, 0, numArray.Length);
                }
            }
            return(texture2D);
        }
        protected internal override Texture2D Read(ContentReader reader, Texture2D existingInstance)
        {
            Texture2D texture = null;

            SurfaceFormat surfaceFormat;

            if (reader.version < 5)
            {
                SurfaceFormatLegacy legacyFormat = (SurfaceFormatLegacy)reader.ReadInt32();
                switch (legacyFormat)
                {
                case SurfaceFormatLegacy.Dxt1:
                    surfaceFormat = SurfaceFormat.Dxt1;
                    break;

                case SurfaceFormatLegacy.Dxt3:
                    surfaceFormat = SurfaceFormat.Dxt3;
                    break;

                case SurfaceFormatLegacy.Dxt5:
                    surfaceFormat = SurfaceFormat.Dxt5;
                    break;

                case SurfaceFormatLegacy.Color:
                    surfaceFormat = SurfaceFormat.Color;
                    break;

                default:
                    throw new NotSupportedException("Unsupported legacy surface format.");
                }
            }
            else
            {
                surfaceFormat = (SurfaceFormat)reader.ReadInt32();
            }

            int width            = (reader.ReadInt32());
            int height           = (reader.ReadInt32());
            int levelCount       = (reader.ReadInt32());
            int levelCountOutput = levelCount;

            SurfaceFormat convertedFormat = surfaceFormat;

            switch (surfaceFormat)
            {
            case SurfaceFormat.Dxt1:
            case SurfaceFormat.Dxt1a:
                convertedFormat = SurfaceFormat.Color;
                break;

            case SurfaceFormat.Dxt3:
            case SurfaceFormat.Dxt5:
                convertedFormat = SurfaceFormat.Color;
                break;

            case SurfaceFormat.NormalizedByte4:
                convertedFormat = SurfaceFormat.Color;
                break;
            }

            UnityEngine.Texture2D unityTexture = new UnityEngine.Texture2D(width, height, XnaToUnity.TextureFormat(convertedFormat), levelCountOutput > 1);

            for (int level = 0; level < levelCount; level++)
            {
                int    levelDataSizeInBytes = (reader.ReadInt32());
                byte[] levelData            = reader.ReadBytes(levelDataSizeInBytes);
                int    levelWidth           = width >> level;
                int    levelHeight          = height >> level;

                if (level >= levelCountOutput)
                {
                    continue;
                }

                //Convert the image data if required
                switch (surfaceFormat)
                {
                case SurfaceFormat.Dxt1:
                case SurfaceFormat.Dxt1a:
                    levelData = DxtUtil.DecompressDxt1(levelData, levelWidth, levelHeight);
                    break;

                case SurfaceFormat.Dxt3:
                    levelData = DxtUtil.DecompressDxt3(levelData, levelWidth, levelHeight);
                    break;

                case SurfaceFormat.Dxt5:
                    levelData = DxtUtil.DecompressDxt5(levelData, levelWidth, levelHeight);
                    break;
                }

                // un-premultiply alpha (instead do it in the shader)
                for (int i = 0; i < levelData.Length; i += 4)
                {
                    float r = levelData[i + 0] / 255.0f;
                    float g = levelData[i + 1] / 255.0f;
                    float b = levelData[i + 2] / 255.0f;
                    float a = levelData[i + 3] / 255.0f;

                    levelData[i + 0] = (byte)(r / a * 255.0f);
                    levelData[i + 1] = (byte)(g / a * 255.0f);
                    levelData[i + 2] = (byte)(b / a * 255.0f);

                    //levelData[i + 0] = 0;
                    //levelData[i + 1] = 255;
                    //levelData[i + 2] = 0;
                    //levelData[i + 3] = 255;
                }

                // swap rows because unity textures are laid out bottom-top instead of top-bottom
                int    rowSize = width * 4;
                byte[] temp    = new byte[rowSize];
                for (int i = 0; i < levelData.Length / 2; i += rowSize)
                {
                    for (int j = 0; j < rowSize; j++)
                    {
                        temp[j] = levelData[i + j];
                    }
                    int p = levelData.Length - (i + rowSize);
                    for (int j = 0; j < rowSize; j++)
                    {
                        levelData[i + j] = levelData[p + j];
                    }
                    for (int j = 0; j < rowSize; j++)
                    {
                        levelData[p + j] = temp[j];
                    }
                }

                UnityEngine.Color[] unityColors = new UnityEngine.Color[levelData.Length * 4];
                unityTexture.SetPixels(XnaToUnity.Color(levelData, ref unityColors), level);
                unityTexture.Apply();
                texture = new Texture2D(unityTexture);
            }
            return(texture);
        }
Exemple #29
0
        protected internal override SoundEffect Read(
            ContentReader input,
            SoundEffect existingInstance
            )
        {
            /* Swap endian - this is one of the very few places requiring this!
             * Note: This only affects the fmt chunk that's glued into the file.
             */
            bool se = input.platform == 'x';

            // Format block length
            uint formatLength = input.ReadUInt32();

            // WaveFormatEx data
            ushort wFormatTag      = Swap(se, input.ReadUInt16());
            ushort nChannels       = Swap(se, input.ReadUInt16());
            uint   nSamplesPerSec  = Swap(se, input.ReadUInt32());
            uint   nAvgBytesPerSec = Swap(se, input.ReadUInt32());
            ushort nBlockAlign     = Swap(se, input.ReadUInt16());
            ushort wBitsPerSample  = Swap(se, input.ReadUInt16());

            byte[] extra = null;
            if (formatLength > 16)
            {
                ushort cbSize = Swap(se, input.ReadUInt16());

                if (wFormatTag == 0x166 && cbSize == 34)
                {
                    // XMA2 has got some nice extra crap.
                    extra = new byte[34];
                    using (MemoryStream extraStream = new MemoryStream(extra))
                        using (BinaryWriter extraWriter = new BinaryWriter(extraStream))
                        {
                            // See FAudio.FAudioXMA2WaveFormatEx for the layout.
                            extraWriter.Write(Swap(se, input.ReadUInt16()));
                            extraWriter.Write(Swap(se, input.ReadUInt32()));
                            extraWriter.Write(Swap(se, input.ReadUInt32()));
                            extraWriter.Write(Swap(se, input.ReadUInt32()));
                            extraWriter.Write(Swap(se, input.ReadUInt32()));
                            extraWriter.Write(Swap(se, input.ReadUInt32()));
                            extraWriter.Write(Swap(se, input.ReadUInt32()));
                            extraWriter.Write(Swap(se, input.ReadUInt32()));
                            extraWriter.Write(input.ReadByte());
                            extraWriter.Write(input.ReadByte());
                            extraWriter.Write(Swap(se, input.ReadUInt16()));
                        }
                    // Is there any crap that needs skipping? Eh whatever.
                    input.ReadBytes((int)(formatLength - 18 - 34));
                }
                else
                {
                    // Seek past the rest of this crap (cannot seek though!)
                    input.ReadBytes((int)(formatLength - 18));
                }
            }

            // Wavedata
            byte[] data = input.ReadBytes(input.ReadInt32());

            // Loop information
            int loopStart  = input.ReadInt32();
            int loopLength = input.ReadInt32();

            // Sound duration in milliseconds, unused
            input.ReadUInt32();

            return(new SoundEffect(
                       input.AssetName,
                       data,
                       0,
                       data.Length,
                       extra,
                       wFormatTag,
                       nChannels,
                       nSamplesPerSec,
                       nAvgBytesPerSec,
                       nBlockAlign,
                       wBitsPerSample,
                       loopStart,
                       loopLength
                       ));
        }
Exemple #30
0
        protected internal override Texture2D Read(
            ContentReader reader,
            Texture2D existingInstance
            )
        {
            Texture2D texture = null;

            SurfaceFormat surfaceFormat;

            if (reader.version < 5)
            {
                SurfaceFormat_Legacy legacyFormat =
                    (SurfaceFormat_Legacy)reader.ReadInt32();
                switch (legacyFormat)
                {
                case SurfaceFormat_Legacy.Dxt1:
                    surfaceFormat = SurfaceFormat.Dxt1;
                    break;

                case SurfaceFormat_Legacy.Dxt3:
                    surfaceFormat = SurfaceFormat.Dxt3;
                    break;

                case SurfaceFormat_Legacy.Dxt5:
                    surfaceFormat = SurfaceFormat.Dxt5;
                    break;

                case SurfaceFormat_Legacy.Color:
                    surfaceFormat = SurfaceFormat.Color;
                    break;

                default:
                    throw new NotSupportedException(
                              "Unsupported legacy surface format."
                              );
                }
            }
            else
            {
                surfaceFormat = (SurfaceFormat)reader.ReadInt32();
            }
            int           width            = reader.ReadInt32();
            int           height           = reader.ReadInt32();
            int           levelCount       = reader.ReadInt32();
            int           levelCountOutput = levelCount;
            SurfaceFormat convertedFormat  = surfaceFormat;

            switch (surfaceFormat)
            {
            case SurfaceFormat.Dxt1:
                if (!OpenGLDevice.Instance.SupportsDxt1)
                {
                    convertedFormat = SurfaceFormat.Color;
                }
                break;

            case SurfaceFormat.Dxt3:
            case SurfaceFormat.Dxt5:
                if (!OpenGLDevice.Instance.SupportsS3tc)
                {
                    convertedFormat = SurfaceFormat.Color;
                }
                break;

            case SurfaceFormat.NormalizedByte4:
                convertedFormat = SurfaceFormat.Color;
                break;
            }
            if (existingInstance == null)
            {
                texture = new Texture2D(
                    reader.GraphicsDevice,
                    width,
                    height,
                    levelCountOutput > 1,
                    convertedFormat
                    );
            }
            else
            {
                texture = existingInstance;
            }

            for (int level = 0; level < levelCount; level += 1)
            {
                int    levelDataSizeInBytes = (reader.ReadInt32());
                byte[] levelData            = null;      // Don't assign this quite yet...
                int    levelWidth           = width >> level;
                int    levelHeight          = height >> level;
                if (level >= levelCountOutput)
                {
                    continue;
                }
                // Convert the image data if required
                switch (surfaceFormat)
                {
                case SurfaceFormat.Dxt1:
                    if (!OpenGLDevice.Instance.SupportsDxt1)
                    {
                        levelData = reader.ReadBytes(levelDataSizeInBytes);
                        levelData = DxtUtil.DecompressDxt1(
                            levelData,
                            levelWidth,
                            levelHeight
                            );
                    }
                    break;

                case SurfaceFormat.Dxt3:
                    if (!OpenGLDevice.Instance.SupportsS3tc)
                    {
                        levelData = reader.ReadBytes(levelDataSizeInBytes);
                        levelData = DxtUtil.DecompressDxt3(
                            levelData,
                            levelWidth,
                            levelHeight
                            );
                    }
                    break;

                case SurfaceFormat.Dxt5:
                    if (!OpenGLDevice.Instance.SupportsS3tc)
                    {
                        levelData = reader.ReadBytes(levelDataSizeInBytes);
                        levelData = DxtUtil.DecompressDxt5(
                            levelData,
                            levelWidth,
                            levelHeight
                            );
                    }
                    break;

                case SurfaceFormat.Bgr565:
                {
                    levelData = reader.ReadBytes(levelDataSizeInBytes);
                }
                break;

                case SurfaceFormat.Bgra5551:
                {
                    levelData = reader.ReadBytes(levelDataSizeInBytes);
                    // Shift the channels to suit OPENGL
                    int offset = 0;
                    for (int y = 0; y < levelHeight; y += 1)
                    {
                        for (int x = 0; x < levelWidth; x += 1)
                        {
                            ushort pixel = BitConverter.ToUInt16(
                                levelData,
                                offset
                                );
                            pixel = (ushort)(
                                ((pixel & 0x7FFF) << 1) |
                                ((pixel & 0x8000) >> 15)
                                );
                            levelData[offset] =
                                (byte)(pixel);
                            levelData[offset + 1] =
                                (byte)(pixel >> 8);
                            offset += 2;
                        }
                    }
                }
                break;

                case SurfaceFormat.Bgra4444:
                {
                    levelData = reader.ReadBytes(levelDataSizeInBytes);
                    // Shift the channels to suit OPENGL
                    int offset = 0;
                    for (int y = 0; y < levelHeight; y += 1)
                    {
                        for (int x = 0; x < levelWidth; x += 1)
                        {
                            ushort pixel = BitConverter.ToUInt16(
                                levelData,
                                offset
                                );
                            pixel = (ushort)(
                                ((pixel & 0x0FFF) << 4) |
                                ((pixel & 0xF000) >> 12)
                                );
                            levelData[offset] =
                                (byte)(pixel);
                            levelData[offset + 1] =
                                (byte)(pixel >> 8);
                            offset += 2;
                        }
                    }
                }
                break;

                case SurfaceFormat.NormalizedByte4:
                {
                    levelData = reader.ReadBytes(levelDataSizeInBytes);
                    int bytesPerPixel = 4;                             // According to Texture.GetFormatSize()
                    int pitch         = levelWidth * bytesPerPixel;
                    for (int y = 0; y < levelHeight; y += 1)
                    {
                        for (int x = 0; x < levelWidth; x += 1)
                        {
                            int color = BitConverter.ToInt32(
                                levelData,
                                y * pitch + x * bytesPerPixel
                                );
                            // R:=W
                            levelData[y * pitch + x * 4] =
                                (byte)(((color >> 16) & 0xff));
                            // G:=V
                            levelData[y * pitch + x * 4 + 1] =
                                (byte)(((color >> 8) & 0xff));
                            // B:=U
                            levelData[y * pitch + x * 4 + 2] =
                                (byte)(((color) & 0xff));
                            // A:=Q
                            levelData[y * pitch + x * 4 + 3] =
                                (byte)(((color >> 24) & 0xff));
                        }
                    }
                }
                break;
                }

                if (levelData == null &&
                    reader.BaseStream.GetType() != typeof(System.IO.MemoryStream))
                {
                    /* If the ContentReader is not backed by a
                     * MemoryStream, we have to read the data in.
                     */
                    levelData = reader.ReadBytes(levelDataSizeInBytes);
                }
                if (levelData != null)
                {
                    /* If we had to convert the data, or get the data from a
                     * non-MemoryStream, we set the data with our levelData
                     * reference.
                     */
                    texture.SetData(level, null, levelData, 0, levelData.Length);
                }
                else
                {
                    /* Ideally, we didn't have to perform any conversion or
                     * unnecessary reading. Just throw the buffer directly
                     * into SetData, skipping a redundant byte[] copy.
                     */
                    texture.SetData <byte>(
                        level,
                        null,
                        (((System.IO.MemoryStream)(reader.BaseStream)).GetBuffer()),
                        (int)reader.BaseStream.Position,
                        levelDataSizeInBytes
                        );
                    reader.BaseStream.Seek(
                        levelDataSizeInBytes,
                        System.IO.SeekOrigin.Current
                        );
                }
            }

            return(texture);
        }
Exemple #31
0
        protected internal override Texture2D Read(
            ContentReader reader,
            Texture2D existingInstance
            )
        {
            Texture2D texture = null;

            SurfaceFormat surfaceFormat;

            if (reader.version < 5)
            {
                /* These integer values are based on the enum values
                 * from previous XNA versions.
                 * -flibit
                 */
                int legacyFormat = reader.ReadInt32();
                if (legacyFormat == 1)
                {
                    surfaceFormat = SurfaceFormat.ColorBgraEXT;
                }
                else if (legacyFormat == 28)
                {
                    surfaceFormat = SurfaceFormat.Dxt1;
                }
                else if (legacyFormat == 30)
                {
                    surfaceFormat = SurfaceFormat.Dxt3;
                }
                else if (legacyFormat == 32)
                {
                    surfaceFormat = SurfaceFormat.Dxt5;
                }
                else
                {
                    throw new NotSupportedException(
                              "Unsupported legacy surface format."
                              );
                }
            }
            else
            {
                surfaceFormat = (SurfaceFormat)reader.ReadInt32();
            }
            int width            = reader.ReadInt32();
            int height           = reader.ReadInt32();
            int levelCount       = reader.ReadInt32();
            int levelCountOutput = levelCount;

            // Check to see if we need to convert the surface data
            SurfaceFormat convertedFormat = surfaceFormat;

            if (surfaceFormat == SurfaceFormat.Dxt1 &&
                !reader.GraphicsDevice.GLDevice.SupportsDxt1)
            {
                convertedFormat = SurfaceFormat.Color;
            }
            else if ((surfaceFormat == SurfaceFormat.Dxt3 ||
                      surfaceFormat == SurfaceFormat.Dxt5) &&
                     !reader.GraphicsDevice.GLDevice.SupportsS3tc)
            {
                convertedFormat = SurfaceFormat.Color;
            }

            // Check for duplicate instances
            if (existingInstance == null)
            {
                texture = new Texture2D(
                    reader.GraphicsDevice,
                    width,
                    height,
                    levelCountOutput > 1,
                    convertedFormat
                    );
            }
            else
            {
                texture = existingInstance;
            }

            for (int level = 0; level < levelCount; level += 1)
            {
                int    levelDataSizeInBytes = reader.ReadInt32();
                byte[] levelData            = null;      // Don't assign this quite yet...
                int    levelWidth           = width >> level;
                int    levelHeight          = height >> level;
                if (level >= levelCountOutput)
                {
                    continue;
                }

                // Swap the image data if required.
                if (reader.platform == 'x')
                {
                    if (surfaceFormat == SurfaceFormat.Color ||
                        surfaceFormat == SurfaceFormat.ColorBgraEXT)
                    {
                        if (levelData == null)
                        {
                            levelData = reader.ReadBytes(levelDataSizeInBytes);
                        }
                        levelData = X360TexUtil.SwapColor(
                            levelData
                            );
                    }
                    else if (surfaceFormat == SurfaceFormat.Dxt1)
                    {
                        if (levelData == null)
                        {
                            levelData = reader.ReadBytes(levelDataSizeInBytes);
                        }
                        levelData = X360TexUtil.SwapDxt1(
                            levelData,
                            levelWidth,
                            levelHeight
                            );
                    }
                    else if (surfaceFormat == SurfaceFormat.Dxt3)
                    {
                        if (levelData == null)
                        {
                            levelData = reader.ReadBytes(levelDataSizeInBytes);
                        }
                        levelData = X360TexUtil.SwapDxt3(
                            levelData,
                            levelWidth,
                            levelHeight
                            );
                    }
                    else if (surfaceFormat == SurfaceFormat.Dxt5)
                    {
                        if (levelData == null)
                        {
                            levelData = reader.ReadBytes(levelDataSizeInBytes);
                        }
                        levelData = X360TexUtil.SwapDxt5(
                            levelData,
                            levelWidth,
                            levelHeight
                            );
                    }
                }

                // Convert the image data if required
                if (surfaceFormat == SurfaceFormat.Dxt1 &&
                    !reader.GraphicsDevice.GLDevice.SupportsDxt1)
                {
                    if (levelData == null)
                    {
                        levelData = reader.ReadBytes(levelDataSizeInBytes);
                    }
                    levelData = DxtUtil.DecompressDxt1(
                        levelData,
                        levelWidth,
                        levelHeight
                        );
                }
                else if (surfaceFormat == SurfaceFormat.Dxt3 &&
                         !reader.GraphicsDevice.GLDevice.SupportsS3tc)
                {
                    if (levelData == null)
                    {
                        levelData = reader.ReadBytes(levelDataSizeInBytes);
                    }
                    levelData = DxtUtil.DecompressDxt3(
                        levelData,
                        levelWidth,
                        levelHeight
                        );
                }
                else if (surfaceFormat == SurfaceFormat.Dxt5 &&
                         !reader.GraphicsDevice.GLDevice.SupportsS3tc)
                {
                    if (levelData == null)
                    {
                        levelData = reader.ReadBytes(levelDataSizeInBytes);
                    }
                    levelData = DxtUtil.DecompressDxt5(
                        levelData,
                        levelWidth,
                        levelHeight
                        );
                }

                if (levelData == null &&
                    reader.BaseStream.GetType() != typeof(System.IO.MemoryStream))
                {
                    /* If the ContentReader is not backed by a
                     * MemoryStream, we have to read the data in.
                     */
                    levelData = reader.ReadBytes(levelDataSizeInBytes);
                }
                if (levelData != null)
                {
                    /* If we had to convert the data, or get the data from a
                     * non-MemoryStream, we set the data with our levelData
                     * reference.
                     */
                    texture.SetData(level, null, levelData, 0, levelData.Length);
                }
                else
                {
                    /* Ideally, we didn't have to perform any conversion or
                     * unnecessary reading. Just throw the buffer directly
                     * into SetData, skipping a redundant byte[] copy.
                     */
                    texture.SetData <byte>(
                        level,
                        null,
                        (((System.IO.MemoryStream)(reader.BaseStream)).GetBuffer()),
                        (int)reader.BaseStream.Position,
                        levelDataSizeInBytes
                        );
                    reader.BaseStream.Seek(
                        levelDataSizeInBytes,
                        System.IO.SeekOrigin.Current
                        );
                }
            }

            return(texture);
        }
Exemple #32
0
        protected internal override Texture2D Read(ContentReader reader, Texture2D existingInstance)
        {
            SurfaceFormat surfaceFormat;

            if (reader.version < 5)
            {
                switch ((SurfaceFormat_Legacy)reader.ReadInt32())
                {
                case SurfaceFormat_Legacy.Color:
                    surfaceFormat = SurfaceFormat.Color;
                    break;

                case SurfaceFormat_Legacy.Dxt1:
                    surfaceFormat = SurfaceFormat.Dxt1;
                    break;

                case SurfaceFormat_Legacy.Dxt3:
                    surfaceFormat = SurfaceFormat.Dxt3;
                    break;

                case SurfaceFormat_Legacy.Dxt5:
                    surfaceFormat = SurfaceFormat.Dxt5;
                    break;

                default:
                    throw new NotImplementedException();
                }
            }
            else
            {
                surfaceFormat = (SurfaceFormat)reader.ReadInt32();
            }
            int           width  = reader.ReadInt32();
            int           height = reader.ReadInt32();
            int           num1   = reader.ReadInt32();
            SurfaceFormat format = surfaceFormat;

            if (surfaceFormat == SurfaceFormat.NormalizedByte4)
            {
                format = SurfaceFormat.Color;
            }
            Texture2D texture2D = existingInstance != null ? existingInstance : new Texture2D(reader.GraphicsDevice, width, height, num1 > 1, format);

            for (int level = 0; level < num1; ++level)
            {
                int    count = reader.ReadInt32();
                byte[] data  = reader.ReadBytes(count);
                int    num2  = width >> level;
                int    num3  = height >> level;
                switch (surfaceFormat)
                {
                case SurfaceFormat.Bgra4444:
                    int startIndex = 0;
                    for (int index1 = 0; index1 < num3; ++index1)
                    {
                        for (int index2 = 0; index2 < num2; ++index2)
                        {
                            ushort num4 = BitConverter.ToUInt16(data, startIndex);
                            ushort num5 = (ushort)(((int)num4 & 4095) << 4 | ((int)num4 & 61440) >> 12);
                            data[startIndex]     = (byte)num5;
                            data[startIndex + 1] = (byte)((uint)num5 >> 8);
                            startIndex          += 2;
                        }
                    }
                    break;

                case SurfaceFormat.NormalizedByte4:
                    int num6 = GraphicsExtensions.Size(surfaceFormat);
                    int num7 = num2 * num6;
                    for (int index1 = 0; index1 < num3; ++index1)
                    {
                        for (int index2 = 0; index2 < num2; ++index2)
                        {
                            int num4 = BitConverter.ToInt32(data, index1 * num7 + index2 * num6);
                            data[index1 * num7 + index2 * 4]     = (byte)(num4 >> 16 & (int)byte.MaxValue);
                            data[index1 * num7 + index2 * 4 + 1] = (byte)(num4 >> 8 & (int)byte.MaxValue);
                            data[index1 * num7 + index2 * 4 + 2] = (byte)(num4 & (int)byte.MaxValue);
                            data[index1 * num7 + index2 * 4 + 3] = (byte)(num4 >> 24 & (int)byte.MaxValue);
                        }
                    }
                    break;
                }
                texture2D.SetData <byte>(level, new Rectangle?(), data, 0, data.Length);
            }
            return(texture2D);
        }