public byte[] EncodeImage(STGenericTexture texture, byte[] data, uint width, uint height, int array, int mip) { List <byte[]> mipmaps = SwizzleSurfaceMipMaps(texture, data, texture.MipCount); //Combine mip map data return(ByteUtils.CombineArray(mipmaps.ToArray())); }
public static Stream Decompress(Stream stream) { using (var reader = new FileReader(stream, true)) { ushort check = reader.ReadUInt16(); reader.ReadUInt16(); if (check != 0) { reader.SetByteOrder(true); } else { reader.SetByteOrder(false); } try { uint chunkCount = reader.ReadUInt32(); uint decompressedSize = reader.ReadUInt32(); uint[] chunkSizes = reader.ReadUInt32s((int)chunkCount); //Not very sure about this reader.Align(128); List <byte[]> DecompressedChunks = new List <byte[]>(); Console.WriteLine($"pos {reader.Position}"); //Now search for zlibbed chunks while (!reader.EndOfStream) { uint size = reader.ReadUInt32(); long pos = reader.Position; ushort magic = reader.ReadUInt16(); ///Check zlib magic if (magic == 0x78da || magic == 0xda78) { var data = STLibraryCompression.ZLIB.Decompress(reader.getSection((uint)pos, size)); DecompressedChunks.Add(data); reader.SeekBegin(pos + size); //Seek the compressed size and align it to goto the next chunk reader.Align(128); } else //If the magic check fails, seek back 2. This shouldn't happen, but just incase { reader.Seek(-2); } } //Return the decompressed stream with all chunks combined return(new MemoryStream(ByteUtils.CombineArray(DecompressedChunks.ToArray()))); } catch { } } return(null); }
public static GLTexture2DArray FromDDS(DDS dds) { GLTexture2DArray texture = new GLTexture2DArray(); texture.Width = (int)dds.Width; texture.Height = (int)dds.Height; texture.Bind(); GL.TexParameter(texture.Target, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); GL.TexParameter(texture.Target, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(texture.Target, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(texture.Target, TextureParameterName.TextureMaxLevel, 13); GL.TexParameter(texture.Target, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(texture.Target, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(texture.Target, TextureParameterName.TextureWrapR, (int)TextureWrapMode.ClampToEdge); InternalFormat format = InternalFormat.Rgba8; for (int j = 0; j < dds.MipCount; j++) { int mipWidth = CalculateMipDimension(texture.Width, j); int mipHeight = CalculateMipDimension(texture.Height, j); int imageSize = GLFormatHelper.CalculateImageSize(mipWidth, mipHeight, format) * (int)dds.ArrayCount; List <byte[]> levels = new List <byte[]>(); for (int i = 0; i < dds.ArrayCount; i++) { var data = dds.GetDecodedSurface(i, j); if (i == 0 || i == 1) { data = FlipHorizontal(mipWidth, mipHeight, data); } levels.Add(data); } var surface = ByteUtils.CombineArray(levels.ToArray()); if (format == InternalFormat.Rgba8) { GL.TexImage3D(TextureTarget.Texture2DArray, j, PixelInternalFormat.Rgba, mipWidth, mipHeight, (int)dds.ArrayCount, 0, PixelFormat.Bgra, PixelType.UnsignedByte, surface); } else { GL.CompressedTexImage3D(TextureTarget.Texture2DArray, j, format, mipWidth, mipHeight, (int)dds.ArrayCount, 0, imageSize, surface); } } texture.Unbind(); return(texture); }
public static GLTextureCubeArray FromDDS(DDS dds) { int size = (int)dds.Width; GLTextureCubeArray texture = new GLTextureCubeArray(); texture.Width = size; texture.Height = size; texture.Bind(); var format = dds.Platform.OutputFormat; var surfaces = dds.GetSurfaces(); List <byte[]> cubemapSurfaces = new List <byte[]>(); for (int a = 0; a < surfaces.Count; a++) { cubemapSurfaces.Add(surfaces[a].mipmaps[0]); } int depth = surfaces.Count; Console.WriteLine($"depth {depth}"); byte[] buffer = ByteUtils.CombineArray(cubemapSurfaces.ToArray()); for (int j = 0; j < dds.MipCount; j++) { int mipWidth = CalculateMipDimension(texture.Width, j); int mipHeight = CalculateMipDimension(texture.Height, j); if (dds.IsBCNCompressed()) { var internalFormat = GLFormatHelper.ConvertCompressedFormat(format, true); GLTextureDataLoader.LoadCompressedImage(texture.Target, mipWidth, mipHeight, depth, internalFormat, buffer, j); } else { var formatInfo = GLFormatHelper.ConvertPixelFormat(format); if (dds.Platform.OutputFormat == TexFormat.RGBA8_UNORM) { formatInfo.Format = PixelFormat.Rgba; } GLTextureDataLoader.LoadImage(texture.Target, mipWidth, mipHeight, depth, formatInfo, buffer, j); } } GL.TexParameter(texture.Target, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(texture.Target, TextureParameterName.TextureMaxLevel, 13); GL.GenerateMipmap(GenerateMipmapTarget.TextureCubeMapArray); texture.Unbind(); return(texture); }
public byte[] GetFullRawImageData(int level) { List <byte[]> outputs = new List <byte[]>(); for (int i = 0; i < 6; i++) { byte[] outputRaw = new byte[Width * Height * 5]; GL.GetTexImage(TextureTarget.TextureCubeMapPositiveX + i, level, PixelFormat, PixelType, outputRaw); outputs.Add(outputRaw); } return(ByteUtils.CombineArray(outputs.ToArray())); }
public byte[] GetDecompressedRawImageData(int level) { int size = CalculateMipDimension(Width, level); List <byte[]> outputs = new List <byte[]>(); for (int i = 0; i < 6; i++) { byte[] outputRaw = new byte[size * size * 4]; GL.GetTexImage(TextureTarget.TextureCubeMapPositiveX + i, level, PixelFormat.Bgra, PixelType.UnsignedByte, outputRaw); outputs.Add(outputRaw); } return(ByteUtils.CombineArray(outputs.ToArray())); }
/// <summary> /// Converts a Wii U texture instance to a switch texture. /// </summary> /// <param name="texture"></param> public void FromSwitch(Switch.SwitchTexture textureNX) { Width = textureNX.Width; Height = textureNX.Height; MipCount = textureNX.MipCount; Depth = 1; ArrayLength = textureNX.ArrayLength; TileMode = GX2TileMode.Mode2dTiledThin1; Dim = GX2SurfaceDim.Dim2D; Use = GX2SurfaceUse.Texture; Format = PlatformConverters.TextureConverter.FormatList.FirstOrDefault( x => x.Value == textureNX.Format).Key; Name = textureNX.Name; //Save arrays and mips into a list for swizzling back for (int i = 0; i < textureNX.ArrayLength; i++) { List <byte[]> mipData = new List <byte[]>(); for (int j = 0; j < textureNX.MipCount; j++) { mipData.Add(textureNX.GetDeswizzledData(i, j)); } //Swizzle the current mip data into a switch swizzled image var surface = SwizzleSurfaceMipMaps(ByteUtils.CombineArray(mipData.ToArray())); Data = surface.data; MipData = surface.mipData; TileMode = (GX2TileMode)surface.tileMode; MipOffsets = surface.mipOffset; MipCount = surface.numMips; Alignment = surface.alignment; Pitch = surface.pitch; Swizzle = surface.swizzle; Regs = surface.texRegs; } CompSelR = ConvertChannelSelector(textureNX.Texture.ChannelRed); CompSelG = ConvertChannelSelector(textureNX.Texture.ChannelGreen); CompSelB = ConvertChannelSelector(textureNX.Texture.ChannelBlue); CompSelA = ConvertChannelSelector(textureNX.Texture.ChannelAlpha); /* //Convert user data. BNTX doesn't share the same user data library atm so it needs manual conversion. * UserData = new ResDict<UserData>(); * foreach (var userData in textureNX.UserData) * { * * }*/ }
public override byte[] GetImageData(int ArrayLevel = 0, int MipLevel = 0, int DepthLevel = 0) { //Update the channels in the event the user may have adjusted them in the properties RedChannel = SetChannel(Texture.ChannelRed); GreenChannel = SetChannel(Texture.ChannelGreen); BlueChannel = SetChannel(Texture.ChannelBlue); AlphaChannel = SetChannel(Texture.ChannelAlpha); List <byte[]> buffer = new List <byte[]>(); foreach (var surface in Texture.TextureData) { buffer.Add(ByteUtils.CombineArray(surface.ToArray())); } return(ByteUtils.CombineArray(buffer.ToArray())); }
public static ShaderInfo LoadShaderProgram(byte[] vertexShader, byte[] fragmentShader) { if (!Directory.Exists("GFD/Cache")) { Directory.CreateDirectory("GFD/Cache"); } var shaderName = GetHashSHA1(ByteUtils.CombineArray(vertexShader, fragmentShader)); string key = $"{shaderName}"; if (GLShaderPrograms.ContainsKey(key)) { return(GLShaderPrograms[key]); } List <ShaderStage> stages = new List <ShaderStage>(); stages.Add(new ShaderStage() { Name = shaderName + "VS", Data = vertexShader, Command = "-v" }); stages.Add(new ShaderStage() { Name = shaderName + "FS", Data = fragmentShader, Command = "-p" }); var info = DecodeSharcBinary($"GFD", stages); //Load the source to opengl info.Program = new ShaderProgram( new FragmentShader(File.ReadAllText(info.FragPath)), new VertexShader(File.ReadAllText(info.VertPath))); GLShaderPrograms.Add(key, info); return(GLShaderPrograms[key]); }
public override byte[] GetSwizzledData() { return(ByteUtils.CombineArray(Data, MipData)); }