Add multi thread option for encoding images faster.

This commit is contained in:
KillzXGaming 2019-11-07 17:40:48 -05:00
parent aab84f91e6
commit 0435f651e1
11 changed files with 61 additions and 34 deletions

View file

@ -143,7 +143,7 @@ namespace FirstPlugin
if (settings.GenerateMipmaps && !settings.IsFinishedCompressing)
{
settings.DataBlockOutput.Clear();
settings.DataBlockOutput.Add(settings.GenerateMips(importer.CompressionMode));
settings.DataBlockOutput.Add(settings.GenerateMips(importer.CompressionMode, importer.MultiThreading));
}
ApplySettings(settings);

View file

@ -673,7 +673,7 @@ namespace FirstPlugin
if (setting.GenerateMipmaps && !setting.IsFinishedCompressing)
{
setting.DataBlockOutput.Clear();
setting.DataBlockOutput.Add(setting.GenerateMips(CompressionMode));
setting.DataBlockOutput.Add(setting.GenerateMips(CompressionMode, false));
}
if (setting.DataBlockOutput.Count <= 0)
throw new Exception("Data block is empty! Failed to compress!");
@ -902,11 +902,11 @@ namespace FirstPlugin
case ".dds":
case ".dds2":
setting.LoadDDS(file, null, node);
node.ApplyImportSettings(setting, STCompressionMode.Normal);
node.ApplyImportSettings(setting, STCompressionMode.Normal, false);
break;
case ".astc":
setting.LoadASTC(file);
node.ApplyImportSettings(setting, STCompressionMode.Normal);
node.ApplyImportSettings(setting, STCompressionMode.Normal, false);
break;
case ".png":
case ".tga":
@ -940,7 +940,7 @@ namespace FirstPlugin
if (importer.ShowDialog() == DialogResult.OK)
{
foreach (TextureData node in TexturesForImportSettings)
node.ApplyImportSettings(settings[settingsIndex++], importer.CompressionMode);
node.ApplyImportSettings(settings[settingsIndex++], importer.CompressionMode, importer.MultiThreading);
}
TexturesForImportSettings.Clear();
@ -1482,11 +1482,11 @@ namespace FirstPlugin
break;
case ".dds":
setting.LoadDDS(FileName, null, this);
ApplyImportSettings(setting, STCompressionMode.Normal);
ApplyImportSettings(setting, STCompressionMode.Normal, false);
break;
case ".astc":
setting.LoadASTC(FileName);
ApplyImportSettings(setting, STCompressionMode.Normal);
ApplyImportSettings(setting, STCompressionMode.Normal, false);
break;
default:
setting.LoadBitMap(FileName);
@ -1503,7 +1503,7 @@ namespace FirstPlugin
if (importer.ShowDialog() == DialogResult.OK)
{
ApplyImportSettings(setting, importer.CompressionMode);
ApplyImportSettings(setting, importer.CompressionMode, importer.MultiThreading);
}
break;
}
@ -1519,14 +1519,14 @@ namespace FirstPlugin
Texture.TextureData.Add(ImageDataCached[i]);
}
}
public void ApplyImportSettings(TextureImporterSettings setting,STCompressionMode CompressionMode)
public void ApplyImportSettings(TextureImporterSettings setting,STCompressionMode CompressionMode, bool multiThread)
{
Cursor.Current = Cursors.WaitCursor;
if (setting.GenerateMipmaps && !setting.IsFinishedCompressing)
{
setting.DataBlockOutput.Clear();
setting.DataBlockOutput.Add(setting.GenerateMips(CompressionMode));
setting.DataBlockOutput.Add(setting.GenerateMips(CompressionMode, multiThread));
}
Texture = setting.FromBitMap(setting.DataBlockOutput, setting);

View file

@ -51,6 +51,7 @@
this.compModeLbl = new Toolbox.Library.Forms.STLabel();
this.dataSizeLbl = new Toolbox.Library.Forms.STLabel();
this.cnkGammaFix = new Toolbox.Library.Forms.STCheckBox();
this.chkMultiThreading = new Toolbox.Library.Forms.STCheckBox();
this.contentContainer.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.SwizzleNum)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.MipmapNum)).BeginInit();
@ -59,6 +60,7 @@
//
// contentContainer
//
this.contentContainer.Controls.Add(this.chkMultiThreading);
this.contentContainer.Controls.Add(this.cnkGammaFix);
this.contentContainer.Controls.Add(this.dataSizeLbl);
this.contentContainer.Controls.Add(this.compressionModeCB);
@ -100,6 +102,7 @@
this.contentContainer.Controls.SetChildIndex(this.compressionModeCB, 0);
this.contentContainer.Controls.SetChildIndex(this.dataSizeLbl, 0);
this.contentContainer.Controls.SetChildIndex(this.cnkGammaFix, 0);
this.contentContainer.Controls.SetChildIndex(this.chkMultiThreading, 0);
//
// button2
//
@ -321,6 +324,17 @@
this.cnkGammaFix.UseVisualStyleBackColor = true;
this.cnkGammaFix.CheckedChanged += new System.EventHandler(this.cnkGammaFix_CheckedChanged);
//
// chkMultiThreading
//
this.chkMultiThreading.AutoSize = true;
this.chkMultiThreading.Location = new System.Drawing.Point(754, 322);
this.chkMultiThreading.Name = "chkMultiThreading";
this.chkMultiThreading.Size = new System.Drawing.Size(124, 17);
this.chkMultiThreading.TabIndex = 33;
this.chkMultiThreading.Text = "Use Multi Threading ";
this.chkMultiThreading.UseVisualStyleBackColor = true;
this.chkMultiThreading.CheckedChanged += new System.EventHandler(this.chkMultiThreading_CheckedChanged);
//
// BinaryTextureImporterList
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -362,5 +376,6 @@
private Toolbox.Library.Forms.STLabel dataSizeLbl;
private Toolbox.Library.Forms.STLabel compModeLbl;
private Toolbox.Library.Forms.STCheckBox cnkGammaFix;
private Toolbox.Library.Forms.STCheckBox chkMultiThreading;
}
}

