-
Notifications
You must be signed in to change notification settings - Fork 5
/
ImGuiSystem.cs
391 lines (320 loc) · 13.6 KB
/
ImGuiSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
using Hexa.NET.ImGui;
using Stride.Core;
using Stride.Core.Annotations;
using Stride.Core.Mathematics;
using Stride.Games;
using Stride.Graphics;
using Stride.Input;
using Stride.Profiling;
using Stride.Rendering;
using System.Collections.Generic;
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StrideCommunity.ImGuiDebug;
public class ImGuiSystem : GameSystemBase
{
public readonly ImGuiContextPtr ImGuiContext;
public float Scale
{
get => _scale;
set
{
_scale = value;
CreateFontTexture();
}
}
private float _scale = 1;
const int INITIAL_VERTEX_BUFFER_SIZE = 128;
const int INITIAL_INDEX_BUFFER_SIZE = 128;
private ImGuiIOPtr _io;
// dependencies
readonly InputManager input;
readonly GraphicsDevice device;
readonly GraphicsDeviceManager deviceManager;
readonly GraphicsContext context;
readonly EffectSystem effectSystem;
readonly DebugTextSystem debug;
CommandList commandList;
// device objects
PipelineState imPipeline;
VertexDeclaration imVertLayout;
VertexBufferBinding vertexBinding;
IndexBufferBinding indexBinding;
EffectInstance imShader;
Texture fontTexture;
private Dictionary<Keys, ImGuiKey> _keys = [];
public ImGuiSystem([NotNull] IServiceRegistry registry, [NotNull] GraphicsDeviceManager graphicsDeviceManager, InputManager inputManager = null) : base(registry)
{
input = inputManager ?? Services.GetService<InputManager>();
Debug.Assert(input != null, "ImGuiSystem: InputManager must be available!");
deviceManager = graphicsDeviceManager;
Debug.Assert(deviceManager != null, "ImGuiSystem: GraphicsDeviceManager must be available!");
device = deviceManager.GraphicsDevice;
Debug.Assert(device != null, "ImGuiSystem: GraphicsDevice must be available!");
context = Services.GetService<GraphicsContext>();
Debug.Assert(context != null, "ImGuiSystem: GraphicsContext must be available!");
effectSystem = Services.GetService<EffectSystem>();
Debug.Assert(effectSystem != null, "ImGuiSystem: EffectSystem must be available!");
ImGuiContext = ImGui.CreateContext();
ImGui.SetCurrentContext(ImGuiContext);
_io = ImGui.GetIO();
// SETTO
SetupInput();
// vbos etc
CreateDeviceObjects();
// font stuff
CreateFontTexture();
Enabled = true; // Force Update functions to be run
Visible = true; // Force Draw related functions to be run
UpdateOrder = 1; // Update should occur after Stride's InputManager
// Include this new instance into our services and systems so that stride fires our functions automatically
Services.AddService(this);
Game.GameSystems.Add(this);
}
protected override void Destroy()
{
ImGui.DestroyContext(ImGuiContext);
base.Destroy();
}
unsafe void SetupInput()
{
// keyboard nav yes
_io.ConfigFlags |= ImGuiConfigFlags.NavEnableKeyboard;
_keys.Add(Keys.Tab, ImGuiKey.Tab);
_keys.Add(Keys.Left, ImGuiKey.LeftArrow);
_keys.Add(Keys.Right, ImGuiKey.RightArrow);
_keys.Add(Keys.Up, ImGuiKey.UpArrow);
_keys.Add(Keys.Down, ImGuiKey.DownArrow);
_keys.Add(Keys.PageUp, ImGuiKey.PageUp);
_keys.Add(Keys.PageDown, ImGuiKey.PageDown);
_keys.Add(Keys.Home, ImGuiKey.Home);
_keys.Add(Keys.End, ImGuiKey.End);
_keys.Add(Keys.Delete, ImGuiKey.Delete);
_keys.Add(Keys.Back, ImGuiKey.Backspace);
_keys.Add(Keys.Enter, ImGuiKey.Enter);
_keys.Add(Keys.Escape, ImGuiKey.Escape);
_keys.Add(Keys.Space, ImGuiKey.Space);
_keys.Add(Keys.A, ImGuiKey.A);
_keys.Add(Keys.C, ImGuiKey.C);
_keys.Add(Keys.V, ImGuiKey.V);
_keys.Add(Keys.X, ImGuiKey.X);
_keys.Add(Keys.Y, ImGuiKey.Y);
_keys.Add(Keys.Z, ImGuiKey.Z);
setClipboardFn = SetClipboard;
getClipboardFn = GetClipboard;
_io.SetClipboardTextFn = (void*)Marshal.GetFunctionPointerForDelegate(setClipboardFn);
_io.GetClipboardTextFn = (void*)Marshal.GetFunctionPointerForDelegate(getClipboardFn);
}
[FixedAddressValueType]
static SetClipboardDelegate setClipboardFn;
[FixedAddressValueType]
static GetClipboardDelegate getClipboardFn;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate void SetClipboardDelegate(IntPtr data);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate IntPtr GetClipboardDelegate();
void SetClipboard(IntPtr data)
{
}
unsafe IntPtr GetClipboard()
{
return (nint)_io.ClipboardUserData;
}
void CreateDeviceObjects()
{
// set up a commandlist
commandList = context.CommandList;
// compile de shader
imShader = new EffectInstance(effectSystem.LoadEffect("ImGuiShader").WaitForResult());
imShader.UpdateEffect(device);
var layout = new VertexDeclaration(
VertexElement.Position<Vector2>(),
VertexElement.TextureCoordinate<Vector2>(),
VertexElement.Color(PixelFormat.R8G8B8A8_UNorm)
);
imVertLayout = layout;
// de pipeline desc
var pipeline = new PipelineStateDescription()
{
BlendState = BlendStates.NonPremultiplied,
RasterizerState = new RasterizerStateDescription()
{
CullMode = CullMode.None,
DepthBias = 0,
FillMode = FillMode.Solid,
MultisampleAntiAliasLine = false,
ScissorTestEnable = true,
SlopeScaleDepthBias = 0,
},
PrimitiveType = PrimitiveType.TriangleList,
InputElements = imVertLayout.CreateInputElements(),
DepthStencilState = DepthStencilStates.Default,
EffectBytecode = imShader.Effect.Bytecode,
RootSignature = imShader.RootSignature,
Output = new RenderOutputDescription(PixelFormat.R8G8B8A8_UNorm)
};
// finally set up the pipeline
var pipelineState = PipelineState.New(device, ref pipeline);
imPipeline = pipelineState;
var is32Bits = false;
var indexBuffer = Stride.Graphics.Buffer.Index.New(device, INITIAL_INDEX_BUFFER_SIZE * sizeof(ushort), GraphicsResourceUsage.Dynamic);
var indexBufferBinding = new IndexBufferBinding(indexBuffer, is32Bits, 0);
indexBinding = indexBufferBinding;
var vertexBuffer = Stride.Graphics.Buffer.Vertex.New(device, INITIAL_VERTEX_BUFFER_SIZE * imVertLayout.CalculateSize(), GraphicsResourceUsage.Dynamic);
var vertexBufferBinding = new VertexBufferBinding(vertexBuffer, layout, 0);
vertexBinding = vertexBufferBinding;
}
unsafe void CreateFontTexture()
{
_io.Fonts.Clear();
// font data, important
var text = _io.Fonts.AddFontDefault();
text.Scale = Scale;
byte* pixelData;
int width;
int height;
int bytesPerPixel;
_io.Fonts.GetTexDataAsRGBA32(&pixelData, &width, &height, &bytesPerPixel);
var newFontTexture = Texture.New2D(device, width, height, PixelFormat.R8G8B8A8_UNorm, TextureFlags.ShaderResource);
newFontTexture.SetData(commandList, new DataPointer(pixelData, (width * height) * bytesPerPixel));
fontTexture = newFontTexture;
}
public override void Update(GameTime gameTime)
{
var surfaceSize = Game.Window.ClientBounds;
_io.DisplaySize = new System.Numerics.Vector2(surfaceSize.Width, surfaceSize.Height);
_io.DeltaTime = (float)gameTime.TimePerFrame.TotalSeconds;
if (input.HasMouse == false || input.IsMousePositionLocked == false)
{
var mousePos = input.AbsoluteMousePosition;
_io.MousePos = new System.Numerics.Vector2(mousePos.X, mousePos.Y);
if (_io.WantTextInput)
{
input.TextInput.EnabledTextInput();
}
else
{
input.TextInput.DisableTextInput();
}
// handle input events
foreach (InputEvent ev in input.Events)
{
switch (ev)
{
case TextInputEvent tev:
if (tev.Text == "\t") continue;
_io.AddInputCharactersUTF8(tev.Text);
break;
case KeyEvent kev:
if (_keys.TryGetValue(kev.Key, out var imGuiKey))
_io.AddKeyEvent(imGuiKey, input.IsKeyDown(kev.Key));
break;
case MouseWheelEvent mw:
_io.MouseWheel += mw.WheelDelta;
break;
}
}
var mouseDown = _io.MouseDown;
mouseDown[0] = input.IsMouseButtonDown(MouseButton.Left);
mouseDown[1] = input.IsMouseButtonDown(MouseButton.Right);
mouseDown[2] = input.IsMouseButtonDown(MouseButton.Middle);
_io.KeyAlt = input.IsKeyDown(Keys.LeftAlt) || input.IsKeyDown(Keys.RightAlt);
_io.KeyShift = input.IsKeyDown(Keys.LeftShift) || input.IsKeyDown(Keys.RightShift);
_io.KeyCtrl = input.IsKeyDown(Keys.LeftCtrl) || input.IsKeyDown(Keys.RightCtrl);
_io.KeySuper = input.IsKeyDown(Keys.LeftWin) || input.IsKeyDown(Keys.RightWin);
}
ImGui.NewFrame();
}
public override void EndDraw()
{
ImGui.Render();
RenderDrawLists(ImGui.GetDrawData());
ImGuiExtension.ClearTextures();
}
void CheckBuffers(ImDrawDataPtr drawData)
{
uint totalVBOSize = (uint)(drawData.TotalVtxCount * Unsafe.SizeOf<ImDrawVert>());
if (totalVBOSize > vertexBinding.Buffer.SizeInBytes)
{
var vertexBuffer = Stride.Graphics.Buffer.Vertex.New(device, (int)(totalVBOSize * 1.5f));
vertexBinding = new VertexBufferBinding(vertexBuffer, imVertLayout, 0);
}
uint totalIBOSize = (uint)(drawData.TotalIdxCount * sizeof(ushort));
if (totalIBOSize > indexBinding.Buffer.SizeInBytes)
{
var is32Bits = false;
var indexBuffer = Stride.Graphics.Buffer.Index.New(device, (int)(totalIBOSize * 1.5f));
indexBinding = new IndexBufferBinding(indexBuffer, is32Bits, 0);
}
}
unsafe void UpdateBuffers(ImDrawDataPtr drawData)
{
// copy de dators
int vtxOffsetBytes = 0;
int idxOffsetBytes = 0;
for (int n = 0; n < drawData.CmdListsCount; n++)
{
ImDrawListPtr cmdList = drawData.CmdLists[n];
vertexBinding.Buffer.SetData(commandList, new DataPointer(cmdList.VtxBuffer.Data, cmdList.VtxBuffer.Size * Unsafe.SizeOf<ImDrawVert>()), vtxOffsetBytes);
indexBinding.Buffer.SetData(commandList, new DataPointer(cmdList.IdxBuffer.Data, cmdList.IdxBuffer.Size * sizeof(ushort)), idxOffsetBytes);
vtxOffsetBytes += cmdList.VtxBuffer.Size * Unsafe.SizeOf<ImDrawVert>();
idxOffsetBytes += cmdList.IdxBuffer.Size * sizeof(ushort);
}
}
void RenderDrawLists(ImDrawDataPtr drawData)
{
// view proj
var surfaceSize = Game.Window.ClientBounds;
var projMatrix = Matrix.OrthoRH(surfaceSize.Width, -surfaceSize.Height, -1, 1);
CheckBuffers(drawData); // potentially resize buffers first if needed
UpdateBuffers(drawData); // updeet em now
// set pipeline stuff
var is32Bits = false;
commandList.SetPipelineState(imPipeline);
commandList.SetVertexBuffer(0, vertexBinding.Buffer, 0, Unsafe.SizeOf<ImDrawVert>());
commandList.SetIndexBuffer(indexBinding.Buffer, 0, is32Bits);
imShader.Parameters.Set(ImGuiShaderKeys.tex, fontTexture);
int vtxOffset = 0;
int idxOffset = 0;
for (int n = 0; n < drawData.CmdListsCount; n++)
{
ImDrawListPtr cmdList = drawData.CmdLists[n];
for (int i = 0; i < cmdList.CmdBuffer.Size; i++)
{
ImDrawCmd cmd = cmdList.CmdBuffer[i];
// Bind the appropriate texture based on cmd.TextureId
if (cmd.TextureId != IntPtr.Zero)
{
// Convert the IntPtr to the correct texture resource
if (ImGuiExtension.TryGetTexture(cmd.TextureId.Handle, out var texture))
{
imShader.Parameters.Set(ImGuiShaderKeys.tex, texture);
}
}
else
{
// If no specific texture, use the default font texture
imShader.Parameters.Set(ImGuiShaderKeys.tex, fontTexture);
}
// Set the scissor rectangle for clipping
commandList.SetScissorRectangle(
new Rectangle(
(int)cmd.ClipRect.X,
(int)cmd.ClipRect.Y,
(int)(cmd.ClipRect.Z - cmd.ClipRect.X),
(int)(cmd.ClipRect.W - cmd.ClipRect.Y)
)
);
// Set the projection matrix and apply shader
imShader.Parameters.Set(ImGuiShaderKeys.proj, ref projMatrix);
imShader.Apply(context);
// Draw the indexed vertices
commandList.DrawIndexed((int)cmd.ElemCount, idxOffset, vtxOffset);
idxOffset += (int)cmd.ElemCount;
}
vtxOffset += cmdList.VtxBuffer.Size;
}
}
}