public byte[] DecodeImage(STGenericTexture texture, byte[] data, uint width, uint height, int array, int mip) { uint bpp = TextureFormatHelper.GetBytesPerPixel(OutputFormat); GX2.GX2Surface surf = new GX2.GX2Surface(); surf.bpp = bpp; surf.height = texture.Height; surf.width = texture.Width; surf.depth = texture.ArrayCount; surf.alignment = Alignment; surf.aa = (uint)AAMode; surf.dim = (uint)SurfaceDimension; surf.format = (uint)Format; surf.use = (uint)SurfaceUse; surf.pitch = Pitch; surf.data = data; surf.mipData = MipData != null ? MipData : data; surf.mipOffset = MipOffsets != null ? MipOffsets : new uint[0]; surf.numMips = texture.MipCount; surf.numArray = texture.ArrayCount; surf.tileMode = (uint)TileMode; surf.swizzle = Swizzle; Console.WriteLine("WII U DECODE"); return(GX2.Decode(surf, array, mip)); }
public byte[] DecodeImage(STGenericTexture texture, byte[] data, uint width, uint height, int array, int mip) { if (BlockHeightLog2 == 0) { uint blkHeight = TextureFormatHelper.GetBlockHeight(OutputFormat); uint blockHeight = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight)); BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1; } return(Switch.TegraX1Swizzle.GetImageData(texture, data, array, mip, 0, BlockHeightLog2, Target, LinearMode)); }
public bool Decode(TexFormat format, byte[] input, int width, int height, out byte[] output) { output = null; if (format.ToString().StartsWith("ASTC")) { var x = (int)TextureFormatHelper.GetBlockWidth(format); var y = (int)TextureFormatHelper.GetBlockHeight(format); var z = (int)TextureFormatHelper.GetBlockDepth(format); output = ASTCDecoder.DecodeToRGBA8888(input, x, y, z, width, height, 1); } return(output != null); }
internal void SetData(DataBox data, int mipLevel) { var format = EnumConverter.Convert(Format); if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None) { graphicsDevice.BindManager.SetTexture(this, 0); if (!TextureFormatHelper.IsCompressed(Format)) { GL.TexSubImage3D(TextureTarget.Texture3D, mipLevel, 0, 0, 0, Width, Height, Length, format.Item2, format.Item3, data.Pointer); } else { if (isInitialized) { GL.CompressedTexSubImage3D(TextureTarget.Texture3D, mipLevel, 0, 0, 0, Width, Height, Length, format.Item2, data.Size, data.Pointer); } else { GL.CompressedTexImage3D(TextureTarget.Texture3D, mipLevel, format.Item1, Width, Height, Length, 0, data.Size, data.Pointer); } } } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension) { if (!TextureFormatHelper.IsCompressed(Format)) { Ext.TextureSubImage3D(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture3D, mipLevel, 0, 0, 0, Width, Height, Length, (OpenTK.Graphics.OpenGL.PixelFormat)format.Item2, (OpenTK.Graphics.OpenGL.PixelType)format.Item3, data.Pointer); } else { if (isInitialized) { Ext.CompressedTextureSubImage3D(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture3D, mipLevel, 0, 0, 0, Width, Height, Length, (OpenTK.Graphics.OpenGL.PixelFormat)format.Item2, Marshal.SizeOf(data), data.Pointer); } else { Ext.CompressedTextureImage3D(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture3D, mipLevel, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)format.Item1, Width, Height, Length, 0, Marshal.SizeOf(data), data.Pointer); } } } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Core) { //OpenGL 4.5 } graphicsDevice.CheckGLError("Texture3D GenerateMipMaps"); }
public void LoadImage(int width, int height, TexFormat format, byte[] data) { if (TextureFormatHelper.IsBCNCompressed(format)) { var internalFormat = GLFormatHelper.ConvertCompressedFormat(format, true); int imageSize = GLFormatHelper.CalculateImageSize(width, height, internalFormat); GL.CompressedTexImage3D(TextureTarget.Texture2DArray, 0, internalFormat, width, height, 1, 0, imageSize, data); } else { var formatInfo = GLFormatHelper.ConvertPixelFormat(format); GL.TexImage3D(Target, 0, formatInfo.InternalFormat, width, height, 1, 0, formatInfo.Format, formatInfo.Type, data); } }
static void GetAllTex( ) { List <string> texPathLs = new List <string>(); GetAssetsTexInSubFolderForAtlas("assets", ref texPathLs); int totalCount = texPathLs.Count; if (texPathLs.Count > 0) { int iCurCount = 0; foreach (var path in texPathLs) { TextureFormatHelper.ModifyTextureFormat(path, "Android", TextureImporterFormat.ASTC_RGBA_4x4); TextureFormatHelper.ModifyTextureFormat(path, "iPhone", TextureImporterFormat.ASTC_RGBA_4x4); EditorUtility.DisplayCancelableProgressBar("Check TexFormat", "Wait......", (++iCurCount) * 1f / totalCount); } } EditorUtility.ClearProgressBar(); }
public byte[] DecodeImage(STGenericTexture texture, byte[] data, uint width, uint height, int array, int mip) { if (data.Length == 0) { throw new Exception("Data is empty! Failed to swizzle image!"); } if (BlockHeightLog2 == 0) { uint blkHeight = TextureFormatHelper.GetBlockHeight(OutputFormat); uint blockHeight = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(texture.Height, blkHeight)); BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length; if (OutputFormat != TexFormat.ASTC_8x5_UNORM) { BlockHeightLog2 -= 1; } } return(TegraX1Swizzle.GetImageData(texture, data, array, mip, 0, BlockHeightLog2, Target, LinearMode)); }
internal void SetData(DataRectangle data, int arrayIndex, int mipLevel) { var format = EnumConverter.Convert(Format); if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None) { graphicsDevice.BindManager.SetTexture(this, 0); if (!TextureFormatHelper.IsCompressed(Format)) { GL.TexSubImage3D(TextureTarget.Texture2DArray, mipLevel, 0, 0, 0, Width, Height, ArraySize, format.Item2, format.Item3, data.Pointer); } else { GL.CompressedTexImage3D(TextureTarget.Texture2DArray, mipLevel, format.Item1, Width, Height, ArraySize, 0, data.Size, data.Pointer); } GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMaxLevel, this.MipLevels - 1); } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension) { if (!TextureFormatHelper.IsCompressed(Format)) { Ext.TextureSubImage3D(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture2DArray, mipLevel, 0, 0, 0, Width, Height, ArraySize, (OpenTK.Graphics.OpenGL.PixelFormat)format.Item2, (OpenTK.Graphics.OpenGL.PixelType)format.Item3, data.Pointer); } else { Ext.CompressedTextureImage3D(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture2DArray, mipLevel, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.Convert(Format).Item1, Width, Height, ArraySize, 0, Marshal.SizeOf(data), data.Pointer); } OpenTK.Graphics.OpenGL.GL.Ext.TextureParameter(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture2DArray, OpenTK.Graphics.OpenGL.TextureParameterName.TextureMaxLevel, this.MipLevels - 1); } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Core) { //OpenGL 4.5 } graphicsDevice.CheckGLError("Texture2DArray SetData"); }
} internal Texture3D(GraphicsDevice graphicsDevice, int width, int height, int length, TextureFormat format, bool generateMipMaps, ResourceUsage usage = ResourceUsage.Normal, DataBox data = new DataBox()) : base(graphicsDevice, new System.Diagnostics.StackTrace(1)) { if (width <= 0) { throw new ArgumentOutOfRangeException("width", "Width must be positive."); } if (height <= 0) { throw new ArgumentOutOfRangeException("height", "Height must be positive."); } if (format == TextureFormat.Unknown) { throw new ArgumentException("Format must not be TextureFormat.Unknown.", "format"); } if (width > graphicsDevice.OpenGLCapabilities.MaxTextureSize) { throw new PlatformNotSupportedException("width exceeds the maximum texture size"); } if (height > graphicsDevice.OpenGLCapabilities.MaxTextureSize) { throw new PlatformNotSupportedException("height exceeds the maximum texture size"); } if ((width % 2 != 0 || height % 2 != 0 || length % 2 != 0) && graphicsDevice.OpenGLCapabilities.SupportsNonPowerOf2Textures) { throw new PlatformNotSupportedException("Driver doesn't support non power of two textures"); } if (length > graphicsDevice.OpenGLCapabilities.MaxTextureSize) { throw new PlatformNotSupportedException("length exceeds the maximum texture size"); } if (usage == ResourceUsage.Immutable && (data.IsNull)) { throw new ArgumentException("data", "Immutable textures must be initialized with data."); } this.Width = width; this.Height = height; this.Length = length; this.MipLevels = generateMipMaps ? OpenGL4.GraphicsDevice.MipLevels(width, height) : 1; this.Format = format; this.Usage = usage; var internalFormat = EnumConverter.Convert(Format); this.TextureID = GL.GenTexture(); if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None) { graphicsDevice.BindManager.SetTexture(this, 0); //Höchstes Mipmap-Level setzen GL.TexParameter(TextureTarget.Texture3D, TextureParameterName.TextureMaxLevel, this.MipLevels - 1); //Die Textur erstellen (Null Daten funktionieren nicht mit Compression) if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2)) { GL.TexStorage3D(TextureTarget3d.Texture3D, MipLevels, EnumConverter.ConvertSizedInternalFormat(Format), Width, Height, Length); } else { int size = 0; IntPtr ptr = IntPtr.Zero; if (!data.IsNull) { size = data.Size; ptr = data.Pointer; } if (!TextureFormatHelper.IsCompressed(Format)) { GL.TexImage3D(TextureTarget.Texture3D, 0, internalFormat.Item1, width, height, length, 0, internalFormat.Item2, internalFormat.Item3, ptr); } else if (ptr != IntPtr.Zero) { GL.CompressedTexImage3D(TextureTarget.Texture3D, 0, internalFormat.Item1, width, height, length, 0, size, ptr); isInitialized = true; } } } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension) { //Höchstes Mipmap-Level setzen OpenTK.Graphics.OpenGL.GL.Ext.TextureParameter(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture3D, OpenTK.Graphics.OpenGL.TextureParameterName.TextureMaxLevel, this.MipLevels - 1); //Die Textur erstellen (Null Daten funktionieren nicht mit Compression) if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2) && !TextureFormatHelper.IsCompressed(Format)) { Ext.TextureStorage3D(TextureID, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)TextureTarget3d.Texture3D, MipLevels, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.ConvertSizedInternalFormat(Format), Width, Height, Length); } else { int size = 0; IntPtr ptr = IntPtr.Zero; if (!data.IsNull) { size = data.Size; ptr = data.Pointer; } if (!TextureFormatHelper.IsCompressed(Format)) { Ext.TextureImage3D(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture3D, 0, (int)internalFormat.Item1, width, height, length, 0, (OpenTK.Graphics.OpenGL.PixelFormat)internalFormat.Item2, (OpenTK.Graphics.OpenGL.PixelType)internalFormat.Item3, ptr); } else if (ptr != IntPtr.Zero) { Ext.CompressedTextureImage3D(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture3D, 0, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)internalFormat.Item1, width, height, length, 0, size, ptr); isInitialized = true; } } } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Core) { } graphicsDevice.CheckGLError("Texture3D Constructor"); }
public static byte[] GetImageData(STGenericTexture texture, byte[] ImageData, int ArrayLevel, int MipLevel, int DepthLevel, uint BlockHeightLog2, int target = 1, bool LinearTileMode = false) { var format = texture.Platform.OutputFormat; uint bpp = TextureFormatHelper.GetBytesPerPixel(format); uint blkWidth = texture.GetBlockWidth(); uint blkHeight = texture.GetBlockHeight(); uint blkDepth = texture.GetBlockDepth(); uint blockHeight = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(texture.Height, blkHeight)); Console.WriteLine($"format {format} {bpp} {blkWidth} {blkHeight} {blockHeight}"); Console.WriteLine($"ArrayLevel {ArrayLevel} {MipLevel} {DepthLevel} {BlockHeightLog2}"); uint Pitch = 0; uint DataAlignment = 512; uint TileMode = 0; if (LinearTileMode) { TileMode = 1; } uint numDepth = 1; if (texture.Depth > 1) { numDepth = texture.Depth; } int linesPerBlockHeight = (1 << (int)BlockHeightLog2) * 8; uint ArrayOffset = 0; for (int depthLevel = 0; depthLevel < numDepth; depthLevel++) { for (int arrayLevel = 0; arrayLevel < texture.ArrayCount; arrayLevel++) { uint SurfaceSize = 0; int blockHeightShift = 0; List <uint> MipOffsets = new List <uint>(); for (int mipLevel = 0; mipLevel < texture.MipCount; mipLevel++) { uint width = (uint)Math.Max(1, texture.Width >> mipLevel); uint height = (uint)Math.Max(1, texture.Height >> mipLevel); uint depth = (uint)Math.Max(1, texture.Depth >> mipLevel); uint size = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth) * TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight) * bpp; Console.WriteLine($"size " + size); if (TegraX1Swizzle.pow2_round_up(TegraX1Swizzle.DIV_ROUND_UP(height, blkWidth)) < linesPerBlockHeight) { blockHeightShift += 1; } uint width__ = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth); uint height__ = TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight); //Calculate the mip size instead byte[] AlignedData = new byte[(TegraX1Swizzle.round_up(SurfaceSize, DataAlignment) - SurfaceSize)]; SurfaceSize += (uint)AlignedData.Length; MipOffsets.Add(SurfaceSize); //Get the first mip offset and current one and the total image size int msize = (int)((MipOffsets[0] + ImageData.Length - MipOffsets[mipLevel]) / texture.ArrayCount); byte[] data_ = ByteUtils.SubArray(ImageData, ArrayOffset + MipOffsets[mipLevel], (uint)msize); try { Pitch = TegraX1Swizzle.round_up(width__ * bpp, 64); SurfaceSize += Pitch * TegraX1Swizzle.round_up(height__, Math.Max(1, blockHeight >> blockHeightShift) * 8); byte[] result = TegraX1Swizzle.deswizzle(width, height, depth, blkWidth, blkHeight, blkDepth, target, bpp, TileMode, (int)Math.Max(0, BlockHeightLog2 - blockHeightShift), data_); //Create a copy and use that to remove uneeded data byte[] result_ = new byte[size]; Array.Copy(result, 0, result_, 0, size); result = null; if (ArrayLevel == arrayLevel && MipLevel == mipLevel && DepthLevel == depthLevel) { return(result_); } } catch (Exception e) { Console.WriteLine($"Failed to swizzle texture {texture.Name}!"); Console.WriteLine(e); return(new byte[0]); } } ArrayOffset += (uint)(ImageData.Length / texture.ArrayCount); } } return(new byte[0]); }
internal void SetData(DataRectangle data, int mipLevel) { if (mipLevel >= this.MipLevels) { throw new ArgumentOutOfRangeException("MipLevel exceeds the amount of mip levels."); } if (mipLevel < 0) { throw new ArgumentOutOfRangeException("MipLevel must not be smaller than zero."); } if (data.Size < 0) { throw new ArgumentOutOfRangeException("ImageSize must not be smaller than zero."); } var format = EnumConverter.Convert(Format); int width = Width / (int)Math.Pow(2, mipLevel); int height = Height / (int)Math.Pow(2, mipLevel); if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None) { graphicsDevice.BindManager.SetTexture(this, 0); if (!TextureFormatHelper.IsCompressed(Format)) { GL.TexSubImage2D(TextureTarget.Texture2D, mipLevel, 0, 0, width, height, format.Item2, format.Item3, data.Pointer); } else { if (isInitialized) { GL.CompressedTexSubImage2D(TextureTarget.Texture2D, mipLevel, 0, 0, width, height, format.Item2, data.Size, data.Pointer); } else { GL.CompressedTexImage2D(TextureTarget.Texture2D, mipLevel, format.Item1, width, height, 0, data.Size, data.Pointer); } } } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension) { if (!TextureFormatHelper.IsCompressed(Format)) { Ext.TextureSubImage2D(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, mipLevel, 0, 0, width, height, (OpenTK.Graphics.OpenGL.PixelFormat)format.Item2, (OpenTK.Graphics.OpenGL.PixelType)format.Item3, data.Pointer); } else { if (isInitialized) { Ext.CompressedTextureSubImage2D(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, mipLevel, 0, 0, width, height, (OpenTK.Graphics.OpenGL.PixelFormat)format.Item2, data.Size, data.Pointer); } else { Ext.CompressedTextureImage2D(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, mipLevel, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)format.Item1, width, height, 0, data.Size, data.Pointer); } } } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Core) { //OpenGL 4.5 } graphicsDevice.CheckGLError("Texture2D SetData"); }
internal Texture2D(GraphicsDevice graphicsDevice, int width, int height, TextureFormat format, int mipLevels, ResourceUsage usage = ResourceUsage.Normal, params DataRectangle[] data) : base(graphicsDevice, new System.Diagnostics.StackTrace(1)) { if (width <= 0) { throw new ArgumentOutOfRangeException("width", "Width must be positive."); } if (height <= 0) { throw new ArgumentOutOfRangeException("height", "Height must be positive."); } if (mipLevels < 0) { throw new ArgumentOutOfRangeException("mipLevels", "MipLevels must not be negative."); } if (format == TextureFormat.Unknown) { throw new ArgumentException("Format must not be TextureFormat.Unknown.", "format"); } if (width > graphicsDevice.OpenGLCapabilities.MaxTextureSize) { throw new PlatformNotSupportedException("width exceeds the maximum texture size"); } if (height > graphicsDevice.OpenGLCapabilities.MaxTextureSize) { throw new PlatformNotSupportedException("height exceeds the maximum texture size"); } if (usage == ResourceUsage.Immutable && (data == null || data.Length == 0)) { throw new ArgumentException("data", "Immutable textures must be initialized with data."); } if (data != null && data.Length != 0 && data.Length < mipLevels) { throw new ArgumentOutOfRangeException("data", data.Length, string.Format("data Lenght is too small for specified mipLevels, expected: {0}", mipLevels)); } this.Width = width; this.Height = height; this.Format = format; this.Usage = usage; var internalFormat = EnumConverter.Convert(Format); this.MipLevels = mipLevels > 0 ? mipLevels : 1; this.TextureID = GL.GenTexture(); if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None) { graphicsDevice.BindManager.SetTexture(this, 0); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, this.MipLevels - 1); if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2) && !TextureFormatHelper.IsCompressed(Format)) { GL.TexStorage2D(TextureTarget2d.Texture2D, MipLevels, EnumConverter.ConvertSizedInternalFormat(Format), Width, Height); } else { int size = 0; IntPtr ptr = IntPtr.Zero; if (data != null && data.Length > 0) { size = data[0].Size; ptr = data[0].Pointer; } if (!TextureFormatHelper.IsCompressed(Format)) { GL.TexImage2D(TextureTarget.Texture2D, 0, internalFormat.Item1, width, height, 0, internalFormat.Item2, internalFormat.Item3, ptr); } else if (ptr != IntPtr.Zero) { GL.CompressedTexImage2D(TextureTarget.Texture2D, 0, internalFormat.Item1, width, height, 0, size, ptr); } } } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension) { OpenTK.Graphics.OpenGL.GL.Ext.TextureParameter(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, OpenTK.Graphics.OpenGL.TextureParameterName.TextureMaxLevel, this.MipLevels - 1); if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2) && !TextureFormatHelper.IsCompressed(Format)) { Ext.TextureStorage2D(TextureID, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)TextureTarget2d.Texture2D, MipLevels, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.ConvertSizedInternalFormat(Format), Width, Height); } else { int size = 0; IntPtr ptr = IntPtr.Zero; if (data != null && data.Length > 0) { size = data[0].Size; ptr = data[0].Pointer; } if (!TextureFormatHelper.IsCompressed(Format)) { Ext.TextureImage2D(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture2D, 0, (int)internalFormat.Item1, width, height, 0, (OpenTK.Graphics.OpenGL.PixelFormat)internalFormat.Item2, (OpenTK.Graphics.OpenGL.PixelType)internalFormat.Item3, ptr); } else if (ptr != IntPtr.Zero) { Ext.CompressedTextureImage2D(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture2D, 0, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)internalFormat.Item1, width, height, 0, size, ptr); isInitialized = true; } } } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Core) { } graphicsDevice.CheckGLError("Texture2D Constructor"); if (data != null && data.Length > 1) { for (int i = 1; i < data.Length; i++) { SetData(data[i], i); } } }
public override void LoadData(Graphics.ITexture2D handle, string file, TextureLoadSettings settings) { if (file == null) { throw new ArgumentNullException("file"); } if (handle == null) { throw new ArgumentNullException("handle"); } if (!IsHeaderLoaded) { LoadHeader(file, settings); } FileStream stream = new FileStream(file, FileMode.Open); if (stream.Length <= 128) { throw new Exception("File is too small"); } GCHandle arrayHandle; try { int dataOffset = 4 + 124; if (hasHeaderDXT10) { dataOffset += Marshal.SizeOf(typeof(HeaderDXT10)); } stream.Position = dataOffset; int dataSize = (int)stream.Length - dataOffset; byte[] data = new byte[dataSize]; stream.Read(data, 0, dataSize); if (!IsVolumeTexture) { int faces = IsCubeMap ? 6 : 1; int levels = header.MipLevels > 0 ? header.MipLevels : 1; DataRectangle[] rectangles = new DataRectangle[levels * faces]; if (TextureFormatHelper.IsCompressed(header.Format)) { arrayHandle = GCHandle.Alloc(data, GCHandleType.Pinned); } else { arrayHandle = GCHandle.Alloc(ConvertFormat(data), GCHandleType.Pinned); } try { IntPtr pointer = arrayHandle.AddrOfPinnedObject(); for (int face = 0; face < faces; face++) { int width = header.Width; int height = header.Height; for (int mipLevel = 0; mipLevel < levels; mipLevel++) { int size = (int)GetSize(width, height); rectangles[levels * face + mipLevel] = new DataRectangle(pointer, (int)GetPitch(width), size); width /= 2; height /= 2; pointer += size; } } for (int i = 0; i < rectangles.Length; i++) { Engine.GraphicsDevice.RenderContext.UpdateContext.Update(handle, i, rectangles[i]); } } finally { arrayHandle.Free(); } } else { //TextureImporterBase doesn't support 3D Textures } } finally { //if (arrayHandle.IsAllocated) // arrayHandle.Free(); stream.Close(); } }
internal Texture2DArray(GraphicsDevice graphicsDevice, int width, int height, int arraySize, TextureFormat format, bool generateMipMaps, ResourceUsage usage = ResourceUsage.Normal, params DataRectangle[] data) : base(graphicsDevice, new System.Diagnostics.StackTrace(1)) { if (width <= 0) { throw new ArgumentOutOfRangeException("width", "Width must be positive."); } if (height <= 0) { throw new ArgumentOutOfRangeException("height", "Height must be positive."); } if (format == TextureFormat.Unknown) { throw new ArgumentException("Format must not be TextureFormat.Unknown.", "format"); } if (width > graphicsDevice.OpenGLCapabilities.MaxTextureSize) { throw new PlatformNotSupportedException("width exceeds the maximum texture size"); } if (height > graphicsDevice.OpenGLCapabilities.MaxTextureSize) { throw new PlatformNotSupportedException("height exceeds the maximum texture size"); } if ((width % 2 != 0 || height % 2 != 0) && graphicsDevice.OpenGLCapabilities.SupportsNonPowerOf2Textures) { throw new PlatformNotSupportedException("Driver doesn't support non power of two textures"); } if (width != height) { throw new PlatformNotSupportedException("Texture arrays must be quadratic"); } if (arraySize <= 0) { throw new ArgumentOutOfRangeException("Array Size must be at least one", "arraySize"); } if (usage == ResourceUsage.Immutable && (data == null || data.Length == 0)) { throw new ArgumentException("data", "Immutable textures must be initialized with data."); } if (data != null && data.Length != 0 && data.Length < arraySize) { throw new ArgumentOutOfRangeException("data", data.Length, string.Format("data Lenght is too small for specified arraySize, expected: {0}", arraySize)); } this.Width = width; this.Height = height; this.ArraySize = arraySize; this.MipLevels = generateMipMaps ? OpenGL4.GraphicsDevice.MipLevels(width, height) : 1; this.Format = format; this.Usage = usage; var internalFormat = EnumConverter.Convert(Format); this.TextureID = GL.GenTexture(); if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None) { graphicsDevice.BindManager.SetTexture(this, 0); GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMaxLevel, this.MipLevels - 1); if (!TextureFormatHelper.IsCompressed(Format)) { if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2)) { GL.TexStorage3D(TextureTarget3d.Texture2DArray, MipLevels, EnumConverter.ConvertSizedInternalFormat(Format), Width, Height, ArraySize); } else { GL.TexImage3D(TextureTarget.Texture2DArray, 0, internalFormat.Item1, width, height, arraySize, 0, internalFormat.Item2, internalFormat.Item3, IntPtr.Zero); } } } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension) { Ext.TextureParameter(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, OpenTK.Graphics.OpenGL.TextureParameterName.TextureMaxLevel, this.MipLevels - 1); if (!TextureFormatHelper.IsCompressed(Format)) { if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2)) { Ext.TextureStorage3D(TextureID, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)TextureTarget3d.Texture2DArray, MipLevels, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.ConvertSizedInternalFormat(Format), Width, Height, ArraySize); } else { Ext.TextureImage3D(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture3D, 0, (int)internalFormat.Item1, width, height, arraySize, 0, (OpenTK.Graphics.OpenGL.PixelFormat)internalFormat.Item2, (OpenTK.Graphics.OpenGL.PixelType)internalFormat.Item3, IntPtr.Zero); } } } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Core) { } if (data != null) { for (int i = 0; i < data.Length; i++) { SetData(data[i], i, 0); } } graphicsDevice.CheckGLError(); }
internal Texture2DArray(GraphicsDevice graphicsDevice, int width, int height, int arraySize, TextureFormat format, int mipLevels, ResourceUsage usage = ResourceUsage.Normal, params DataRectangle[] data) : base(graphicsDevice, new System.Diagnostics.StackTrace(1)) { if (width <= 0) { throw new ArgumentOutOfRangeException("width", "Width must be positive."); } if (height <= 0) { throw new ArgumentOutOfRangeException("height", "Height must be positive."); } if (mipLevels < 0) { throw new ArgumentOutOfRangeException("mipLevels", "MipLevels must not be negative."); } if (format == TextureFormat.Unknown) { throw new ArgumentException("Format must not be TextureFormat.Unknown.", "format"); } if (width > graphicsDevice.OpenGLCapabilities.MaxTextureSize) { throw new PlatformNotSupportedException("width exceeds the maximum texture size"); } if (height > graphicsDevice.OpenGLCapabilities.MaxTextureSize) { throw new PlatformNotSupportedException("height exceeds the maximum texture size"); } if (width != height) { throw new PlatformNotSupportedException("Texture arrays must be quadratic"); } if (arraySize <= 0) { throw new ArgumentOutOfRangeException("Array Size must be at least one", "arraySize"); } if (data != null && data.Length != 0 && data.Length < mipLevels * arraySize) { throw new ArgumentOutOfRangeException("data", data.Length, string.Format("data Lenght is too small for specified arraySize and mipLevels, expected: {0}", mipLevels * arraySize)); } this.Width = width; this.Height = height; this.ArraySize = arraySize; this.MipLevels = mipLevels > 0 ? mipLevels : 1; this.Format = format; this.Usage = usage; var internalFormat = EnumConverter.Convert(Format); this.TextureID = GL.GenTexture(); if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None) { graphicsDevice.BindManager.SetTexture(this, 0); GL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMaxLevel, this.MipLevels - 1); if (!TextureFormatHelper.IsCompressed(Format)) { if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2)) { GL.TexStorage3D(TextureTarget3d.Texture2DArray, MipLevels, EnumConverter.ConvertSizedInternalFormat(Format), Width, Height, ArraySize); } else { GL.TexImage3D(TextureTarget.Texture2DArray, 0, internalFormat.Item1, width, height, arraySize, 0, internalFormat.Item2, internalFormat.Item3, IntPtr.Zero); } } } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension) { Ext.TextureParameter(TextureID, OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, OpenTK.Graphics.OpenGL.TextureParameterName.TextureMaxLevel, this.MipLevels - 1); if (!TextureFormatHelper.IsCompressed(Format)) { if (graphicsDevice.OpenGLCapabilities.SupportsTextureStorage && graphicsDevice.OpenGLCapabilities.OpenGLVersion > new Version(4, 2)) { Ext.TextureStorage3D(TextureID, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)TextureTarget3d.Texture2DArray, MipLevels, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.ConvertSizedInternalFormat(Format), Width, Height, ArraySize); } else { Ext.TextureImage3D(TextureID, (OpenTK.Graphics.OpenGL.TextureTarget)TextureTarget.Texture3D, 0, (int)internalFormat.Item1, width, height, arraySize, 0, (OpenTK.Graphics.OpenGL.PixelFormat)internalFormat.Item2, (OpenTK.Graphics.OpenGL.PixelType)internalFormat.Item3, IntPtr.Zero); } } } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Core) { } if (data != null) { for (int i = 0; i < data.Length; i++) { int arrayIndex = i / MipLevels; int mipIndex = i - arrayIndex * MipLevels; //Macht Sinn weil int gerundet wird SetData(data[i], arrayIndex, 0); } } graphicsDevice.CheckGLError(); }
//Method from https://github.com/aboood40091/BNTX-Editor/blob/master/formConv.py public bool Decode(TexFormat format, byte[] input, int width, int height, out byte[] output) { if (!IsSupported(format)) { output = null; return(false); } uint bpp = TextureFormatHelper.GetBytesPerPixel(format); int size = width * height * 4; bpp = (uint)(input.Length / (width * height)); output = new byte[size]; int inPos = 0; int outPos = 0; byte[] comp = new byte[] { 0, 0, 0, 0xFF, 0, 0xFF }; byte[] compSel = new byte[4] { 0, 1, 2, 3 }; if (format == TexFormat.LA8) { compSel = new byte[4] { 0, 0, 0, 1 }; bpp = 2; } else if (format == TexFormat.L8) { compSel = new byte[4] { 0, 0, 0, 5 } } ; for (int Y = 0; Y < height; Y++) { for (int X = 0; X < width; X++) { inPos = (Y * width + X) * (int)bpp; outPos = (Y * width + X) * 4; int pixel = 0; for (int i = 0; i < bpp; i++) { pixel |= input[inPos + i] << (8 * i); } comp = GetComponentsFromPixel(format, pixel, comp); output[outPos + 3] = comp[compSel[3]]; output[outPos + 2] = comp[compSel[2]]; output[outPos + 1] = comp[compSel[1]]; output[outPos + 0] = comp[compSel[0]]; } } return(output != null); }
public static List <uint[]> GenerateMipSizes(TexFormat Format, uint Width, uint Height, uint Depth, uint SurfaceCount, uint MipCount, uint ImageSize) { List <uint[]> MipMapSizes = new List <uint[]>(); uint bpp = TextureFormatHelper.GetBytesPerPixel(Format); uint blkWidth = TextureFormatHelper.GetBlockWidth(Format); uint blkHeight = TextureFormatHelper.GetBlockHeight(Format); uint blkDepth = TextureFormatHelper.GetBlockDepth(Format); uint blockHeight = TegraX1Swizzle.GetBlockHeight(TegraX1Swizzle.DIV_ROUND_UP(Height, blkHeight)); uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1; uint Pitch = 0; uint DataAlignment = 512; int linesPerBlockHeight = (1 << (int)BlockHeightLog2) * 8; uint ArrayCount = SurfaceCount; uint ArrayOffset = 0; for (int arrayLevel = 0; arrayLevel < ArrayCount; arrayLevel++) { uint SurfaceSize = 0; int blockHeightShift = 0; uint[] MipOffsets = new uint[MipCount]; for (int mipLevel = 0; mipLevel < MipCount; mipLevel++) { uint width = (uint)Math.Max(1, Width >> mipLevel); uint height = (uint)Math.Max(1, Height >> mipLevel); uint depth = (uint)Math.Max(1, Depth >> mipLevel); uint size = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth) * TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight) * bpp; if (TegraX1Swizzle.pow2_round_up(TegraX1Swizzle.DIV_ROUND_UP(height, blkWidth)) < linesPerBlockHeight) { blockHeightShift += 1; } uint width__ = TegraX1Swizzle.DIV_ROUND_UP(width, blkWidth); uint height__ = TegraX1Swizzle.DIV_ROUND_UP(height, blkHeight); //Calculate the mip size instead byte[] AlignedData = new byte[(TegraX1Swizzle.round_up(SurfaceSize, DataAlignment) - SurfaceSize)]; SurfaceSize += (uint)AlignedData.Length; MipOffsets[mipLevel] = (SurfaceSize); //Get the first mip offset and current one and the total image size int msize = (int)((MipOffsets[0] + ImageSize - MipOffsets[mipLevel]) / ArrayCount); Pitch = TegraX1Swizzle.round_up(width__ * bpp, 64); SurfaceSize += Pitch * TegraX1Swizzle.round_up(height__, Math.Max(1, blockHeight >> blockHeightShift) * 8); } ArrayOffset += (uint)(ImageSize / ArrayCount); MipMapSizes.Add(MipOffsets); } return(MipMapSizes); }
static void printLightMapIndex() { TextureImporterFormat format = TextureImporterFormat.ASTC_4x4; string root = AssetDatabase.GetAssetPath(Lightmapping.lightingDataAsset); root = root.Substring(0, root.LastIndexOf('/')); string end1 = "_comp_shadowmask.png"; string end2 = "_comp_light.exr"; var s = UnityEngine.SceneManagement.SceneManager.GetActiveScene(); string end3 = "_LightSM.tga"; List <string> found_paths = new List <string>(); int width = 512; int height = 512; string [] paths = System.IO.Directory.GetFiles(root); Dictionary <int, string> lightMapTemp = new Dictionary <int, string>(); int maxIndex = -1; for (int i = 0; i < paths.Length; i++) { string path = paths[i]; if (path.EndsWith(end1)) { string heater = path.Substring(0, path.Length - end1.Length); int index = int.Parse(heater.Substring(heater.LastIndexOf('-') + 1)); maxIndex = Mathf.Max(index, maxIndex); string path2 = heater + end2; if (System.IO.File.Exists(path2)) { Texture2D t = AssetDatabase.LoadAssetAtPath <Texture2D>(path); Texture2D t2 = AssetDatabase.LoadAssetAtPath <Texture2D>(path2); found_paths.Add(path); found_paths.Add(path2); width = t.width; height = t.height; RenderTexture rt = RenderTexture.GetTemporary(t.width, t.height, 0); Material mat = new Material(Shader.Find("Hidden/HdrToHalfColor")); mat.SetTexture("_MainTex", t2); mat.SetTexture("_MainTex2", t); Graphics.Blit(t2, rt, mat); RenderTexture.active = rt; Texture2D png = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false); png.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0); byte[] bytes = EncodeToTGAExtension.EncodeToTGA(png, 4); string path3 = heater + end3; System.IO.File.WriteAllBytes(path3, bytes); AssetDatabase.ImportAsset(path3); TextureFormatHelper.ModifyTextureFormat(path3, "iPhone", format); TextureFormatHelper.ModifyTextureFormat(path3, "Android", format); TextureImporter texImporter = TextureImporter.GetAtPath(path3) as TextureImporter; texImporter.isReadable = true; GameObject.DestroyImmediate(png, true); RenderTexture.ReleaseTemporary(rt); GameObject.DestroyImmediate(t, false); GameObject.DestroyImmediate(t2, false);; lightMapTemp[index] = path3; } } } Texture2DArray texture2DArray = new Texture2DArray(width, height, maxIndex + 1, TextureFormat.ASTC_4x4, true); foreach (var kp in lightMapTemp) { AssetDatabase.ImportAsset(kp.Value); Texture2D t = AssetDatabase.LoadAssetAtPath <Texture2D>(kp.Value); Graphics.CopyTexture(t, 0, texture2DArray, kp.Key); texture2DArray.Apply(); GameObject.DestroyImmediate(t, true); AssetDatabase.DeleteAsset(kp.Value); //System.IO.File.Delete(kp.Value) ; } texture2DArray.wrapMode = TextureWrapMode.Clamp; texture2DArray.filterMode = FilterMode.Bilinear; string arrayPath = root + "\\" + s.name + "_LMSM.asset"; AssetDatabase.CreateAsset(texture2DArray, arrayPath); AssetDatabase.ImportAsset(arrayPath); ShadowMarkTex2dAry sma = GameObject.FindObjectOfType <ShadowMarkTex2dAry>(); if (null == sma) { GameObject g = null; if (null != Camera.main) { g = Camera.main.gameObject; } else { g = new GameObject("Camera"); g.AddComponent <Camera>(); } sma = g.AddComponent <ShadowMarkTex2dAry>(); } sma.shadowMark = AssetDatabase.LoadAssetAtPath <Texture2DArray>(arrayPath); AddCmpToRender(); //Texture2D empty = new Texture2D(1, 1,TextureFormat.RGBA32,false); //empty.SetPixel(0, 0, Color.white); for (int i = 0; i < found_paths.Count; i++) { var str = found_paths[i]; //System.IO.File.WriteAllBytes(str,empty.EncodeToPNG()); //AssetDatabase.ImportAsset(str); AssetDatabase.DeleteAsset(str); } EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene()); //GameObject.DestroyImmediate(empty, true); }