Skip to content

Commit 8a9b0e7

Browse files
committed
fit wip
1 parent d292b75 commit 8a9b0e7

File tree

59 files changed

+1598
-1514
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1598
-1514
lines changed

Dalamud/ImGuiScene/Helpers/D3D11RectangleDrawer.cs

Lines changed: 393 additions & 0 deletions
Large diffs are not rendered by default.

Dalamud/ImGuiScene/IImGuiRenderer.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Runtime.CompilerServices;
22

33
using Dalamud.Interface.Internal;
4+
using Dalamud.Interface.Textures.TextureWraps;
45

56
using ImGuiNET;
67

@@ -18,6 +19,9 @@ internal interface IImGuiRenderer : IDisposable
1819
/// <param name="drawCmd">The relevant draw command.</param>
1920
public delegate void DrawCmdUserCallbackDelegate(ImDrawDataPtr drawData, ImDrawCmdPtr drawCmd);
2021

22+
/// <summary>Gets the texture manager.</summary>
23+
ISceneTextureManager TextureManager { get; }
24+
2125
/// <summary>
2226
/// Notifies that the window is about to be resized.
2327
/// </summary>
@@ -70,22 +74,4 @@ internal interface IImGuiRenderer : IDisposable
7074
/// </summary>
7175
/// <param name="delegate">The delegate.</param>
7276
void RemoveDrawCmdUserCallback(DrawCmdUserCallbackDelegate @delegate);
73-
74-
/// <summary>
75-
/// Load an image from a span of bytes of specified format.
76-
/// </summary>
77-
/// <param name="data">The data to load.</param>
78-
/// <param name="pitch">The pitch(stride) in bytes.</param>
79-
/// <param name="width">The width in pixels.</param>
80-
/// <param name="height">The height in pixels.</param>
81-
/// <param name="format">Format of the texture.</param>
82-
/// <param name="debugName">Name for debugging.</param>
83-
/// <returns>A texture, ready to use in ImGui.</returns>
84-
IDalamudTextureWrap LoadTexture(
85-
ReadOnlySpan<byte> data,
86-
int pitch,
87-
int width,
88-
int height,
89-
int format,
90-
[CallerMemberName] string debugName = "");
9177
}

Dalamud/ImGuiScene/IImGuiScene.cs

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System.Runtime.CompilerServices;
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Runtime.CompilerServices;
23

34
using Dalamud.Interface.Internal;
5+
using Dalamud.Interface.Textures.TextureWraps;
46

57
namespace Dalamud.ImGuiScene;
68

@@ -40,19 +42,22 @@ internal interface IImGuiScene : IDisposable
4042
event NewRenderFrameDelegate? NewRenderFrame;
4143

4244
/// <summary>
43-
/// Gets or sets a value indicating whether or not the cursor should be overridden with the ImGui cursor.
45+
/// Gets or sets a value indicating whether the cursor should be overridden with the ImGui cursor.
4446
/// </summary>
45-
public bool UpdateCursor { get; set; }
47+
bool UpdateCursor { get; set; }
4648

4749
/// <summary>
4850
/// Gets or sets the path of ImGui configuration .ini file.
4951
/// </summary>
50-
public string? IniPath { get; set; }
52+
string? IniPath { get; set; }
5153

5254
/// <summary>
5355
/// Gets the device handle.
5456
/// </summary>
55-
public nint DeviceHandle { get; }
57+
nint DeviceHandle { get; }
58+
59+
/// <summary>Gets the texture manager.</summary>
60+
ISceneTextureManager TextureManager { get; }
5661

5762
/// <summary>
5863
/// Perform a render cycle.
@@ -77,47 +82,6 @@ internal interface IImGuiScene : IDisposable
7782
/// <remarks>Call this while handling <see cref="NewRenderFrame"/>.</remarks>
7883
void InvalidateFonts();
7984

80-
/// <summary>
81-
/// Check whether the current backend supports the given texture format.
82-
/// </summary>
83-
/// <param name="format">DXGI format to check.</param>
84-
/// <returns>Whether it is supported.</returns>
85-
public bool SupportsTextureFormat(int format);
86-
87-
/// <summary>
88-
/// Loads an image from a file.
89-
/// </summary>
90-
/// <param name="path">The path to file.</param>
91-
/// <param name="debugName">The debug name.</param>
92-
/// <returns>The loaded image.</returns>
93-
IDalamudTextureWrap CreateTexture2DFromFile(string path, [CallerMemberName] string debugName = "");
94-
95-
/// <summary>
96-
/// Loads an image from memory. The image must be in a contained format, such as .png, .jpg, and etc.
97-
/// </summary>
98-
/// <param name="data">The data of the image.</param>
99-
/// <param name="debugName">The debug name.</param>
100-
/// <returns>The loaded image.</returns>
101-
IDalamudTextureWrap CreateTexture2DFromBytes(ReadOnlySpan<byte> data, [CallerMemberName] string debugName = "");
102-
103-
/// <summary>
104-
/// Load an image from a span of bytes of specified format.
105-
/// </summary>
106-
/// <param name="data">The data to load.</param>
107-
/// <param name="pitch">The pitch(stride) in bytes.</param>
108-
/// <param name="width">The width in pixels.</param>
109-
/// <param name="height">The height in pixels.</param>
110-
/// <param name="format">Format of the texture.</param>
111-
/// <param name="debugName">The debug name.</param>
112-
/// <returns>A texture, ready to use in ImGui.</returns>
113-
IDalamudTextureWrap CreateTexture2DFromRaw(
114-
ReadOnlySpan<byte> data,
115-
int pitch,
116-
int width,
117-
int height,
118-
int format,
119-
[CallerMemberName] string debugName = "");
120-
12185
/// <inheritdoc cref="IImGuiRenderer.SetTexturePipeline"/>
12286
void SetTexturePipeline(IDalamudTextureWrap textureHandle, ITexturePipelineWrap? pipelineHandle);
12387

