Skip to content

Commit

Permalink
Compile & load non-square textures correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
xezno committed Sep 13, 2024
1 parent 09dbdb3 commit 65d1217
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Source/Mocha.Engine/Render/Assets/Texture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Texture( string path, bool isSrgb = true )
var mipCount = textureFormat.Data.MipCount;
var format = GetRenderTextureFormat( textureFormat.Data.Format, isSrgb );

NativeTexture = new( Path, DataWidth, DataHeight );
NativeTexture = new( Path, Width, Height );

// Flatten mip data into one big buffer
List<byte> textureData = new List<byte>();
Expand All @@ -42,7 +42,7 @@ public Texture( string path, bool isSrgb = true )
textureData.AddRange( mipData[i] );
}

NativeTexture.SetData( DataWidth, DataHeight, (uint)mipCount, textureData.ToInterop(), (int)format );
NativeTexture.SetData( Width, Height, (uint)mipCount, textureData.ToInterop(), (int)format );
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ private static CompressionFormat TextureFormatToCompressionFormat( TextureFormat
private static byte[] BlockCompression( byte[] data, uint width, uint height, uint mip, TextureFormat format )
{
var targetWidth = MathX.CalcMipSize( (int)width, (int)mip );
byte[] resizedData = new byte[4 * targetWidth * targetWidth];
var targetHeight = MathX.CalcMipSize( (int)height, (int)mip );
byte[] resizedData = new byte[4 * targetWidth * targetHeight];

Rgba32[] pixels = new Rgba32[width * height];
for ( int i = 0; i < pixels.Length; i++ )
Expand All @@ -149,7 +150,7 @@ private static byte[] BlockCompression( byte[] data, uint width, uint height, ui

using ( var image = Image.LoadPixelData<Rgba32>( pixels.AsSpan(), (int)width, (int)height ) )
{
image.Mutate( x => x.Resize( targetWidth, targetWidth, KnownResamplers.Lanczos5 ) );
image.Mutate( x => x.Resize( targetWidth, targetHeight, KnownResamplers.Lanczos5 ) );
image.CopyPixelDataTo( resizedData );
}

Expand All @@ -160,7 +161,7 @@ private static byte[] BlockCompression( byte[] data, uint width, uint height, ui
encoder.OutputOptions.Format = TextureFormatToCompressionFormat( format );
encoder.OutputOptions.FileFormat = OutputFileFormat.Dds;

return encoder.EncodeToRawBytes( resizedData, (int)targetWidth, (int)targetWidth, PixelFormat.Rgba32, 0, out _, out _ );
return encoder.EncodeToRawBytes( resizedData, (int)targetWidth, (int)targetHeight, PixelFormat.Rgba32, 0, out _, out _ );
}

private static bool IsPowerOfTwo( int x )
Expand Down

0 comments on commit 65d1217

Please sign in to comment.