-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathImGuiController.cs
575 lines (506 loc) · 22.3 KB
/
ImGuiController.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Reflection;
using System.IO;
using Veldrid;
using System.Runtime.CompilerServices;
namespace ImGuiNET
{
/// <summary>
/// A modified version of Veldrid.ImGui's ImGuiRenderer.
/// Manages input for ImGui and handles rendering ImGui's DrawLists with Veldrid.
/// </summary>
public class ImGuiController : IDisposable
{
private GraphicsDevice _gd;
private bool _frameBegun;
// Veldrid objects
private DeviceBuffer _vertexBuffer;
private DeviceBuffer _indexBuffer;
private DeviceBuffer _projMatrixBuffer;
private Texture _fontTexture;
private TextureView _fontTextureView;
private Shader _vertexShader;
private Shader _fragmentShader;
private ResourceLayout _layout;
private ResourceLayout _textureLayout;
private Pipeline _pipeline;
private ResourceSet _mainResourceSet;
private ResourceSet _fontTextureResourceSet;
private IntPtr _fontAtlasID = (IntPtr)1;
private bool _controlDown;
private bool _shiftDown;
private bool _altDown;
private bool _winKeyDown;
private int _windowWidth;
private int _windowHeight;
private Vector2 _scaleFactor = Vector2.One;
// Image trackers
private readonly Dictionary<TextureView, ResourceSetInfo> _setsByView
= new Dictionary<TextureView, ResourceSetInfo>();
private readonly Dictionary<Texture, TextureView> _autoViewsByTexture
= new Dictionary<Texture, TextureView>();
private readonly Dictionary<IntPtr, ResourceSetInfo> _viewsById = new Dictionary<IntPtr, ResourceSetInfo>();
private readonly List<IDisposable> _ownedResources = new List<IDisposable>();
private int _lastAssignedID = 100;
/// <summary>
/// Constructs a new ImGuiController.
/// </summary>
public ImGuiController(GraphicsDevice gd, OutputDescription outputDescription, int width, int height)
{
_gd = gd;
_windowWidth = width;
_windowHeight = height;
IntPtr context = ImGui.CreateContext();
ImGui.SetCurrentContext(context);
var fonts = ImGui.GetIO().Fonts;
ImGui.GetIO().Fonts.AddFontDefault();
ImGui.GetIO().BackendFlags |= ImGuiBackendFlags.RendererHasVtxOffset;
CreateDeviceResources(gd, outputDescription);
SetKeyMappings();
SetPerFrameImGuiData(1f / 60f);
ImGui.NewFrame();
_frameBegun = true;
}
public void WindowResized(int width, int height)
{
_windowWidth = width;
_windowHeight = height;
}
public void DestroyDeviceObjects()
{
Dispose();
}
public void CreateDeviceResources(GraphicsDevice gd, OutputDescription outputDescription)
{
_gd = gd;
ResourceFactory factory = gd.ResourceFactory;
_vertexBuffer = factory.CreateBuffer(new BufferDescription(10000, BufferUsage.VertexBuffer | BufferUsage.Dynamic));
_vertexBuffer.Name = "ImGui.NET Vertex Buffer";
_indexBuffer = factory.CreateBuffer(new BufferDescription(2000, BufferUsage.IndexBuffer | BufferUsage.Dynamic));
_indexBuffer.Name = "ImGui.NET Index Buffer";
RecreateFontDeviceTexture(gd);
_projMatrixBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic));
_projMatrixBuffer.Name = "ImGui.NET Projection Buffer";
byte[] vertexShaderBytes = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-vertex", ShaderStages.Vertex);
byte[] fragmentShaderBytes = LoadEmbeddedShaderCode(gd.ResourceFactory, "imgui-frag", ShaderStages.Fragment);
_vertexShader = factory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytes, gd.BackendType == GraphicsBackend.Metal ? "VS" : "main"));
_fragmentShader = factory.CreateShader(new ShaderDescription(ShaderStages.Fragment, fragmentShaderBytes, gd.BackendType == GraphicsBackend.Metal ? "FS" : "main"));
VertexLayoutDescription[] vertexLayouts = new VertexLayoutDescription[]
{
new VertexLayoutDescription(
new VertexElementDescription("in_position", VertexElementSemantic.Position, VertexElementFormat.Float2),
new VertexElementDescription("in_texCoord", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2),
new VertexElementDescription("in_color", VertexElementSemantic.Color, VertexElementFormat.Byte4_Norm))
};
_layout = factory.CreateResourceLayout(new ResourceLayoutDescription(
new ResourceLayoutElementDescription("ProjectionMatrixBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex),
new ResourceLayoutElementDescription("MainSampler", ResourceKind.Sampler, ShaderStages.Fragment)));
_textureLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(
new ResourceLayoutElementDescription("MainTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment)));
GraphicsPipelineDescription pd = new GraphicsPipelineDescription(
BlendStateDescription.SingleAlphaBlend,
new DepthStencilStateDescription(false, false, ComparisonKind.Always),
new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, false, true),
PrimitiveTopology.TriangleList,
new ShaderSetDescription(vertexLayouts, new[] { _vertexShader, _fragmentShader }),
new ResourceLayout[] { _layout, _textureLayout },
outputDescription,
ResourceBindingModel.Default);
_pipeline = factory.CreateGraphicsPipeline(ref pd);
_mainResourceSet = factory.CreateResourceSet(new ResourceSetDescription(_layout,
_projMatrixBuffer,
gd.PointSampler));
_fontTextureResourceSet = factory.CreateResourceSet(new ResourceSetDescription(_textureLayout, _fontTextureView));
}
/// <summary>
/// Gets or creates a handle for a texture to be drawn with ImGui.
/// Pass the returned handle to Image() or ImageButton().
/// </summary>
public IntPtr GetOrCreateImGuiBinding(ResourceFactory factory, TextureView textureView)
{
if (!_setsByView.TryGetValue(textureView, out ResourceSetInfo rsi))
{
ResourceSet resourceSet = factory.CreateResourceSet(new ResourceSetDescription(_textureLayout, textureView));
rsi = new ResourceSetInfo(GetNextImGuiBindingID(), resourceSet);
_setsByView.Add(textureView, rsi);
_viewsById.Add(rsi.ImGuiBinding, rsi);
_ownedResources.Add(resourceSet);
}
return rsi.ImGuiBinding;
}
private IntPtr GetNextImGuiBindingID()
{
int newID = _lastAssignedID++;
return (IntPtr)newID;
}
/// <summary>
/// Gets or creates a handle for a texture to be drawn with ImGui.
/// Pass the returned handle to Image() or ImageButton().
/// </summary>
public IntPtr GetOrCreateImGuiBinding(ResourceFactory factory, Texture texture)
{
if (!_autoViewsByTexture.TryGetValue(texture, out TextureView textureView))
{
textureView = factory.CreateTextureView(texture);
_autoViewsByTexture.Add(texture, textureView);
_ownedResources.Add(textureView);
}
return GetOrCreateImGuiBinding(factory, textureView);
}
/// <summary>
/// Retrieves the shader texture binding for the given helper handle.
/// </summary>
public ResourceSet GetImageResourceSet(IntPtr imGuiBinding)
{
if (!_viewsById.TryGetValue(imGuiBinding, out ResourceSetInfo tvi))
{
throw new InvalidOperationException("No registered ImGui binding with id " + imGuiBinding.ToString());
}
return tvi.ResourceSet;
}
public void ClearCachedImageResources()
{
foreach (IDisposable resource in _ownedResources)
{
resource.Dispose();
}
_ownedResources.Clear();
_setsByView.Clear();
_viewsById.Clear();
_autoViewsByTexture.Clear();
_lastAssignedID = 100;
}
private byte[] LoadEmbeddedShaderCode(ResourceFactory factory, string name, ShaderStages stage)
{
switch (factory.BackendType)
{
case GraphicsBackend.Direct3D11:
{
string resourceName = name + ".hlsl.bytes";
return GetEmbeddedResourceBytes(resourceName);
}
case GraphicsBackend.OpenGL:
{
string resourceName = name + ".glsl";
return GetEmbeddedResourceBytes(resourceName);
}
case GraphicsBackend.Vulkan:
{
string resourceName = name + ".spv";
return GetEmbeddedResourceBytes(resourceName);
}
case GraphicsBackend.Metal:
{
string resourceName = name + ".metallib";
return GetEmbeddedResourceBytes(resourceName);
}
default:
throw new NotImplementedException();
}
}
private byte[] GetEmbeddedResourceBytes(string resourceName)
{
Assembly assembly = typeof(ImGuiController).Assembly;
using (Stream s = assembly.GetManifestResourceStream(resourceName))
{
byte[] ret = new byte[s.Length];
s.Read(ret, 0, (int)s.Length);
return ret;
}
}
/// <summary>
/// Recreates the device texture used to render text.
/// </summary>
public void RecreateFontDeviceTexture(GraphicsDevice gd)
{
ImGuiIOPtr io = ImGui.GetIO();
// Build
IntPtr pixels;
int width, height, bytesPerPixel;
io.Fonts.GetTexDataAsRGBA32(out pixels, out width, out height, out bytesPerPixel);
// Store our identifier
io.Fonts.SetTexID(_fontAtlasID);
_fontTexture = gd.ResourceFactory.CreateTexture(TextureDescription.Texture2D(
(uint)width,
(uint)height,
1,
1,
PixelFormat.R8_G8_B8_A8_UNorm,
TextureUsage.Sampled));
_fontTexture.Name = "ImGui.NET Font Texture";
gd.UpdateTexture(
_fontTexture,
pixels,
(uint)(bytesPerPixel * width * height),
0,
0,
0,
(uint)width,
(uint)height,
1,
0,
0);
_fontTextureView = gd.ResourceFactory.CreateTextureView(_fontTexture);
io.Fonts.ClearTexData();
}
/// <summary>
/// Renders the ImGui draw list data.
/// This method requires a <see cref="GraphicsDevice"/> because it may create new DeviceBuffers if the size of vertex
/// or index data has increased beyond the capacity of the existing buffers.
/// A <see cref="CommandList"/> is needed to submit drawing and resource update commands.
/// </summary>
public void Render(GraphicsDevice gd, CommandList cl)
{
if (_frameBegun)
{
_frameBegun = false;
ImGui.Render();
RenderImDrawData(ImGui.GetDrawData(), gd, cl);
}
}
/// <summary>
/// Updates ImGui input and IO configuration state.
/// </summary>
public void Update(float deltaSeconds, InputSnapshot snapshot)
{
if (_frameBegun)
{
ImGui.Render();
}
SetPerFrameImGuiData(deltaSeconds);
UpdateImGuiInput(snapshot);
_frameBegun = true;
ImGui.NewFrame();
}
/// <summary>
/// Sets per-frame data based on the associated window.
/// This is called by Update(float).
/// </summary>
private void SetPerFrameImGuiData(float deltaSeconds)
{
ImGuiIOPtr io = ImGui.GetIO();
io.DisplaySize = new Vector2(
_windowWidth / _scaleFactor.X,
_windowHeight / _scaleFactor.Y);
io.DisplayFramebufferScale = _scaleFactor;
io.DeltaTime = deltaSeconds; // DeltaTime is in seconds.
}
private void UpdateImGuiInput(InputSnapshot snapshot)
{
ImGuiIOPtr io = ImGui.GetIO();
Vector2 mousePosition = snapshot.MousePosition;
// Determine if any of the mouse buttons were pressed during this snapshot period, even if they are no longer held.
bool leftPressed = false;
bool middlePressed = false;
bool rightPressed = false;
foreach (MouseEvent me in snapshot.MouseEvents)
{
if (me.Down)
{
switch (me.MouseButton)
{
case MouseButton.Left:
leftPressed = true;
break;
case MouseButton.Middle:
middlePressed = true;
break;
case MouseButton.Right:
rightPressed = true;
break;
}
}
}
io.MouseDown[0] = leftPressed || snapshot.IsMouseDown(MouseButton.Left);
io.MouseDown[1] = rightPressed || snapshot.IsMouseDown(MouseButton.Right);
io.MouseDown[2] = middlePressed || snapshot.IsMouseDown(MouseButton.Middle);
io.MousePos = mousePosition;
io.MouseWheel = snapshot.WheelDelta;
IReadOnlyList<char> keyCharPresses = snapshot.KeyCharPresses;
for (int i = 0; i < keyCharPresses.Count; i++)
{
char c = keyCharPresses[i];
io.AddInputCharacter(c);
}
IReadOnlyList<KeyEvent> keyEvents = snapshot.KeyEvents;
for (int i = 0; i < keyEvents.Count; i++)
{
KeyEvent keyEvent = keyEvents[i];
io.KeysDown[(int)keyEvent.Key] = keyEvent.Down;
if (keyEvent.Key == Key.ControlLeft)
{
_controlDown = keyEvent.Down;
}
if (keyEvent.Key == Key.ShiftLeft)
{
_shiftDown = keyEvent.Down;
}
if (keyEvent.Key == Key.AltLeft)
{
_altDown = keyEvent.Down;
}
if (keyEvent.Key == Key.WinLeft)
{
_winKeyDown = keyEvent.Down;
}
}
io.KeyCtrl = _controlDown;
io.KeyAlt = _altDown;
io.KeyShift = _shiftDown;
io.KeySuper = _winKeyDown;
}
private static void SetKeyMappings()
{
ImGuiIOPtr io = ImGui.GetIO();
io.KeyMap[(int)ImGuiKey.LeftArrow] = (int)Key.Left;
io.KeyMap[(int)ImGuiKey.RightArrow] = (int)Key.Right;
io.KeyMap[(int)ImGuiKey.UpArrow] = (int)Key.Up;
io.KeyMap[(int)ImGuiKey.DownArrow] = (int)Key.Down;
io.KeyMap[(int)ImGuiKey._0] = (int)Key.Number0;
io.KeyMap[(int)ImGuiKey._1] = (int)Key.Number1;
io.KeyMap[(int)ImGuiKey._2] = (int)Key.Number2;
io.KeyMap[(int)ImGuiKey._3] = (int)Key.Number3;
io.KeyMap[(int)ImGuiKey._4] = (int)Key.Number4;
io.KeyMap[(int)ImGuiKey._5] = (int)Key.Number5;
io.KeyMap[(int)ImGuiKey._6] = (int)Key.Number6;
io.KeyMap[(int)ImGuiKey._7] = (int)Key.Number7;
io.KeyMap[(int)ImGuiKey._8] = (int)Key.Number8;
io.KeyMap[(int)ImGuiKey._9] = (int)Key.Number9;
io.KeyMap[(int)ImGuiKey.Apostrophe] = (int)Key.Grave;
io.KeyMap[(int)ImGuiKey.Equal] = (int)Key.Plus;
io.KeyMap[(int)ImGuiKey.LeftBracket] = (int)Key.BracketLeft;
io.KeyMap[(int)ImGuiKey.Backslash] = (int)Key.BackSlash;
io.KeyMap[(int)ImGuiKey.RightBracket] = (int)Key.BracketRight;
foreach (var key in Enum.GetValues<ImGuiKey>())
{
if (Enum.TryParse<Key>(key.ToString(), out var target))
{
io.KeyMap[(int)key] = (int)target;
}
}
}
private void RenderImDrawData(ImDrawDataPtr draw_data, GraphicsDevice gd, CommandList cl)
{
uint vertexOffsetInVertices = 0;
uint indexOffsetInElements = 0;
if (draw_data.CmdListsCount == 0)
{
return;
}
uint totalVBSize = (uint)(draw_data.TotalVtxCount * Unsafe.SizeOf<ImDrawVert>());
if (totalVBSize > _vertexBuffer.SizeInBytes)
{
gd.DisposeWhenIdle(_vertexBuffer);
_vertexBuffer = gd.ResourceFactory.CreateBuffer(new BufferDescription((uint)(totalVBSize * 1.5f), BufferUsage.VertexBuffer | BufferUsage.Dynamic));
}
uint totalIBSize = (uint)(draw_data.TotalIdxCount * sizeof(ushort));
if (totalIBSize > _indexBuffer.SizeInBytes)
{
gd.DisposeWhenIdle(_indexBuffer);
_indexBuffer = gd.ResourceFactory.CreateBuffer(new BufferDescription((uint)(totalIBSize * 1.5f), BufferUsage.IndexBuffer | BufferUsage.Dynamic));
}
for (int i = 0; i < draw_data.CmdListsCount; i++)
{
ImDrawListPtr cmd_list = draw_data.CmdListsRange[i];
cl.UpdateBuffer(
_vertexBuffer,
vertexOffsetInVertices * (uint)Unsafe.SizeOf<ImDrawVert>(),
cmd_list.VtxBuffer.Data,
(uint)(cmd_list.VtxBuffer.Size * Unsafe.SizeOf<ImDrawVert>()));
cl.UpdateBuffer(
_indexBuffer,
indexOffsetInElements * sizeof(ushort),
cmd_list.IdxBuffer.Data,
(uint)(cmd_list.IdxBuffer.Size * sizeof(ushort)));
vertexOffsetInVertices += (uint)cmd_list.VtxBuffer.Size;
indexOffsetInElements += (uint)cmd_list.IdxBuffer.Size;
}
// Setup orthographic projection matrix into our constant buffer
ImGuiIOPtr io = ImGui.GetIO();
Matrix4x4 mvp = Matrix4x4.CreateOrthographicOffCenter(
0f,
io.DisplaySize.X,
io.DisplaySize.Y,
0.0f,
-1.0f,
1.0f);
_gd.UpdateBuffer(_projMatrixBuffer, 0, ref mvp);
cl.SetVertexBuffer(0, _vertexBuffer);
cl.SetIndexBuffer(_indexBuffer, IndexFormat.UInt16);
cl.SetPipeline(_pipeline);
cl.SetGraphicsResourceSet(0, _mainResourceSet);
draw_data.ScaleClipRects(io.DisplayFramebufferScale);
// Render command lists
int vtx_offset = 0;
int idx_offset = 0;
for (int n = 0; n < draw_data.CmdListsCount; n++)
{
ImDrawListPtr cmd_list = draw_data.CmdListsRange[n];
for (int cmd_i = 0; cmd_i < cmd_list.CmdBuffer.Size; cmd_i++)
{
ImDrawCmdPtr pcmd = cmd_list.CmdBuffer[cmd_i];
if (pcmd.UserCallback != IntPtr.Zero)
{
throw new NotImplementedException();
}
else
{
if (pcmd.TextureId != IntPtr.Zero)
{
if (pcmd.TextureId == _fontAtlasID)
{
cl.SetGraphicsResourceSet(1, _fontTextureResourceSet);
}
else
{
cl.SetGraphicsResourceSet(1, GetImageResourceSet(pcmd.TextureId));
}
}
cl.SetScissorRect(
0,
(uint)pcmd.ClipRect.X,
(uint)pcmd.ClipRect.Y,
(uint)(pcmd.ClipRect.Z - pcmd.ClipRect.X),
(uint)(pcmd.ClipRect.W - pcmd.ClipRect.Y));
cl.DrawIndexed(pcmd.ElemCount, 1, pcmd.IdxOffset + (uint)idx_offset, (int)pcmd.VtxOffset + vtx_offset, 0);
}
}
vtx_offset += cmd_list.VtxBuffer.Size;
idx_offset += cmd_list.IdxBuffer.Size;
}
}
/// <summary>
/// Frees all graphics resources used by the renderer.
/// </summary>
public void Dispose()
{
_vertexBuffer.Dispose();
_indexBuffer.Dispose();
_projMatrixBuffer.Dispose();
_fontTexture.Dispose();
_fontTextureView.Dispose();
_vertexShader.Dispose();
_fragmentShader.Dispose();
_layout.Dispose();
_textureLayout.Dispose();
_pipeline.Dispose();
_mainResourceSet.Dispose();
foreach (IDisposable resource in _ownedResources)
{
resource.Dispose();
}
}
private struct ResourceSetInfo
{
public readonly IntPtr ImGuiBinding;
public readonly ResourceSet ResourceSet;
public ResourceSetInfo(IntPtr imGuiBinding, ResourceSet resourceSet)
{
ImGuiBinding = imGuiBinding;
ResourceSet = resourceSet;
}
}
}
}