From 9ba0a297c9f5c6905dd5fe07e8ba72d8bf659c8f Mon Sep 17 00:00:00 2001 From: Soreepeong Date: Tue, 5 Mar 2024 01:53:31 +0900 Subject: [PATCH] Add texture vram usage estimation --- .../Windows/Data/Widgets/TexWidget.cs | 20 +++++++++++++++--- .../Textures/Internal/TextureManager.Wic.cs | 4 ++-- .../Textures/Internal/TextureManager.cs | 2 +- .../Textures/RawImageSpecification.cs | 21 +++++++++++++++++-- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/Dalamud/Interface/Internal/Windows/Data/Widgets/TexWidget.cs b/Dalamud/Interface/Internal/Windows/Data/Widgets/TexWidget.cs index d14aeb5ab..58a2b6b6e 100644 --- a/Dalamud/Interface/Internal/Windows/Data/Widgets/TexWidget.cs +++ b/Dalamud/Interface/Internal/Windows/Data/Widgets/TexWidget.cs @@ -309,7 +309,7 @@ internal class TexWidget : IDataWindowWidget conf.QueueSave(); } - if (!ImGui.BeginTable("##table", 5)) + if (!ImGui.BeginTable("##table", 6)) return; const int numIcons = 1; @@ -318,12 +318,21 @@ internal class TexWidget : IDataWindowWidget iconWidths = ImGui.CalcTextSize(FontAwesomeIcon.Save.ToIconString()).X; ImGui.TableSetupScrollFreeze(0, 1); - ImGui.TableSetupColumn("Size", ImGuiTableColumnFlags.WidthFixed, ImGui.CalcTextSize("00000x00000").X); + ImGui.TableSetupColumn( + "Dimensions", + ImGuiTableColumnFlags.WidthFixed, + ImGui.CalcTextSize("00000x00000").X); ImGui.TableSetupColumn( "Format", ImGuiTableColumnFlags.WidthFixed, ImGui.CalcTextSize("R32G32B32A32_TYPELESS").X); - ImGui.TableSetupColumn("Name", ImGuiTableColumnFlags.WidthStretch); + ImGui.TableSetupColumn( + "Size", + ImGuiTableColumnFlags.WidthFixed, + ImGui.CalcTextSize("123.45 MB").X); + ImGui.TableSetupColumn( + "Name", + ImGuiTableColumnFlags.WidthStretch); ImGui.TableSetupColumn( "Actions", ImGuiTableColumnFlags.WidthFixed, @@ -353,6 +362,11 @@ internal class TexWidget : IDataWindowWidget ImGui.TableNextColumn(); ImGui.TextUnformatted(Enum.GetName(wrap.Format)?[12..] ?? wrap.Format.ToString()); + ImGui.TableNextColumn(); + var rawSpec = new RawImageSpecification(wrap.Width, wrap.Height, (int)wrap.Format, 0); + var bytes = rawSpec.EstimatedBytes; + ImGui.TextUnformatted(bytes < 0 ? "-" : Util.FormatBytes(bytes)); + ImGui.TableNextColumn(); ImGui.TextUnformatted(wrap.Name); diff --git a/Dalamud/Interface/Textures/Internal/TextureManager.Wic.cs b/Dalamud/Interface/Textures/Internal/TextureManager.Wic.cs index e59a3a1f2..bc10e5b77 100644 --- a/Dalamud/Interface/Textures/Internal/TextureManager.Wic.cs +++ b/Dalamud/Interface/Textures/Internal/TextureManager.Wic.cs @@ -485,7 +485,7 @@ internal sealed partial class TextureManager IReadOnlyDictionary? props = null, CancellationToken cancellationToken = default) { - if (!GetCorrespondingWicPixelFormat((DXGI_FORMAT)specs.DxgiFormat, out var inPixelFormat, out var srgb)) + if (!GetCorrespondingWicPixelFormat(specs.Format, out var inPixelFormat, out var srgb)) throw new NotSupportedException("DXGI_FORMAT from specs is not supported by WIC."); using var encoder = default(ComPtr); @@ -494,7 +494,7 @@ internal sealed partial class TextureManager cancellationToken.ThrowIfCancellationRequested(); // See: DirectXTK/Src/ScreenGrab.cpp - var outPixelFormat = (DXGI_FORMAT)specs.DxgiFormat switch + var outPixelFormat = specs.Format switch { DXGI_FORMAT.DXGI_FORMAT_R32G32B32A32_FLOAT => GUID.GUID_WICPixelFormat128bppRGBAFloat, DXGI_FORMAT.DXGI_FORMAT_R16G16B16A16_FLOAT when !this.wicFactory2.IsEmpty() => diff --git a/Dalamud/Interface/Textures/Internal/TextureManager.cs b/Dalamud/Interface/Textures/Internal/TextureManager.cs index 8fa9efa25..f69f01877 100644 --- a/Dalamud/Interface/Textures/Internal/TextureManager.cs +++ b/Dalamud/Interface/Textures/Internal/TextureManager.cs @@ -242,7 +242,7 @@ internal sealed partial class TextureManager Height = (uint)specs.Height, MipLevels = 1, ArraySize = 1, - Format = (DXGI_FORMAT)specs.DxgiFormat, + Format = specs.Format, SampleDesc = new(1, 0), Usage = D3D11_USAGE.D3D11_USAGE_IMMUTABLE, BindFlags = (uint)D3D11_BIND_FLAG.D3D11_BIND_SHADER_RESOURCE, diff --git a/Dalamud/Interface/Textures/RawImageSpecification.cs b/Dalamud/Interface/Textures/RawImageSpecification.cs index 2abf3729d..9ac649812 100644 --- a/Dalamud/Interface/Textures/RawImageSpecification.cs +++ b/Dalamud/Interface/Textures/RawImageSpecification.cs @@ -51,10 +51,27 @@ public record struct RawImageSpecification /// Gets the number of bits per pixel. /// Thrown if is not supported. public int BitsPerPixel => - GetFormatInfo((DXGI_FORMAT)this.DxgiFormat, out var bitsPerPixel, out _) + GetFormatInfo(this.Format, out var bitsPerPixel, out _) ? bitsPerPixel : throw new NotSupportedException(FormatNotSupportedMessage); + /// Gets or sets the format (typed). + internal DXGI_FORMAT Format + { + get => (DXGI_FORMAT)this.DxgiFormat; + set => this.DxgiFormat = (int)value; + } + + /// Gets the estimated number of bytes. + /// -1 if failed. + internal int EstimatedBytes => + GetFormatInfo(this.Format, out var bitsPerPixel, out var isBlockCompression) + ? isBlockCompression + ? (((Math.Max(1, (this.Width + 3) / 4) * 2 * bitsPerPixel) + 63) / 64) * 64 * + Math.Max(1, (this.Height + 3) / 4) + : (((((bitsPerPixel * this.Width) + 7) / 8) + 63) / 64) * 64 * this.Height + : -1; + /// /// Creates a new instance of record using the given resolution, /// in B8G8R8A8(BGRA32) UNorm pixel format. @@ -230,7 +247,7 @@ public record struct RawImageSpecification case DXGI_FORMAT.DXGI_FORMAT_B4G4R4A4_UNORM: bitsPerPixel = 16; isBlockCompression = true; - return false; + return true; default: bitsPerPixel = 0; isBlockCompression = false;