View file

@ -21,6 +21,8 @@ namespace FirstPlugin
public int SelectedIndex = -1;
public bool ForceMipCount = false;
public bool MultiThreading => chkMultiThreading.Checked;
public uint SelectedMipCount
{
set
@ -176,7 +178,7 @@ namespace FirstPlugin
pictureBox1.Image = bitmap;
var mips = setting.GenerateMipList(CompressionMode);
var mips = setting.GenerateMipList(CompressionMode, MultiThreading);
setting.DataBlockOutput.Clear();
setting.DataBlockOutput.Add(Utils.CombineByteArray(mips.ToArray()));
@ -331,5 +333,11 @@ namespace FirstPlugin
SetupSettings(SelectedTexSettings);
}
}
private void chkMultiThreading_CheckedChanged(object sender, EventArgs e) {
if (SelectedTexSettings != null) {
SetupSettings(SelectedTexSettings);
}
}
}
}

View file

@ -159,7 +159,7 @@ namespace FirstPlugin
}
public List<byte[]> GenerateMipList(STCompressionMode CompressionMode, int SurfaceLevel = 0)
public List<byte[]> GenerateMipList(STCompressionMode CompressionMode, bool multiThread, int SurfaceLevel = 0)
{
Bitmap Image = BitmapExtension.GetBitmap(DecompressedData[SurfaceLevel], (int)TexWidth, (int)TexHeight);
if (GammaFix)
@ -175,14 +175,14 @@ namespace FirstPlugin
Image = BitmapExtension.Resize(Image, MipWidth, MipHeight);
mipmaps.Add(STGenericTexture.CompressBlock(BitmapExtension.ImageToByte(Image),
Image.Width, Image.Height, TextureData.ConvertFormat(Format), alphaRef, CompressionMode));
Image.Width, Image.Height, TextureData.ConvertFormat(Format), alphaRef, multiThread, CompressionMode));
}
Image.Dispose();
return mipmaps;
}
public byte[] GenerateMips(STCompressionMode CompressionMode, int SurfaceLevel = 0)
public byte[] GenerateMips(STCompressionMode CompressionMode, bool multiThread, int SurfaceLevel = 0)
{
Bitmap Image = BitmapExtension.GetBitmap(DecompressedData[SurfaceLevel], (int)TexWidth, (int)TexHeight);
if (GammaFix)
@ -198,19 +198,19 @@ namespace FirstPlugin
Image = BitmapExtension.Resize(Image, MipWidth, MipHeight);
mipmaps.Add(STGenericTexture.CompressBlock(BitmapExtension.ImageToByte(Image),
Image.Width, Image.Height, TextureData.ConvertFormat(Format), alphaRef, CompressionMode));
Image.Width, Image.Height, TextureData.ConvertFormat(Format), alphaRef, multiThread, CompressionMode));
}
Image.Dispose();
return Utils.CombineByteArray(mipmaps.ToArray());
}
public void Compress(STCompressionMode CompressionMode)
public void Compress(STCompressionMode CompressionMode, bool multiThread)
{
DataBlockOutput.Clear();
foreach (var surface in DecompressedData)
{
DataBlockOutput.Add(STGenericTexture.CompressBlock(surface,
(int)TexWidth, (int)TexHeight, TextureData.ConvertFormat(Format), alphaRef, CompressionMode));
(int)TexWidth, (int)TexHeight, TextureData.ConvertFormat(Format), alphaRef, multiThread, CompressionMode));
}
}
public static uint DIV_ROUND_UP(uint value1, uint value2)

