Parent Directory
|
Revision Log
|
Patch
--- trunk/gr2lib_typedefs/typedefs/TextureCreator/s3tc_texture_creator.cs 2010/08/07 02:42:31 48 +++ trunk/gr2lib_typedefs/typedefs/TextureCreator/s3tc_texture_creator.cs 2010/08/08 07:43:37 49 @@ -9,6 +9,9 @@ using gr2lib.core; using System.Runtime.InteropServices; using System.Diagnostics; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Graphics.PackedVector; +using Microsoft.Xna.Framework; namespace gr2lib_typedefs.typedefs.TextureCreator { @@ -17,6 +20,7 @@ //public override granny_texture_encoding EncodingType { get { return granny_texture_encoding.GrannyS3TCTextureEncoding; } } public override string TextureCreatorName { get { return "s3tc Texture Creator"; } } + #region Constructors public s3tc_texture_creator() : base() { } public s3tc_texture_creator(string filename) : base(filename) { } @@ -27,6 +31,12 @@ #endregion #endregion + #region Private Members + GraphicsDevice mDevice = null; + PresentationParameters pp = null; + Texture2D _texture = null; + #endregion + #region ATextureResource Members protected override void Parse() { @@ -35,48 +45,65 @@ //throw new NotImplementedException("s3tc texture creation has not been implemented"); this.Stream.Seek(0, SeekOrigin.Begin); - DdsFileTypePlugin.DdsFile _ddsfile = new DdsFileTypePlugin.DdsFile(); - _ddsfile.Load(this.Stream); - - this.TextureName = new FilePath(this.filename); - this.TextureWidth = _ddsfile.GetWidth(); - this.TextureHeight = _ddsfile.GetHeight(); - this.TextureBitsPerPixel = (int)_ddsfile.m_header.m_pixelFormat.m_rgbBitCount; - this.TextureMipMapCount = (int)_ddsfile.m_header.m_mipMapCount; - this.TextureRawPixelFormat = _ddsfile.m_header.m_fileformat; - //this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyUndefinedPixelFormat; + //DdsFileTypePlugin.DdsFile _ddsfile = new DdsFileTypePlugin.DdsFile(); + //_ddsfile.Load(this.Stream); + pp = new PresentationParameters(); + // Create device + mDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, + DeviceType.Hardware, this.Handle,pp); + + _texture = Texture2D.FromFile(mDevice, this.Stream, (int)this.Stream.Length); + + SpriteBatch batch = new SpriteBatch(mDevice); + RenderTarget2D renderTarget = new RenderTarget2D(mDevice, _texture.Width, _texture.Height, 1, SurfaceFormat.Color); + mDevice.SetRenderTarget(0, renderTarget); + batch.Begin(); + batch.Draw(_texture, new Rectangle(0, 0, _texture.Width, _texture.Height), Color.White); + batch.End(); - this.PixelBytes = _ddsfile.GetPixelData(); + this.TextureName = new FilePath(this.filename); + this.TextureWidth = _texture.Width; + this.TextureHeight = _texture.Height; + this.TextureBitsPerPixel = (int)_texture.Format; + //this.TextureMipMapCount = (int)_texture.LevelCount; + this.TextureMipMapCount = 0; + this.TextureRawPixelFormat = _texture.Format; - switch ((DdsFileTypePlugin.DdsFileFormat)this.TextureRawPixelFormat) + int granny_s3tc_texture_format_value = -1; + switch ((SurfaceFormat)this.TextureRawPixelFormat) { - case DdsFileTypePlugin.DdsFileFormat.DDS_FORMAT_R5G6B5: + case SurfaceFormat.Bgr565: this.TextureSpecificPixelFormat = coreapi.TextureSupport.GrannyGetS3TCPixelSubFormat(granny_s3tc_texture_format.GrannyS3TCBGR565); + granny_s3tc_texture_format_value = (int)granny_s3tc_texture_format.GrannyS3TCBGR565; break; - case DdsFileTypePlugin.DdsFileFormat.DDS_FORMAT_A1R5G5B5: + case SurfaceFormat.Bgra5551: this.TextureSpecificPixelFormat = coreapi.TextureSupport.GrannyGetS3TCPixelSubFormat(granny_s3tc_texture_format.GrannyS3TCBGRA5551); + granny_s3tc_texture_format_value = (int)granny_s3tc_texture_format.GrannyS3TCBGRA5551; break; - case DdsFileTypePlugin.DdsFileFormat.DDS_FORMAT_A8R8G8B8: - this.TextureSpecificPixelFormat = coreapi.TextureSupport.GrannyGetS3TCPixelSubFormat(granny_s3tc_texture_format.GrannyS3TCBGRA8888InterpolatedAlpha); - break; - case DdsFileTypePlugin.DdsFileFormat.DDS_FORMAT_DXT1: // can have an 1 bit alpha or no alpha (565 or 5551) - //if (_ddsfile.m_header.m_pixelFormat.m_flags == (int)DdsFileTypePlugin.DdsPixelFormat.PixelFormatFlags.DDS_RGBA) this.TextureSpecificPixelFormat = coreapi.TextureSupport.GrannyGetS3TCPixelSubFormat(granny_s3tc_texture_format.GrannyS3TCBGRA5551); - //if (_ddsfile.m_header.m_pixelFormat.m_flags == (int)DdsFileTypePlugin.DdsPixelFormat.PixelFormatFlags.DDS_RGB) this.TextureSpecificPixelFormat = coreapi.TextureSupport.GrannyGetS3TCPixelSubFormat(granny_s3tc_texture_format.GrannyS3TCBGR565); - break; - case DdsFileTypePlugin.DdsFileFormat.DDS_FORMAT_DXT3: + + case SurfaceFormat.Dxt1: // can have an 1 bit alpha or no alpha (565 or 5551) + throw new InvalidOperationException("Dxt1 Pixel format is not supported yet: could be 565 or 5551"); + case SurfaceFormat.Dxt2: + case SurfaceFormat.Dxt3: this.TextureSpecificPixelFormat = coreapi.TextureSupport.GrannyGetS3TCPixelSubFormat(granny_s3tc_texture_format.GrannyS3TCBGRA8888MappedAlpha); + granny_s3tc_texture_format_value = (int)granny_s3tc_texture_format.GrannyS3TCBGRA8888MappedAlpha; break; - case DdsFileTypePlugin.DdsFileFormat.DDS_FORMAT_DXT5: + case SurfaceFormat.Dxt4: + case SurfaceFormat.Dxt5: this.TextureSpecificPixelFormat = coreapi.TextureSupport.GrannyGetS3TCPixelSubFormat(granny_s3tc_texture_format.GrannyS3TCBGRA8888InterpolatedAlpha); + granny_s3tc_texture_format_value = (int)granny_s3tc_texture_format.GrannyS3TCBGRA8888InterpolatedAlpha; break; - case DdsFileTypePlugin.DdsFileFormat.DDS_FORMAT_INVALID: default: this.TextureSpecificPixelFormat = granny_pixel_layout.GrannyUndefinedPixelFormat; throw new InvalidOperationException("Attempted to read an image with an unsupported pixel format."); } + + // Get Pixel Bytes + this.GetPixelBytes(); + this.NativeTextureData.NativeTextureLayout = granny_pixel_layouts.GetPixelLayoutFromEnum(this.TextureSpecificPixelFormat); // create Granny2 Texture - this.NativeTextureData.NativeTextureBuilder = coreapi.TextureSupport.GrannyBeginBestMatchS3TCTexture(this.TextureWidth, this.TextureHeight); + this.NativeTextureData.NativeTextureBuilder = coreapi.TextureSupport.GrannyBeginS3TCTexture(this.TextureWidth, this.TextureHeight, (granny_s3tc_texture_format)granny_s3tc_texture_format_value); if (this.NativeTextureData.NativeTextureBuilder == IntPtr.Zero) throw new InvalidOperationException("Cannot process texure as the pointer to the Texture_Builder is null"); coreapi.TextureSupport.GrannyEncodeImage(this.NativeTextureData.NativeTextureBuilder, this.TextureWidth, this.TextureHeight, this.TextureStride, this.TextureMipMapCount, this.PixelBytesPointer); IntPtr ResultingTextureSizePTR = Marshal.AllocHGlobal(coreapi.TextureSupport.GrannyGetResultingTextureSize(this.NativeTextureData.NativeTextureBuilder)); @@ -91,7 +118,7 @@ this.NativeTextureData.NativeTextureEncoding = new gr2lib.core.helpers.NameValuePair<string, granny_texture_encoding>( coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_encoding>(granny_texture_encoding.GrannyS3TCTextureEncoding), granny_texture_encoding.GrannyS3TCTextureEncoding); this.NativeTextureData.NativeTextureSubFormat = new gr2lib.core.helpers.NameValuePair<string, granny_pixel_layout>( - coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_pixel_layout>(this.TextureSpecificPixelFormat), this.TextureSpecificPixelFormat); + coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_pixel_layout>((granny_pixel_layout)granny_s3tc_texture_format_value), (granny_pixel_layout)granny_s3tc_texture_format_value); this.NativeTextureData.NativeTextureType = new gr2lib.core.helpers.NameValuePair<string, granny_texture_type>( coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_type>(granny_texture_type.GrannyColorMapTextureType), granny_texture_type.GrannyColorMapTextureType); @@ -101,7 +128,8 @@ catch (Exception ex) { this.HaveBindableTexture = false; - Debug.WriteLine(ex.ToString()); + //Debug.WriteLine(ex.ToString()); + throw new InvalidOperationException("Failed to read texture using: " + new s3tc_texture_creator().TextureCreatorName, ex); } } @@ -111,8 +139,106 @@ } #endregion - + private void GetPixelBytes() + { + //switch ((SurfaceFormat)this.TextureRawPixelFormat) + //{ + // case SurfaceFormat.Bgr565: + // Bgr565[] _Bgr565 = new Bgr565[_texture.Width * _texture.Height]; + // _texture.GetData(_Bgr565); + // break; + // case SurfaceFormat.Bgra5551: + // Bgra5551[] _Bgra5551 = new Bgra5551[_texture.Width * _texture.Height]; + // _texture.GetData(_Bgra5551); + // break; + // case SurfaceFormat.Dxt1: // can have an 1 bit alpha or no alpha (565 or 5551) + // throw new InvalidOperationException("Dxt1 Pixel format is not supported yet: could be 565 or 5551"); + // case SurfaceFormat.Dxt2: + // case SurfaceFormat.Dxt3: + // break; + // case SurfaceFormat.Dxt4: + // case SurfaceFormat.Dxt5: + // break; + // default: + // throw new InvalidOperationException("Attempted to read pixel bytes for an image with an unsupported pixel format."); + //} + byte[] data; + Bgr565[] _Bgr565; + Bgra5551[] _Bgra5551; + //Color[] _Color; + //int BytesPerPixel = 4; + //int RealBytsPerPixel =4; + //Rectangle? _rect = new Rectangle(); + if ((SurfaceFormat)this.TextureRawPixelFormat == SurfaceFormat.Dxt1) + { + // 8 bytes per 4x4 blocks + int blocks = (_texture.Width * _texture.Height) / 16; + data = new byte[8 * blocks]; + _texture.GetData<byte>(data, 0, data.Length); + } + else if ((int)(SurfaceFormat)this.TextureRawPixelFormat > 28 && (int)(SurfaceFormat)this.TextureRawPixelFormat < 33) + { + // 16 bytes per 4x4 blocks + int blocks = (_texture.Width * _texture.Height) / 16; + data = new byte[16 * blocks]; + _texture.GetData<byte>(data, 0, data.Length); + } + else + { + switch ((SurfaceFormat)this.TextureRawPixelFormat) + { + case SurfaceFormat.Bgr565: + _Bgr565 = new Bgr565[_texture.Width * _texture.Height]; + _texture.GetData(_Bgr565); + this.PixelBytesCount = _texture.Width * _texture.Height * sizeof(ushort); + this.PixelBytes = new byte[this.PixelBytesCount]; + MemoryStream _Bgr565_Stream = new MemoryStream(this.PixelBytesCount); + BinaryWriter _Bgr565_Stream_Writer = new BinaryWriter(_Bgr565_Stream); + for (int i = 0; i < _Bgr565.Length; i += sizeof(ushort)) + { + Microsoft.Xna.Framework.Graphics.Color _color = new Microsoft.Xna.Framework.Graphics.Color(_Bgr565[i].ToVector3()); + // write value in format: RGB, because the source is BGR + Bgr565 _repack = new Bgr565((float)_color.R, (float)_color.G, (float)_color.B); + _Bgr565_Stream_Writer.Write(_repack.PackedValue); + } + _Bgr565_Stream_Writer.Flush(); + this.PixelBytes = _Bgr565_Stream.ToArray(); + _Bgr565_Stream_Writer.Close(); + break; + case SurfaceFormat.Bgra5551: + _Bgra5551 = new Bgra5551[_texture.Width * _texture.Height]; + _texture.GetData(_Bgra5551); + this.PixelBytesCount = _texture.Width * _texture.Height * sizeof(ushort); + this.PixelBytes = new byte[this.PixelBytesCount]; + MemoryStream _Bgra5551_Stream = new MemoryStream(this.PixelBytesCount); + BinaryWriter _Bgra5551_Stream_Writer = new BinaryWriter(_Bgra5551_Stream); + for (int i = 0; i < _Bgra5551.Length; i += sizeof(ushort)) + { + Microsoft.Xna.Framework.Graphics.Color _color = new Microsoft.Xna.Framework.Graphics.Color(_Bgra5551[i].ToVector4()); + // write value in format: ARGB, because the source is BGRA + Bgra5551 _repack = new Bgra5551((float)_color.R, (float)_color.G, (float)_color.B, (float)_color.A); + _Bgra5551_Stream_Writer.Write(_repack.PackedValue); + } + _Bgra5551_Stream_Writer.Flush(); + this.PixelBytes = _Bgra5551_Stream.ToArray(); + _Bgra5551_Stream_Writer.Close(); + + + break; + case SurfaceFormat.Color: + throw new InvalidOperationException("Attempted to read pixel data for a dds image with a pixel format of type: " + ((SurfaceFormat)this.TextureRawPixelFormat).GetType().FullName); + default: + throw new InvalidOperationException("Attempted to read pixel data for a dds image with a pixel format of type: " + ((SurfaceFormat)this.TextureRawPixelFormat).GetType().FullName); + } + } + + } + private static float UnpackUNorm(uint bitmask, uint value) + { + value &= bitmask; + return (((float)value) / ((float)bitmask)); + } #region RessourceHandler for s3tc Textures public class ResourceHandler : ATextureResourceHandler, IBindableTexture {
ViewVC Help | |
Powered by ViewVC 1.1.22 |