@@ -129,18 +93,18 @@ IDalamudTextureWrap CreateTexture2DFromRaw(
12993
/// </summary>
13094
/// <param name="cursorHandle">The cursor.</param>
13195
/// <returns>Whether it is the case.</returns>
132-
public bool IsImGuiCursor(nint cursorHandle);
96+
bool IsImGuiCursor(nint cursorHandle);
13397

13498
/// <summary>
13599
/// Determines if this instance of <see cref="IImGuiScene"/> is rendering to <paramref name="targetHandle"/>.
136100
/// </summary>
137101
/// <param name="targetHandle">The present target handle.</param>
138102
/// <returns>Whether it is the case.</returns>
139-
public bool IsAttachedToPresentationTarget(nint targetHandle);
103+
bool IsAttachedToPresentationTarget(nint targetHandle);
140104

141105
/// <summary>
142106
/// Determines if the main viewport is full screen.
143107
/// </summary>
144108
/// <returns>Whether it is the case.</returns>
145-
public bool IsMainViewportFullScreen();
109+
bool IsMainViewportFullScreen();
146110
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
4+
using Dalamud.Interface.Internal;
5+
using Dalamud.Interface.Textures;
6+
using Dalamud.Interface.Textures.TextureWraps;
7+
using Dalamud.Plugin.Internal.Types;
8+
9+
namespace Dalamud.ImGuiScene;
10+
11+
internal interface ISceneTextureManager
12+
{
13+
/// <summary>Creates a texture.</summary>
14+
/// <param name="data">Optional data to initialize the texture with.</param>
15+
/// <param name="specs">Texture specifications.</param>
16+
/// <param name="cpuRead">Whether to support reading from CPU, while disabling reading from GPU.</param>
17+
/// <param name="cpuWrite">Whether to support writing from CPU, while disabling writing from GPU.</param>
18+
/// <param name="immutable">Whether the texture should be immutable.</param>
19+
/// <param name="debugName">Name for debug display purposes.</param>
20+
/// <returns>A new empty texture.</returns>
21+
IDalamudTextureWrap Create(
22+
ReadOnlySpan<byte> data,
23+
RawImageSpecification specs,
24+
bool cpuRead,
25+
bool cpuWrite,
26+
bool immutable,
27+
string? debugName = null);
28+
29+
/// <summary>Creates a texture from the given existing texture, cropping and converting pixel format as needed.
30+
/// </summary>
31+
/// <param name="wrap">The source texture wrap. The passed value may be disposed once this function returns,
32+
/// without having to wait for the completion of the returned <see cref="Task{TResult}"/>.</param>
33+
/// <param name="args">The texture modification arguments.</param>
34+
/// <param name="debugName">Name for debug display purposes.</param>
35+
/// <param name="cancellationToken">The cancellation token.</param>
36+
/// <returns>A <see cref="Task{TResult}"/> containing the copied texture on success. Dispose after use.</returns>
37+
/// <remarks><para>This function may throw an exception.</para></remarks>
38+
Task<IDalamudTextureWrap> CreateFromExistingTextureAsync(
39+
IDalamudTextureWrap wrap,
40+
TextureModificationArgs args = default,
41+
string? debugName = null,
42+
CancellationToken cancellationToken = default);
43+
44+
/// <summary>Creates a texture from an ImGui viewport.</summary>
45+
/// <param name="args">The arguments for creating a texture.</param>
46+
/// <param name="ownerPlugin">Plugin that called the function.</param>
47+
/// <param name="debugName">Name for debug display purposes.</param>
48+
/// <param name="cancellationToken">The cancellation token.</param>
49+
/// <returns>A <see cref="Task{TResult}"/> containing the copied texture on success. Dispose after use.</returns>
50+
/// <remarks>
51+
/// <para>Use <c>ImGui.GetMainViewport().ID</c> to capture the game screen with Dalamud rendered.</para>
52+
/// <para>This function may throw an exception.</para>
53+
/// </remarks>
54+
Task<IDalamudTextureWrap> CreateFromImGuiViewportAsync(
55+
ImGuiViewportTextureArgs args,
56+
LocalPlugin? ownerPlugin,
57+
string? debugName = null,
58+
CancellationToken cancellationToken = default);
59+
60+
/// <summary>
61+
/// Determines whether the system supports the given DXGI format.
62+
/// For use with <see cref="RawImageSpecification.DxgiFormat"/>.
63+
/// </summary>
64+
/// <param name="dxgiFormat">The DXGI format.</param>
65+
/// <returns><c>true</c> if supported.</returns>
66+
/// <remarks><para>This function does not throw exceptions.</para></remarks>
67+
bool IsDxgiFormatSupported(int dxgiFormat);
68+
69+
/// <summary>Determines whether the system supports the given DXGI format for use with
70+
/// <see cref="CreateFromExistingTextureAsync"/>.</summary>
71+
/// <param name="dxgiFormat">The DXGI format.</param>
72+
/// <returns><c>true</c> if supported.</returns>
73+
/// <remarks><para>This function does not throw exceptions.</para></remarks>
74+
bool IsDxgiFormatSupportedForCreateFromExistingTextureAsync(int dxgiFormat);
75+
76+
/// <summary>Gets the raw data of a texture wrap.</summary>
77+
/// <param name="wrap">The source texture wrap.</param>
78+
/// <param name="args">The texture modification arguments.</param>
79+
/// <param name="cancellationToken">The cancellation token.</param>
80+
/// <returns>The raw data and its specifications.</returns>
81+
/// <remarks>
82+
/// <para>The length of the returned <c>RawData</c> may not match
83+
/// <see cref="RawImageSpecification.Height"/> * <see cref="RawImageSpecification.Pitch"/>.</para>
84+
/// <para>This function may throw an exception.</para>
85+
/// </remarks>
86+
Task<(RawImageSpecification Specification, byte[] RawData)> GetRawImageAsync(
87+
IDalamudTextureWrap wrap,
88+
TextureModificationArgs args = default,
89+
CancellationToken cancellationToken = default);
90+
}

Dalamud/ImGuiScene/Implementations/Dx11Renderer.TextureData.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Runtime.InteropServices;
44

55
using Dalamud.ImGuiScene.Helpers;
6-
using Dalamud.Interface.Internal;
6+
using Dalamud.Interface.Textures.TextureWraps;
77

88
using TerraFX.Interop;
99
using TerraFX.Interop.DirectX;
@@ -27,21 +27,31 @@ private class TextureData : ManagedComObjectBase<TextureData>, INativeGuid
2727
public static readonly Guid MyGuid =
2828
new(0x72fe3f82, 0x3ffc, 0x4be9, 0xb0, 0x08, 0x4a, 0xef, 0x7a, 0x94, 0x2f, 0x55);
2929

30+
private ComPtr<ID3D11Texture2D> tex;
3031
private ComPtr<ID3D11ShaderResourceView> srv;
3132
private TexturePipeline? customPipeline;
3233

33-
public TextureData(ID3D11ShaderResourceView* srv, int width, int height)
34+
public TextureData(
35+
ComPtr<ID3D11Texture2D> tex,
36+
ComPtr<ID3D11ShaderResourceView> srv,
37+
int width,
38+
int height,
39+
DXGI_FORMAT format)
3440
{
35-
this.srv = new(srv);
41+
this.tex = tex;
42+
this.srv = srv;
3643
this.Width = width;
3744
this.Height = height;
45+
this.Format = format;
3846
}
3947

4048
public static Guid* NativeGuid => (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in MyGuid));
4149