View file

@ -1314,7 +1314,7 @@ namespace Toolbox.Library
if (settings.GenerateMipmaps && !settings.IsFinishedCompressing)
{
settings.DataBlockOutput.Clear();
settings.DataBlockOutput.Add(settings.GenerateMips(importer.CompressionMode));
settings.DataBlockOutput.Add(settings.GenerateMips(importer.CompressionMode, importer.MultiThreading));
}
ApplySettings(settings);

View file

@ -433,7 +433,7 @@ namespace Toolbox.Library
return BitmapExtension.GetBitmap(Output, W * 4, H * 4);
}
public static unsafe byte[] CompressBlock(Byte[] data, int width, int height, DDS.DXGI_FORMAT format, float AlphaRef = 0.5f, STCompressionMode CompressionMode = STCompressionMode.Normal)
public static unsafe byte[] CompressBlock(Byte[] data, int width, int height, DDS.DXGI_FORMAT format, bool multiThread, float AlphaRef = 0.5f, STCompressionMode CompressionMode = STCompressionMode.Normal)
{
long inputRowPitch = width * 4;
long inputSlicePitch = width * height * 4;
@ -455,7 +455,9 @@ namespace Toolbox.Library
new DirectXTexNet.Image[] { inputImage }, texMetadata, null);
var compFlags = TEX_COMPRESS_FLAGS.DEFAULT;
// compFlags |= TEX_COMPRESS_FLAGS.PARALLEL;
if (multiThread)
compFlags |= TEX_COMPRESS_FLAGS.PARALLEL;
if (format == DDS.DXGI_FORMAT.DXGI_FORMAT_BC7_UNORM ||
format == DDS.DXGI_FORMAT.DXGI_FORMAT_BC7_UNORM_SRGB ||

View file

@ -17,6 +17,8 @@ namespace Toolbox.Library.Forms
public int SelectedIndex = -1;
public bool ForceMipCount = false;
public bool MultiThreading = false;
public uint SelectedMipCount
{
set
@ -126,7 +128,7 @@ namespace Toolbox.Library.Forms
pictureBox1.Image = bitmap;
var mips = SelectedTexSettings.GenerateMipList(CompressionMode);
var mips = SelectedTexSettings.GenerateMipList(CompressionMode, MultiThreading);
SelectedTexSettings.DataBlockOutput.Clear();
SelectedTexSettings.DataBlockOutput.Add(Utils.CombineByteArray(mips.ToArray()));

View file

@ -139,7 +139,7 @@ namespace Toolbox.Library.Forms
}
public List<byte[]> GenerateMipList(STCompressionMode CompressionMode, int SurfaceLevel = 0)
public List<byte[]> GenerateMipList(STCompressionMode CompressionMode, bool multiThread, int SurfaceLevel = 0)
{
Bitmap Image = BitmapExtension.GetBitmap(DecompressedData[SurfaceLevel], (int)TexWidth, (int)TexHeight);
@ -153,14 +153,14 @@ namespace Toolbox.Library.Forms
Image = BitmapExtension.Resize(Image, MipWidth, MipHeight);
mipmaps.Add(STGenericTexture.CompressBlock(BitmapExtension.ImageToByte(Image),
Image.Width, Image.Height, Format, alphaRef, CompressionMode));
Image.Width, Image.Height, Format, alphaRef, multiThread, CompressionMode));
}
Image.Dispose();
return mipmaps;
}
public byte[] GenerateMips(STCompressionMode CompressionMode, int SurfaceLevel = 0)
public byte[] GenerateMips(STCompressionMode CompressionMode, bool multiThread, int SurfaceLevel = 0)
{
Bitmap Image = BitmapExtension.GetBitmap(DecompressedData[SurfaceLevel], (int)TexWidth, (int)TexHeight);
@ -174,20 +174,20 @@ namespace Toolbox.Library.Forms
Image = BitmapExtension.Resize(Image, MipWidth, MipHeight);
mipmaps.Add(STGenericTexture.CompressBlock(BitmapExtension.ImageToByte(Image),
Image.Width, Image.Height, Format, alphaRef, CompressionMode));
Image.Width, Image.Height, Format, alphaRef, multiThread, CompressionMode));
}
Image.Dispose();
return Utils.CombineByteArray(mipmaps.ToArray());
}
public void Compress(STCompressionMode CompressionMode)
public void Compress(STCompressionMode CompressionMode, bool multiThread)
{
DataBlockOutput.Clear();
foreach (var surface in DecompressedData)
{
DataBlockOutput.Add(STGenericTexture.CompressBlock(surface,
(int)TexWidth, (int)TexHeight, Format, alphaRef, CompressionMode));
(int)TexWidth, (int)TexHeight, Format, alphaRef, multiThread, CompressionMode));
}
}
}

