-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathImGuiD3D11Renderer.cs
786 lines (672 loc) · 32 KB
/
ImGuiD3D11Renderer.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
//based on https://github.com/ocornut/imgui/blob/master/backends/imgui_impl_dx11.cpp
#nullable disable
namespace ExampleD3D11
{
using Hexa.NET.ImGui;
using Silk.NET.Core.Native;
using Silk.NET.Direct3D.Compilers;
using Silk.NET.Direct3D11;
using Silk.NET.DXGI;
using Silk.NET.Maths;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.InteropServices;
using ImDrawIdx = UInt16;
[Obsolete("Use ImGuiImplD3D11 instead")]
public static class ImGuiD3D11Renderer
{
private static readonly D3DCompiler D3DCompiler = D3DCompiler.GetApi();
/// <summary>
/// Renderer data
/// </summary>
private struct RendererData
{
public ComPtr<ID3D11Device> device;
public ComPtr<ID3D11DeviceContext> context;
public ComPtr<IDXGIFactory> factory;
public ComPtr<ID3D11Buffer> vertexBuffer;
public ComPtr<ID3D11Buffer> indexBuffer;
public ComPtr<ID3D11VertexShader> vertexShader;
public ComPtr<ID3D11InputLayout> inputLayout;
public ComPtr<ID3D11Buffer> constantBuffer;
public ComPtr<ID3D11PixelShader> pixelShader;
public ComPtr<ID3D11SamplerState> fontSampler;
public ComPtr<ID3D11ShaderResourceView> fontTextureView;
public ComPtr<ID3D11RasterizerState> rasterizerState;
public ComPtr<ID3D11BlendState> blendState;
public ComPtr<ID3D11DepthStencilState> depthStencilState;
public int vertexBufferSize = 5000, indexBufferSize = 10000;
public RendererData()
{
}
}
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
private static unsafe RendererData* GetBackendData()
{
return !ImGui.GetCurrentContext().IsNull ? (RendererData*)ImGui.GetIO().BackendRendererUserData : null;
}
private static unsafe void SetupRenderState(ImDrawData* drawData, ComPtr<ID3D11DeviceContext> ctx)
{
RendererData* bd = GetBackendData();
var viewport = new Viewport(0, 0, drawData->DisplaySize.X, drawData->DisplaySize.Y, 0, 1);
ctx.RSSetViewports(1, &viewport);
uint stride = (uint)sizeof(ImDrawVert);
uint offset = 0;
ctx.IASetInputLayout(bd->inputLayout);
ctx.IASetVertexBuffers(0, 1, bd->vertexBuffer, &stride, &offset);
ctx.IASetIndexBuffer(bd->indexBuffer, sizeof(ushort) == 2 ? Format.FormatR16Uint : Format.FormatR32Uint, 0);
ctx.IASetPrimitiveTopology(D3DPrimitiveTopology.D3DPrimitiveTopologyTrianglelist);
ctx.VSSetShader(bd->vertexShader, null, 0);
ctx.VSSetConstantBuffers(0, 1, bd->constantBuffer);
ctx.PSSetShader(bd->pixelShader, null, 0);
ctx.PSSetSamplers(0, 1, bd->fontSampler);
ctx.GSSetShader((ID3D11GeometryShader*)null, null, 0);
ctx.HSSetShader((ID3D11HullShader*)null, null, 0); // In theory we should backup and restore this as well.. very infrequently used..
ctx.DSSetShader((ID3D11DomainShader*)null, null, 0); // In theory we should backup and restore this as well.. very infrequently used..
ctx.CSSetShader((ID3D11ComputeShader*)null, null, 0); // In theory we should backup and restore this as well.. very infrequently used..
// Setup blend state
Vector4 blend_factor = Vector4.Zero;
ctx.OMSetBlendState(bd->blendState, (float*)&blend_factor, 0xffffffff);
ctx.OMSetDepthStencilState(bd->depthStencilState, 0);
ctx.RSSetState(bd->rasterizerState);
}
/// <summary>
/// Render function
/// </summary>
/// <param name="data"></param>
public static unsafe void RenderDrawData(ImDrawData* data)
{
// Avoid rendering when minimized
if (data->DisplaySize.X <= 0.0f || data->DisplaySize.Y <= 0.0f)
{
return;
}
if (data->CmdListsCount == 0)
{
return;
}
RendererData* bd = GetBackendData();
ComPtr<ID3D11DeviceContext> ctx = bd->context;
// Create and grow vertex/index buffers if needed
if (bd->vertexBuffer.Handle == null || bd->vertexBufferSize < data->TotalVtxCount)
{
if (bd->vertexBuffer.Handle != null)
{
bd->vertexBuffer.Release();
}
bd->vertexBufferSize = data->TotalVtxCount + 5000;
BufferDesc desc = new();
desc.Usage = Usage.Dynamic;
desc.ByteWidth = (uint)(bd->vertexBufferSize * sizeof(ImDrawVert));
desc.BindFlags = (uint)BindFlag.VertexBuffer;
desc.CPUAccessFlags = (uint)CpuAccessFlag.Write;
bd->device.CreateBuffer(desc, null, ref bd->vertexBuffer);
}
if (bd->indexBuffer.Handle == null || bd->indexBufferSize < data->TotalIdxCount)
{
if (bd->indexBuffer.Handle != null)
{
bd->indexBuffer.Release();
}
bd->indexBufferSize = data->TotalIdxCount + 10000;
BufferDesc desc = new();
desc.Usage = Usage.Dynamic;
desc.ByteWidth = (uint)(bd->indexBufferSize * sizeof(ImDrawIdx));
desc.BindFlags = (uint)BindFlag.IndexBuffer;
desc.CPUAccessFlags = (uint)CpuAccessFlag.Write;
bd->device.CreateBuffer(desc, null, ref bd->indexBuffer);
}
// Upload vertex/index data into a single contiguous GPU buffer
MappedSubresource vertexResource;
ctx.Map(bd->vertexBuffer, 0, Map.WriteDiscard, 0, &vertexResource);
MappedSubresource indexResource;
ctx.Map(bd->indexBuffer, 0, Map.WriteDiscard, 0, &indexResource);
var vertexResourcePointer = (ImDrawVert*)vertexResource.PData;
var indexResourcePointer = (ImDrawIdx*)indexResource.PData;
for (int n = 0; n < data->CmdListsCount; n++)
{
var cmdlList = data->CmdLists.Data[n];
var vertBytes = cmdlList.VtxBuffer.Size * sizeof(ImDrawVert);
System.Buffer.MemoryCopy(cmdlList.VtxBuffer.Data, vertexResourcePointer, vertBytes, vertBytes);
var idxBytes = cmdlList.IdxBuffer.Size * sizeof(ImDrawIdx);
System.Buffer.MemoryCopy(cmdlList.IdxBuffer.Data, indexResourcePointer, idxBytes, idxBytes);
vertexResourcePointer += cmdlList.VtxBuffer.Size;
indexResourcePointer += cmdlList.IdxBuffer.Size;
}
ctx.Unmap(bd->vertexBuffer, 0);
ctx.Unmap(bd->indexBuffer, 0);
// Setup orthographic projection matrix into our constant buffer
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
{
MappedSubresource mappedResource;
ctx.Map(bd->constantBuffer, 0, Map.WriteDiscard, 0, &mappedResource);
Matrix4x4* constant_buffer = (Matrix4x4*)mappedResource.PData;
float L = data->DisplayPos.X;
float R = data->DisplayPos.X + data->DisplaySize.X;
float T = data->DisplayPos.Y;
float B = data->DisplayPos.Y + data->DisplaySize.Y;
Matrix4x4 mvp = new
(
2.0f / (R - L), 0.0f, 0.0f, 0.0f,
0.0f, 2.0f / (T - B), 0.0f, 0.0f,
0.0f, 0.0f, 0.5f, 0.0f,
(R + L) / (L - R), (T + B) / (B - T), 0.5f, 1.0f
);
System.Buffer.MemoryCopy(&mvp, constant_buffer, sizeof(Matrix4x4), sizeof(Matrix4x4));
ctx.Unmap(bd->constantBuffer, 0);
}
// Setup desired state
SetupRenderState(data, ctx);
// Render command lists
// (Because we merged all buffers into a single one, we maintain our own offset into them)
int global_idx_offset = 0;
int global_vtx_offset = 0;
Vector2 clip_off = data->DisplayPos;
for (int n = 0; n < data->CmdListsCount; n++)
{
var cmdList = data->CmdLists[n];
for (int i = 0; i < cmdList.CmdBuffer.Size; i++)
{
var cmd = cmdList.CmdBuffer[i];
if (cmd.UserCallback != null)
{
// User callback, registered via ImDrawList::AddCallback()
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
if ((nint)cmd.UserCallback == ImGui.ImDrawCallbackResetRenderState)
{
SetupRenderState(data, ctx);
}
else
{
((delegate*<ImDrawList*, ImDrawCmd*, void>)cmd.UserCallback)(cmdList, &cmd);
}
}
else
{
// Project scissor/clipping rectangles into framebuffer space
Vector2 clip_min = new(cmd.ClipRect.X - clip_off.X, cmd.ClipRect.Y - clip_off.Y);
Vector2 clip_max = new(cmd.ClipRect.Z - clip_off.X, cmd.ClipRect.W - clip_off.Y);
if (clip_max.X <= clip_min.X || clip_max.Y <= clip_min.Y)
continue;
// Apply scissor/clipping rectangle
Box2D<int> rect = new((int)clip_min.X, (int)clip_min.Y, (int)clip_max.X, (int)clip_max.Y);
ctx.RSSetScissorRects(1, &rect);
// Bind texture, Draw
var srv = cmd.TextureId.Handle;
ctx.PSSetShaderResources(0, 1, (ID3D11ShaderResourceView**)&srv);
ctx.DrawIndexedInstanced(cmd.ElemCount, 1, (uint)(cmd.IdxOffset + global_idx_offset), (int)(cmd.VtxOffset + global_vtx_offset), 0);
}
}
global_idx_offset += cmdList.IdxBuffer.Size;
global_vtx_offset += cmdList.VtxBuffer.Size;
}
void* nullObj = null;
ctx.IASetInputLayout((ID3D11InputLayout*)null);
ctx.VSSetShader((ID3D11VertexShader*)null, null, 0);
ctx.PSSetShader((ID3D11PixelShader*)null, null, 0);
ctx.GSSetShader((ID3D11GeometryShader*)null, null, 0);
ctx.HSSetShader((ID3D11HullShader*)null, null, 0);
ctx.DSSetShader((ID3D11DomainShader*)null, null, 0);
ctx.CSSetShader((ID3D11ComputeShader*)null, null, 0);
Vector4 blend_factor = Vector4.Zero;
ctx.OMSetBlendState((ID3D11BlendState*)null, (float*)&blend_factor, 0xffffffff);
ctx.OMSetDepthStencilState((ID3D11DepthStencilState*)null, 0);
ctx.RSSetState((ID3D11RasterizerState*)null);
Viewport viewport = default;
ctx.RSSetViewports(1, &viewport);
ctx.IASetVertexBuffers(0, 1, (ID3D11Buffer**)&nullObj, 0, 0);
ctx.IASetIndexBuffer((ID3D11Buffer*)null, default, 0);
ctx.IASetPrimitiveTopology(D3DPrimitiveTopology.D3DPrimitiveTopologyUndefined);
ctx.VSSetConstantBuffers(0, 1, (ID3D11Buffer**)&nullObj);
ctx.PSSetSamplers(0, 1, (ID3D11SamplerState**)&nullObj);
ctx.PSSetShaderResources(0, 1, (ID3D11ShaderResourceView**)&nullObj);
}
private static unsafe void CreateFontsTexture()
{
var io = ImGui.GetIO();
RendererData* bd = GetBackendData();
byte* pixels;
int width;
int height;
ImGui.GetTexDataAsRGBA32(io.Fonts, &pixels, &width, &height, null);
var texDesc = new Texture2DDesc
{
Width = (uint)width,
Height = (uint)height,
MipLevels = 1,
ArraySize = 1,
Format = Format.FormatR8G8B8A8Unorm,
SampleDesc = new SampleDesc { Count = 1 },
Usage = Usage.Default,
BindFlags = (uint)BindFlag.ShaderResource,
CPUAccessFlags = (uint)CpuAccessFlag.None
};
var subResource = new SubresourceData
{
PSysMem = pixels,
SysMemPitch = texDesc.Width * 4,
SysMemSlicePitch = 0
};
ComPtr<ID3D11Texture2D> texture = default;
bd->device.CreateTexture2D(&texDesc, &subResource, ref texture);
var resViewDesc = new ShaderResourceViewDesc
{
Format = Format.FormatR8G8B8A8Unorm,
ViewDimension = D3DSrvDimension.D3DSrvDimensionTexture2D,
Texture2D = new() { MipLevels = texDesc.MipLevels, MostDetailedMip = 0 }
};
;
bd->device.CreateShaderResourceView(texture, &resViewDesc, ref bd->fontTextureView);
texture.Dispose();
io.Fonts.TexID = new((nint)bd->fontTextureView.Handle);
var samplerDesc = new SamplerDesc
{
Filter = Filter.MinMagMipLinear,
AddressU = TextureAddressMode.Wrap,
AddressV = TextureAddressMode.Wrap,
AddressW = TextureAddressMode.Wrap,
MipLODBias = 0f,
ComparisonFunc = ComparisonFunc.Always,
MinLOD = 0f,
MaxLOD = 0f
};
bd->device.CreateSamplerState(&samplerDesc, ref bd->fontSampler);
}
private static unsafe bool CreateDeviceObjects()
{
RendererData* bd = GetBackendData();
if (bd->device.Handle == null)
return false;
if (bd->fontSampler.Handle != null)
InvalidateDeviceObjects();
{
string vertexShaderCode =
@"cbuffer vertexBuffer : register(b0)
{
float4x4 ProjectionMatrix;
};
struct VS_INPUT
{
float2 pos : POSITION;
float2 uv : TEXCOORD0;
float4 col : COLOR0;
};
struct PS_INPUT
{
float4 pos : SV_POSITION;
float4 col : COLOR0;
float2 uv : TEXCOORD0;
};
PS_INPUT main(VS_INPUT input)
{
PS_INPUT output;
output.pos = mul(ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));
output.col = input.col;
output.uv = input.uv;
return output;
}
";
byte* pVertexShaderCode = vertexShaderCode.ToUTF8Ptr();
byte* pEntryPoint = "main".ToUTF8Ptr();
byte* pProfile = "vs_4_0".ToUTF8Ptr();
ID3D10Blob* vertexShaderBlob;
HResult result = D3DCompiler.Compile(pVertexShaderCode, (nuint)vertexShaderCode.Length, (byte*)null, (D3DShaderMacro*)null, (ID3DInclude*)null, pEntryPoint, pProfile, 0, 0, &vertexShaderBlob, null);
// avoid leaks even if compilation fails.
Free(pVertexShaderCode);
Free(pEntryPoint);
Free(pProfile);
if (!result.IsSuccess)
return false; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
result = bd->device.CreateVertexShader(vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize(), (ID3D11ClassLinkage*)null, bd->vertexShader.GetAddressOf());
if (!result.IsSuccess)
{
vertexShaderBlob->Release();
return false;
}
// Create the input layout
InputElementDesc[] local_layout =
{
new("POSITION".ToUTF8Ptr(), 0, Format.FormatR32G32Float, 0, 0, InputClassification.PerVertexData, 0 ) ,
new("TEXCOORD".ToUTF8Ptr(), 0, Format.FormatR32G32Float, 0, 8, InputClassification.PerVertexData, 0 ),
new("COLOR".ToUTF8Ptr(), 0, Format.FormatR8G8B8A8Unorm, 0, 16, InputClassification.PerVertexData, 0),
};
fixed (InputElementDesc* pLocal_layout = local_layout)
{
result = bd->device.CreateInputLayout(pLocal_layout, 3, vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize(), ref bd->inputLayout);
}
for (int i = 0; i < local_layout.Length; i++)
{
Free(local_layout[i].SemanticName);
}
if (!result.IsSuccess)
{
vertexShaderBlob->Release();
return false;
}
vertexShaderBlob->Release();
// Create the constant buffer
{
var constBufferDesc = new BufferDesc
{
ByteWidth = (uint)sizeof(Matrix4x4),
Usage = Usage.Dynamic,
BindFlags = (uint)BindFlag.ConstantBuffer,
CPUAccessFlags = (uint)CpuAccessFlag.Write,
};
bd->device.CreateBuffer(&constBufferDesc, null, ref bd->constantBuffer);
}
}
// Create the pixel shader
{
string pixelShaderCode =
@"struct PS_INPUT
{
float4 pos : SV_POSITION;
float4 col : COLOR0;
float2 uv : TEXCOORD0;
};
sampler sampler0;
Texture2D texture0;
float4 main(PS_INPUT input) : SV_Target
{
float4 out_col = input.col * texture0.Sample(sampler0, input.uv);
return out_col;
}
";
byte* pPixelShaderCode = pixelShaderCode.ToUTF8Ptr();
byte* pEntryPoint = "main".ToUTF8Ptr();
byte* pProfile = "ps_4_0".ToUTF8Ptr();
ID3D10Blob* pixelShaderBlob;
HResult result = D3DCompiler.Compile(pPixelShaderCode, (nuint)pixelShaderCode.Length, (byte*)null, (D3DShaderMacro*)null, (ID3DInclude*)null, pEntryPoint, pProfile, 0, 0, &pixelShaderBlob, null);
Free(pPixelShaderCode);
Free(pEntryPoint);
Free(pProfile);
if (!result.IsSuccess)
return false; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
result = bd->device.CreatePixelShader(pixelShaderBlob->GetBufferPointer(), pixelShaderBlob->GetBufferSize(), (ID3D11ClassLinkage*)null, bd->pixelShader.GetAddressOf());
if (!result.IsSuccess)
{
pixelShaderBlob->Release();
return false;
}
pixelShaderBlob->Release();
}
// Create the blending setup
{
var blendDesc = new BlendDesc
{
AlphaToCoverageEnable = false
};
blendDesc.RenderTarget[0] = new()
{
BlendOpAlpha = BlendOp.Add,
BlendEnable = true,
BlendOp = BlendOp.Add,
DestBlendAlpha = Blend.InvSrcAlpha,
DestBlend = Blend.InvSrcAlpha,
SrcBlend = Blend.SrcAlpha,
SrcBlendAlpha = Blend.SrcAlpha,
RenderTargetWriteMask = (byte)ColorWriteEnable.All
};
bd->device.CreateBlendState(&blendDesc, ref bd->blendState);
}
// Create the rasterizer state
{
var rasterDesc = new RasterizerDesc
{
FillMode = FillMode.Solid,
CullMode = CullMode.None,
ScissorEnable = true,
DepthClipEnable = false,
};
bd->device.CreateRasterizerState(&rasterDesc, ref bd->rasterizerState);
}
// Create depth-stencil State
{
var stencilOpDesc = new DepthStencilopDesc(StencilOp.Keep, StencilOp.Keep, StencilOp.Keep, ComparisonFunc.Never);
var depthDesc = new DepthStencilDesc
{
DepthEnable = false,
DepthWriteMask = DepthWriteMask.Zero,
DepthFunc = ComparisonFunc.Always,
StencilEnable = false,
FrontFace = stencilOpDesc,
BackFace = stencilOpDesc
};
bd->device.CreateDepthStencilState(&depthDesc, ref bd->depthStencilState);
}
CreateFontsTexture();
return true;
}
private static unsafe void InvalidateDeviceObjects()
{
RendererData* bd = GetBackendData();
if (bd->device.Handle == null)
return;
if (bd->fontSampler.Handle != null)
{
bd->fontSampler.Release();
bd->fontSampler = null;
}
if (bd->fontTextureView.Handle != null)
{
bd->fontTextureView.Release();
bd->fontTextureView = null;
ImGui.GetIO().Fonts.SetTexID(0); // We copied data->pFontTextureView to io.Fonts->TexID so let's clear that as well.
}
if (bd->indexBuffer.Handle != null)
{
bd->indexBuffer.Release();
bd->indexBuffer = null;
}
if (bd->vertexBuffer.Handle != null)
{
bd->vertexBuffer.Release();
bd->vertexBuffer = null;
}
if (bd->blendState.Handle != null)
{
bd->blendState.Release();
bd->blendState = null;
}
if (bd->depthStencilState.Handle != null)
{
bd->depthStencilState.Release();
bd->depthStencilState = null;
}
if (bd->rasterizerState.Handle != null)
{
bd->rasterizerState.Release();
bd->rasterizerState = null;
}
if (bd->pixelShader.Handle != null)
{
bd->pixelShader.Release();
bd->pixelShader = null;
}
if (bd->constantBuffer.Handle != null)
{
bd->constantBuffer.Release();
bd->constantBuffer = null;
}
if (bd->inputLayout.Handle != null)
{
bd->inputLayout.Release();
bd->inputLayout = null;
}
if (bd->vertexShader.Handle != null)
{
bd->vertexShader.Release();
bd->vertexShader = null;
}
}
public static unsafe bool Init(ComPtr<ID3D11Device> device, ComPtr<ID3D11DeviceContext> context)
{
var io = ImGui.GetIO();
Trace.Assert(io.BackendRendererUserData == null, "Already initialized a renderer backend!");
// Setup backend capabilities flags
var bd = AllocT<RendererData>();
io.BackendRendererUserData = bd;
io.BackendRendererName = "imgui_impl_dx11".ToUTF8Ptr();
io.BackendFlags |= ImGuiBackendFlags.RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
io.BackendFlags |= ImGuiBackendFlags.RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
// Get factory from device
if (device.QueryInterface(out ComPtr<IDXGIDevice> pDXGIDevice) == 0)
{
if (pDXGIDevice.GetParent(out ComPtr<IDXGIAdapter> pDXGIAdapter) == 0)
{
if (pDXGIAdapter.GetParent(out ComPtr<IDXGIFactory> pFactory) == 0)
{
bd->device = device;
bd->context = context;
bd->factory = pFactory;
}
}
if (pDXGIAdapter.Handle != null)
{
pDXGIAdapter.Release();
}
}
if (pDXGIDevice.Handle != null)
{
pDXGIDevice.Release();
}
bd->device.AddRef();
bd->context.AddRef();
CreateDeviceObjects();
if ((io.ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0)
InitPlatformInterface();
return true;
}
public static unsafe void Shutdown()
{
RendererData* bd = GetBackendData();
Trace.Assert(bd != null, "No renderer backend to shutdown, or already shutdown?");
var io = ImGui.GetIO();
ShutdownPlatformInterface();
InvalidateDeviceObjects();
if (bd->factory.Handle != null) { bd->factory.Release(); }
if (bd->device.Handle != null) { bd->device.Release(); }
if (bd->context.Handle != null) { bd->context.Release(); }
io.BackendRendererName = null;
io.BackendRendererUserData = null;
io.BackendFlags &= ~(ImGuiBackendFlags.RendererHasVtxOffset | ImGuiBackendFlags.RendererHasViewports);
Free(bd);
}
public static unsafe void NewFrame()
{
RendererData* bd = GetBackendData();
Trace.Assert(bd != null, "Did you call ImGui_ImplDX11_Init()?");
if (bd->fontSampler.Handle == null)
CreateDeviceObjects();
}
//--------------------------------------------------------------------------------------------------------
// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
// This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
// If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
//--------------------------------------------------------------------------------------------------------
/// <summary>
/// Helper structure we store in the void* RendererUserData field of each ImGuiViewport to easily retrieve our backend data.
/// </summary>
private struct ViewportData
{
public ComPtr<IDXGISwapChain> SwapChain;
public ComPtr<ID3D11RenderTargetView> RTView;
};
private static unsafe void CreateWindow(ImGuiViewport* viewport)
{
RendererData* bd = GetBackendData();
ViewportData* vd = AllocT<ViewportData>();
viewport->RendererUserData = vd;
// PlatformHandleRaw should always be a HWND, whereas PlatformHandle might be a higher-level handle (e.g. GLFWWindow*, SDL_Window*).
// Some backends will leave PlatformHandleRaw == 0, in which case we assume PlatformHandle will contain the HWND.
void* hwnd = viewport->PlatformHandleRaw != null ? viewport->PlatformHandleRaw : viewport->PlatformHandle;
// Create swap chain
SwapChainDesc sd;
sd.BufferDesc.Width = (uint)viewport->Size.X;
sd.BufferDesc.Height = (uint)viewport->Size.Y;
sd.BufferDesc.Format = Format.FormatR8G8B8A8Unorm;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.BufferUsage = DXGI.UsageRenderTargetOutput;
sd.BufferCount = 1;
sd.OutputWindow = (nint)hwnd;
sd.Windowed = true;
sd.SwapEffect = SwapEffect.Discard;
sd.Flags = 0;
bd->factory.CreateSwapChain(bd->device, &sd, ref vd->SwapChain);
// Create the render target
if (vd->SwapChain.Handle != null)
{
vd->SwapChain.GetBuffer(0, out ComPtr<ID3D11Texture2D> pBackBuffer);
bd->device.CreateRenderTargetView(pBackBuffer, null, ref vd->RTView);
pBackBuffer.Release();
}
}
private static unsafe void DestroyWindow(ImGuiViewport* viewport)
{
// The main viewport (owned by the application) will always have RendererUserData == null since we didn't create the data for it.
ViewportData* vd = (ViewportData*)viewport->RendererUserData;
if (vd != null)
{
if (vd->SwapChain.Handle != null)
{
vd->SwapChain.Release();
}
vd->SwapChain = null;
vd->RTView = null;
Free(vd);
}
viewport->RendererUserData = null;
}
private static unsafe void SetWindowSize(ImGuiViewport* viewport, Vector2 size)
{
RendererData* bd = GetBackendData();
ViewportData* vd = (ViewportData*)viewport->RendererUserData;
if (vd->RTView.Handle != null)
{
vd->RTView.Release();
vd->RTView = null;
}
if (vd->SwapChain.Handle != null)
{
vd->SwapChain.ResizeBuffers(0, (uint)size.X, (uint)size.Y, Format.FormatUnknown, 0);
vd->SwapChain.GetBuffer(0, out ComPtr<ID3D11Texture2D> pBackBuffer);
bd->device.CreateRenderTargetView(pBackBuffer, null, ref vd->RTView);
pBackBuffer.Release();
}
}
private static unsafe void RenderWindow(ImGuiViewport* viewport, void* userdata)
{
RendererData* bd = GetBackendData();
ViewportData* vd = (ViewportData*)viewport->RendererUserData;
var rtv = vd->RTView.Handle;
bd->context.OMSetRenderTargets(1, &rtv, (ID3D11DepthStencilView*)null);
if ((viewport->Flags & ImGuiViewportFlags.NoRendererClear) == 0)
{
var col = new Vector4(0.0f, 0.0f, 0.0f, 1.0f);
bd->context.ClearRenderTargetView(rtv, (float*)&col);
}
RenderDrawData(viewport->DrawData);
}
private static unsafe void SwapBuffers(ImGuiViewport* viewport, void* userdata)
{
ViewportData* vd = (ViewportData*)viewport->RendererUserData;
vd->SwapChain.Present(0, 0); // Present without vsync
}
private static unsafe void InitPlatformInterface()
{
ImGuiPlatformIOPtr platform_io = ImGui.GetPlatformIO();
platform_io.RendererCreateWindow = (void*)Marshal.GetFunctionPointerForDelegate<RendererCreateWindow>(CreateWindow);
platform_io.RendererDestroyWindow = (void*)Marshal.GetFunctionPointerForDelegate<RendererDestroyWindow>(DestroyWindow);
platform_io.RendererSetWindowSize = (void*)Marshal.GetFunctionPointerForDelegate<RendererSetWindowSize>(SetWindowSize);
platform_io.RendererRenderWindow = (void*)Marshal.GetFunctionPointerForDelegate<RendererRenderWindow>(RenderWindow);
platform_io.RendererSwapBuffers = (void*)Marshal.GetFunctionPointerForDelegate<RendererSwapBuffers>(SwapBuffers);
}
private static unsafe void ShutdownPlatformInterface()
{
ImGui.DestroyPlatformWindows();
}
}
}