42-
public int Width { get; private init; }
50+
public int Width { get; }
4351

44-
public int Height { get; private init; }
52+
public int Height { get; }
53+
54+
public DXGI_FORMAT Format { get; }
4555

4656
public TexturePipeline? CustomPipeline
4757
{
@@ -55,13 +65,16 @@ public TexturePipeline? CustomPipeline
5565
}
5666
}
5767

58-
public ID3D11ShaderResourceView* ShaderResourceView => this.srv;
68+
public ComPtr<ID3D11Texture2D> Texture2D => this.tex;
69+
70+
public ComPtr<ID3D11ShaderResourceView> ShaderResourceView => this.srv;
5971

6072
protected override void* DynamicCast(in Guid iid) =>
6173
iid == MyGuid ? this.AsComInterface() : base.DynamicCast(iid);
6274

6375
protected override void FinalRelease()
6476
{
77+
this.tex.Reset();
6578
this.srv.Reset();
6679
this.customPipeline?.Dispose();
6780
this.customPipeline = null;
@@ -86,6 +99,8 @@ private class TextureWrap : IDalamudTextureWrap, ICloneable
8699

87100
public int Height => this.Data.Height;
88101

102+
public int DxgiFormat => (int)this.Data.Format;
103+
89104
public static TextureWrap TakeOwnership(TextureData data) => new(data);
90105

91106
public static TextureWrap NewReference(TextureData data) => new(data.CloneRef());

0 commit comments

Comments
 (0)