View file

@ -31,7 +31,7 @@ namespace Toolbox.Library
Height = (uint)height;
ImageData = DDSCompressor.CompressBlock(FileData, width, height,
DDS.DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM_SRGB);
DDS.DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, false);
}
public GenericBitmapTexture(string FileName) {

View file

@ -558,7 +558,7 @@ namespace Toolbox.Library
}
catch (Exception ex)
{
Forms.STErrorDialog.Show($"Texture failed to load!", "Texture [GetBitmap({MipLevel},{ArrayLevel})]", DebugInfo() + " \n" + ex);
/* Forms.STErrorDialog.Show($"Texture failed to load!", "Texture [GetBitmap({MipLevel},{ArrayLevel})]", DebugInfo() + " \n" + ex);
try
{
@ -567,7 +567,7 @@ namespace Toolbox.Library
catch
{
Forms.STErrorDialog.Show($"Texture failed to load!", "Texture [GetBitmap({MipLevel},{ArrayLevel})]", DebugInfo() + " \n" + ex);
}
}*/
return null;
}
@ -724,7 +724,7 @@ namespace Toolbox.Library
return MipmapNum;
}
public static byte[] GenerateMipsAndCompress(Bitmap bitmap, uint MipCount, TEX_FORMAT Format, float alphaRef = 0.5f, STCompressionMode CompressionMode = STCompressionMode.Fast)
public static byte[] GenerateMipsAndCompress(Bitmap bitmap, uint MipCount, TEX_FORMAT Format, bool multiThread = false, float alphaRef = 0.5f, STCompressionMode CompressionMode = STCompressionMode.Fast)
{
byte[] DecompressedData = BitmapExtension.ImageToByte(bitmap);
DecompressedData = ConvertBgraToRgba(DecompressedData);
@ -739,20 +739,20 @@ namespace Toolbox.Library
Image = BitmapExtension.Resize(Image, width, height);
mipmaps.Add(STGenericTexture.CompressBlock(BitmapExtension.ImageToByte(Image),
Image.Width, Image.Height, Format, alphaRef, CompressionMode));
Image.Width, Image.Height, Format, alphaRef, multiThread, CompressionMode));
}
Image.Dispose();
return Utils.CombineByteArray(mipmaps.ToArray());
}
public static byte[] CompressBlock(byte[] data, int width, int height, TEX_FORMAT format, float alphaRef, STCompressionMode CompressionMode = STCompressionMode.Fast)
public static byte[] CompressBlock(byte[] data, int width, int height, TEX_FORMAT format, float alphaRef, bool multiThread = false, STCompressionMode CompressionMode = STCompressionMode.Fast)
{
if (!Runtime.UseDirectXTexDecoder)
return data;
if (IsCompressed(format))
return DDSCompressor.CompressBlock(data, width, height, (DDS.DXGI_FORMAT)format, alphaRef, CompressionMode);
return DDSCompressor.CompressBlock(data, width, height, (DDS.DXGI_FORMAT)format, multiThread, alphaRef, CompressionMode);
else if (IsAtscFormat(format))
return null;
else