Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DDS support #68

Closed
wants to merge 3 commits into from
Closed

Add DDS support #68

wants to merge 3 commits into from

Conversation

Soreepeong
Copy link
Contributor

@Soreepeong Soreepeong commented Jul 24, 2023

  • Added a few entries to .editorconfig to accommodate automatic formatting in accordance with the rest of Lumina.
  • Added support for TexFile to DDS conversion.
  • Added support Texture2DArray.
  • Changed Squish to use Spans to avoid excessive byte array allocation.
  • Implemented BC5/7 decoding using Squish.

Tested with the following code. WTV recognizes output DDS files.

Code
using System;
using System.Runtime.CompilerServices;
using Lumina.Data.Files;
using Lumina.Data.Parsing.Tex.DirectDrawSurface;

namespace Lumina.Example
{
    class Program
    {
        static void Main( string[] args )
        {
            var gameData = new GameData( args[ 0 ] );
#if true
            var tex = gameData.GetFileFromDisk< TexFile >( /* bgcommon/texture/~A6C45FC3 */ @"test.tex" )!;
            var basename = "test";
            {
#else
            foreach( var path in new[]
                    {
                        "bg/ffxiv/fst_f1/cnt/f1c1/level/envl/evl6364405.tex",
                        "common/font/font1.tex",
                        "common/graphics/texture/-caustics.tex",
                        "common/graphics/texture/-fresnel.tex",
                        "common/graphics/texture/-mogu_anime_en.tex",
                        "ui/loadingimage/-nowloading_base22_hr1.tex",
                        "ui/loadingimage/-nowloading_base23_hr1.tex",
                        "ui/loadingimage/-nowloading_base25_hr1.tex",
                        "ui/icon/000000/000013_hr1.tex",
                    } )
            {
                var tex = gameData.GetFile< TexFile >( path )!;
                var basename = Path.GetFileNameWithoutExtension( path );
#endif
                var dds = tex.ToDdsFile( true );
                dds.WriteToFile( $"Z:/test/{basename}.dds" );

                for( var i = 0; i < tex.TextureBuffer.Depth; i++ )
                {
                    for( var j = 0; j < tex.TextureBuffer.MipmapAllocations.Length; j++ )
                    {
                        var buf = tex.TextureBuffer.Filter( j, i, TexFile.TextureFormat.B8G8R8A8 );
                        var dds2 = new DdsFile( new()
                        {
                            Size = Unsafe.SizeOf< DdsHeader >(),
                            Flags = DdsHeaderFlags.Caps | DdsHeaderFlags.PixelFormat | DdsHeaderFlags.Width | DdsHeaderFlags.Height,
                            Width = buf.Width,
                            Height = buf.Height,
                            Depth = 0,
                            MipMapCount = 0,
                            PixelFormat = new()
                            {
                                Size = Unsafe.SizeOf< DdsPixelFormat >(),
                                Flags = DdsPixelFormatFlags.Rgb | DdsPixelFormatFlags.AlphaPixels,
                                RgbBitCount = 32,
                                RBitMask = 0xFF0000,
                                GBitMask = 0xFF00,
                                BBitMask = 0xFF,
                                ABitMask = 0xFF000000,
                            },
                            Caps = DdsCaps1.Texture,
                            Caps2 = 0,
                        }, buf.RawData, Array.Empty< byte >() );
                        dds2.Header.Pitch = dds2.Pitch( 0 );
                        dds2.WriteToFile( $"Z:/test/{basename}.conv[{i},{j}].dds" );
                    }
                }

                foreach( var slice in dds.EnumerateSlices() )
                {
                    var span = dds.SliceSpan( slice.Image, slice.Face, slice.Mipmap, slice.Slice );
                    var dds2 = new DdsFile( new()
                    {
                        Size = Unsafe.SizeOf< DdsHeader >(),
                        Flags = DdsHeaderFlags.Caps | DdsHeaderFlags.PixelFormat | DdsHeaderFlags.Width | DdsHeaderFlags.Height,
                        Width = slice.Width,
                        Height = slice.Height,
                        Depth = 0,
                        MipMapCount = 0,
                        PixelFormat = dds.Header.PixelFormat,
                        Caps = DdsCaps1.Texture,
                        Caps2 = 0,
                    }, new()
                    {
                        DxgiFormat = dds.HeaderDxt10.DxgiFormat,
                        ResourceDimension = DdsHeaderDxt10ResourceDimension.Texture2D,
                        MiscFlag = 0,
                        ArraySize = 1,
                        MiscFlags2 = dds.HeaderDxt10.MiscFlags2,
                    }, span.ToArray(), Array.Empty< byte >() );
                    dds2.Header.Pitch = dds2.Pitch( 0 );
                    dds2.WriteToFile( $"Z:/test/{basename}.raw[{slice.Image}.{slice.Face}.{slice.Mipmap}.{slice.Slice}].dds" );
                }
            }
        }
    }
}

@Soreepeong Soreepeong marked this pull request as draft July 26, 2023 08:49
@Soreepeong Soreepeong force-pushed the feature/dds branch 3 times, most recently from 91c2ab6 to dc99cbf Compare July 26, 2023 15:08
@Soreepeong Soreepeong marked this pull request as ready for review July 26, 2023 15:09
@Soreepeong Soreepeong marked this pull request as draft July 26, 2023 17:53
@Soreepeong Soreepeong changed the title Add DDS export support Add DDS and Texture2DArray support Jul 31, 2023
@Soreepeong Soreepeong changed the title Add DDS and Texture2DArray support Add DDS support Jul 31, 2023
@Soreepeong
Copy link
Contributor Author

this is getting out of hand and probably better off not being in lumina

@Soreepeong Soreepeong closed this Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant