-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ImGuiOpenGL3Renderer.cs
916 lines (821 loc) · 39.8 KB
/
ImGuiOpenGL3Renderer.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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
#define IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
#define IMGUI_IMPL_OPENGL_HAS_EXTENSIONS // has glGetIntegerv(GL_NUM_EXTENSIONS)
#define IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE // may have glPolygonMode()
#define IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_BUFFER_PIXEL_UNPACK
#define IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART
#define IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET
#define IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
namespace ExampleOpenGL3
{
using Hexa.NET.ImGui;
using Hexa.NET.Utilities;
using Hexa.NET.OpenGL;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.InteropServices;
using ImDrawIdx = UInt16;
[Obsolete("Use ImGuiImplOpenGL3 instead")]
public static unsafe class ImGuiOpenGL3Renderer
{
private static GL GL = null!;
private struct RenderData
{
public uint GlVersion; // Extracted at runtime using GL_MAJOR_VERSION, GL_MINOR_VERSION queries (e.g. 320 for GL 3.2)
public byte* GlslVersionString; // Specified by user or detected based on compile time GL settings.
public bool GlProfileIsES2;
public bool GlProfileIsES3;
public bool GlProfileIsCompat;
public int GlProfileMask;
public uint FontTexture;
public uint ShaderHandle;
public int AttribLocationTex; // Uniforms location
public int AttribLocationProjMtx;
public uint AttribLocationVtxPos; // Vertex attributes location
public uint AttribLocationVtxUV;
public uint AttribLocationVtxColor;
public uint VboHandle, ElementsHandle;
public nint VertexBufferSize;
public nint IndexBufferSize;
public bool HasPolygonMode;
public bool HasClipOrigin;
public bool UseBufferSubData;
};
private static RenderData* GetBackendData()
{
return !ImGui.GetCurrentContext().IsNull ? (RenderData*)ImGui.GetIO().BackendRendererUserData : null;
}
private struct VtxAttribState
{
public int Enabled, Size, Type, Normalized, Stride;
public void* Ptr;
public void GetState(uint index)
{
GL.GetVertexAttribIiv(index, GLVertexAttribEnum.ArrayEnabled, out Enabled);
GL.GetVertexAttribIiv(index, GLVertexAttribEnum.ArraySize, out Size);
GL.GetVertexAttribIiv(index, GLVertexAttribEnum.ArrayType, out Type);
GL.GetVertexAttribIiv(index, GLVertexAttribEnum.ArrayNormalized, out Normalized);
GL.GetVertexAttribIiv(index, GLVertexAttribEnum.ArrayStride, out Stride);
void* ptr;
GL.GetVertexAttribPointerv(index, GLVertexAttribPointerPropertyARB.ArrayPointer, &ptr);
Ptr = ptr;
}
public void SetState(uint index)
{
GL.VertexAttribPointer(index, Size, (GLVertexAttribPointerType)Type, Normalized != 0, Stride, Ptr);
if (Enabled != 0)
{
GL.EnableVertexAttribArray(index);
}
else
{
GL.DisableVertexAttribArray(index);
}
}
};
public static bool Init(GL gl,string? glsl_version_str)
{
GL = gl;
ImGuiIOPtr io = ImGui.GetIO();
Debug.Assert(io.BackendRendererUserData == null, "Already initialized a renderer backend!");
RenderData* bd = AllocT<RenderData>();
ZeroMemoryT(bd);
io.BackendRendererUserData = bd;
io.BackendRendererName = "imgui_impl_opengl3".ToUTF8Ptr();
#if IMGUI_IMPL_OPENGL_ES2
// GLES 2
bd->GlVersion = 200;
bd->GlProfileIsES2 = true;
#else
// Desktop or GLES 3
string gl_version_str = ToStringFromUTF8(GL.GetString(GLStringName.Version))!;
int major = 0;
int minor = 0;
GL.GetIntegerv(GLGetPName.MajorVersion, &major);
GL.GetIntegerv(GLGetPName.MinorVersion, &minor);
if (major == 0 && minor == 0)
{
// Query GL_VERSION in desktop GL 2.X, the string will start with "<major>.<minor>"
var parts = gl_version_str.Split('.');
major = int.Parse(parts[0]);
minor = int.Parse(parts[1]);
}
bd->GlVersion = (uint)(major * 100 + minor * 10);
#if GL_CONTEXT_PROFILE_MASK
if (bd->GlVersion >= 320)
GL.GetInteger(GL_CONTEXT_PROFILE_MASK, &bd->GlProfileMask);
bd->GlProfileIsCompat = (bd->GlProfileMask & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0;
#endif
#if IMGUI_IMPL_OPENGL_ES3
bd.GlProfileIsES3 = true;
#else
if (gl_version_str.StartsWith("OpenGL ES 3"))
{
bd->GlProfileIsES3 = true;
}
#endif
bd->UseBufferSubData = false;
#endif
#if IMGUI_IMPL_OPENGL_DEBUG
Debug.WriteLine($"GlVersion = {bd.GlVersion}, \"{glVersionStr}\"\nGlProfileIsCompat = {bd.GlProfileIsCompat}\nGlProfileMask = 0x{bd.GlProfileMask:X}\nGlProfileIsES2 = {bd.GlProfileIsES2}\nGlProfileIsES3 = {bd.GlProfileIsES3}\nGL_VENDOR = '{Marshal.PtrToStringAnsi((IntPtr)_gl.GetString(StringName.Vendor))}'\nGL_RENDERER = '{Marshal.PtrToStringAnsi((IntPtr)_gl.GetString(StringName.Renderer))}'");
#endif
#if IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET
if (bd->GlVersion >= 320)
{
io.BackendFlags |= ImGuiBackendFlags.RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
}
#endif
io.BackendFlags |= ImGuiBackendFlags.RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
// Store GLSL version string so we can refer to it later in case we recreate shaders.
// Note: GLSL version is NOT the same as GL version. Leave this to null if unsure.
if (string.IsNullOrEmpty(glsl_version_str))
{
#if IMGUI_IMPL_OPENGL_ES2
glsl_version_str = "#version 100";
#elif IMGUI_IMPL_OPENGL_ES3
glsl_version_str = "#version 300 es";
#elif __APPLE__
glsl_version_str = "#version 150";
#else
glsl_version_str = "#version 130";
#endif
}
bd->GlslVersionString = (glsl_version_str + "\n").ToUTF8Ptr();
// Make an arbitrary GL call (we don't actually need the result)
// IF YOU GET A CRASH HERE: it probably means the OpenGL function loader didn't do its job. Let us know!
int currentTexture = 0;
GL.GetIntegerv(GLGetPName.TextureBinding2D, out currentTexture);
// Detect extensions we support
#if IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE
bd->HasPolygonMode = !bd->GlProfileIsES2 && !bd->GlProfileIsES3;
#endif
bd->HasClipOrigin = bd->GlVersion >= 450;
#if IMGUI_IMPL_OPENGL_HAS_EXTENSIONS
int numExtensions = 0;
GL.GetIntegerv(GLGetPName.NumExtensions, out numExtensions);
for (int i = 0; i < numExtensions; i++)
{
string extension = ToStringFromUTF8(GL.GetStringi(GLStringName.Extensions, (uint)i))!;
if (extension != null && extension.Equals("GL_ARB_clip_control", StringComparison.OrdinalIgnoreCase))
{
bd->HasClipOrigin = true;
}
}
#endif
CreateDeviceObjects();
if ((io.ConfigFlags & ImGuiConfigFlags.ViewportsEnable) != 0)
{
InitPlatformInterface();
}
return true;
}
public static void Shutdown()
{
RenderData* bd = GetBackendData();
Debug.Assert(bd != null, "No renderer backend to shutdown, or already shutdown?");
ImGuiIOPtr io = ImGui.GetIO();
ShutdownPlatformInterface();
DestroyDeviceObjects();
Free(io.BackendRendererName);
io.BackendRendererName = null;
io.BackendRendererUserData = null;
io.BackendFlags &= ~(ImGuiBackendFlags.RendererHasVtxOffset | ImGuiBackendFlags.RendererHasViewports);
Free(bd);
}
public static void NewFrame()
{
RenderData* bd = GetBackendData();
Debug.Assert(bd != null, "Context or backend not initialized! Did you call ImGui_ImplOpenGL3_Init()?");
if (bd->ShaderHandle == 0)
{
CreateDeviceObjects();
}
if (bd->FontTexture == 0)
{
CreateFontsTexture();
}
}
private static void SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height, uint vertex_array_object)
{
RenderData* bd = GetBackendData();
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill
GL.Enable(GLEnableCap.Blend);
GL.BlendEquation(GLBlendEquationModeEXT.FuncAdd);
GL.BlendFuncSeparate(GLBlendingFactor.SrcAlpha, GLBlendingFactor.OneMinusSrcAlpha, GLBlendingFactor.One, GLBlendingFactor.OneMinusSrcAlpha);
GL.Disable(GLEnableCap.CullFace);
GL.Disable(GLEnableCap.DepthTest);
GL.Disable(GLEnableCap.StencilTest);
GL.Enable(GLEnableCap.ScissorTest);
#if IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART
if (bd->GlVersion >= 310)
{
GL.Disable(GLEnableCap.PrimitiveRestart);
}
#endif
#if IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE
if (bd->HasPolygonMode)
{
GL.PolygonMode(GLTriangleFace.FrontAndBack, GLPolygonMode.Fill);
}
#endif
// Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT)
#if GL_CLIP_ORIGIN
bool clip_origin_lower_left = true;
if (bd->HasClipOrigin)
{
GLenum current_clip_origin = 0; GL.GetInteger(GL_CLIP_ORIGIN, (int*)¤t_clip_origin);
if (current_clip_origin == GL_UPPER_LEFT)
clip_origin_lower_left = false;
}
#endif
// Setup viewport, orthographic projection matrix
// 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.
/*GL_CALL*/
GL.Viewport(0, 0, fb_width, fb_height);
float L = draw_data->DisplayPos.X;
float R = draw_data->DisplayPos.X + draw_data->DisplaySize.X;
float T = draw_data->DisplayPos.Y;
float B = draw_data->DisplayPos.Y + draw_data->DisplaySize.Y;
#if GL_CLIP_ORIGIN
if (!clip_origin_lower_left) { float tmp = T; T = B; B = tmp; } // Swap top and bottom if origin is upper left
#endif
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, -1.0f, 0.0f,
(R + L) / (L - R), (T + B) / (B - T), 0.0f, 1.0f
);
GL.UseProgram(bd->ShaderHandle);
GL.Uniform1i(bd->AttribLocationTex, 0);
GL.UniformMatrix4fv(bd->AttribLocationProjMtx, 1, false, (float*)&mvp);
#if IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
if (bd->GlVersion >= 330 || bd->GlProfileIsES3)
{
GL.BindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 and GL ES 3.0 may set that otherwise.
}
#endif
#if IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
GL.BindVertexArray(vertex_array_object);
#endif
// Bind vertex/index buffers and setup attributes for ImDrawVert
/*GL_CALL*/
GL.BindBuffer(GLBufferTargetARB.ArrayBuffer, bd->VboHandle);
/*GL_CALL*/
GL.BindBuffer(GLBufferTargetARB.ElementArrayBuffer, bd->ElementsHandle);
/*GL_CALL*/
GL.EnableVertexAttribArray(bd->AttribLocationVtxPos);
/*GL_CALL*/
GL.EnableVertexAttribArray(bd->AttribLocationVtxUV);
/*GL_CALL*/
GL.EnableVertexAttribArray(bd->AttribLocationVtxColor);
/*GL_CALL*/
GL.VertexAttribPointer(bd->AttribLocationVtxPos, 2, GLVertexAttribPointerType.Float, false, sizeof(ImDrawVert), (void*)0);
/*GL_CALL*/
GL.VertexAttribPointer(bd->AttribLocationVtxUV, 2, GLVertexAttribPointerType.Float, false, sizeof(ImDrawVert), (void*)8);
/*GL_CALL*/
GL.VertexAttribPointer(bd->AttribLocationVtxColor, 4, GLVertexAttribPointerType.UnsignedByte, true, sizeof(ImDrawVert), (void*)16);
}
// OpenGL3 Render function.
// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly.
// This is in order to be able to run within an OpenGL engine that doesn't do so.
public static void RenderDrawData(ImDrawData* draw_data)
{
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
int fb_width = (int)(draw_data->DisplaySize.X * draw_data->FramebufferScale.X);
int fb_height = (int)(draw_data->DisplaySize.Y * draw_data->FramebufferScale.Y);
if (fb_width <= 0 || fb_height <= 0)
{
return;
}
RenderData* bd = GetBackendData();
// Backup GL state
GLTextureUnit last_active_texture; GL.GetIntegerv(GLGetPName.ActiveTexture, (int*)&last_active_texture);
GL.ActiveTexture(GLTextureUnit.Texture0);
uint last_program; GL.GetIntegerv(GLGetPName.CurrentProgram, (int*)&last_program);
uint last_texture; GL.GetIntegerv(GLGetPName.TextureBinding2D, (int*)&last_texture);
#if IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
uint last_sampler; if (bd->GlVersion >= 330 || bd->GlProfileIsES3) { GL.GetIntegerv(GLGetPName.SamplerBinding, (int*)&last_sampler); } else { last_sampler = 0; }
#endif
uint last_array_buffer; GL.GetIntegerv(GLGetPName.ArrayBufferBinding, (int*)&last_array_buffer);
#if !IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
// This is part of VAO on OpenGL 3.0+ and OpenGL ES 3.0+.
int last_element_array_buffer; GL.GetIntegerv(GLGetPName.ElementArrayBufferBinding, &last_element_array_buffer);
//ImGui_ImplOpenGL3_VtxAttribState last_vtx_attrib_state_pos = default; last_vtx_attrib_state_pos.GetState(bd->AttribLocationVtxPos);
//ImGui_ImplOpenGL3_VtxAttribState last_vtx_attrib_state_uv = default; last_vtx_attrib_state_uv.GetState(bd->AttribLocationVtxUV);
//ImGui_ImplOpenGL3_VtxAttribState last_vtx_attrib_state_color = default; last_vtx_attrib_state_color.GetState(bd->AttribLocationVtxColor);
#endif
#if IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
uint last_vertex_array_object; GL.GetIntegerv(GLGetPName.VertexArrayBinding, (int*)&last_vertex_array_object);
#endif
#if IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE
int* last_polygon_mode = stackalloc int[2]; if (bd->HasPolygonMode) { GL.GetIntegerv(GLGetPName.PolygonMode, last_polygon_mode); }
#endif
int* last_viewport = stackalloc int[4]; GL.GetIntegerv(GLGetPName.Viewport, last_viewport);
int* last_scissor_box = stackalloc int[4]; GL.GetIntegerv(GLGetPName.ScissorBox, last_scissor_box);
GLBlendingFactor last_blend_src_rgb; GL.GetIntegerv(GLGetPName.BlendSrcRgb, (int*)&last_blend_src_rgb);
GLBlendingFactor last_blend_dst_rgb; GL.GetIntegerv(GLGetPName.BlendDstRgb, (int*)&last_blend_dst_rgb);
GLBlendingFactor last_blend_src_alpha; GL.GetIntegerv(GLGetPName.BlendSrcAlpha, (int*)&last_blend_src_alpha);
GLBlendingFactor last_blend_dst_alpha; GL.GetIntegerv(GLGetPName.BlendDstAlpha, (int*)&last_blend_dst_alpha);
GLBlendEquationModeEXT last_blend_equation_rgb; GL.GetIntegerv(GLGetPName.BlendEquationRgb, (int*)&last_blend_equation_rgb);
GLBlendEquationModeEXT last_blend_equation_alpha; GL.GetIntegerv(GLGetPName.BlendEquationAlpha, (int*)&last_blend_equation_alpha);
bool last_enable_blend = GL.IsEnabled(GLEnableCap.Blend);
bool last_enable_cull_face = GL.IsEnabled(GLEnableCap.CullFace);
bool last_enable_depth_test = GL.IsEnabled(GLEnableCap.DepthTest);
bool last_enable_stencil_test = GL.IsEnabled(GLEnableCap.StencilTest);
bool last_enable_scissor_test = GL.IsEnabled(GLEnableCap.ScissorTest);
#if IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART
bool last_enable_primitive_restart = bd->GlVersion >= 310 ? GL.IsEnabled(GLEnableCap.PrimitiveRestart) : false;
#endif
// Setup desired GL state
// Recreate the VAO every time (this is to easily allow multiple GL contexts to be rendered to. VAO are not shared among GL contexts)
// The renderer would actually work without any VAO bound, but then our VertexAttrib calls would overwrite the default one currently bound.
uint vertex_array_object = 0;
#if IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
/*GL_CALL*/
GL.GenVertexArrays(1, &vertex_array_object);
#endif
SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object);
// Will project scissor/clipping rectangles into framebuffer space
Vector2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
Vector2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
// Render command lists
for (int n = 0; n < draw_data->CmdListsCount; n++)
{
ImDrawList* cmd_list = draw_data->CmdLists.Data[n];
// Upload vertex/index buffers
// - OpenGL drivers are in a very sorry state nowadays....
// During 2021 we attempted to switch from glBufferData() to orphaning+glBufferSubData() following reports
// of leaks on Intel GPU when using multi-viewports on Windows.
// - After this we kept hearing of various display corruptions issues. We started disabling on non-Intel GPU, but issues still got reported on Intel.
// - We are now back to using exclusively glBufferData(). So bd->UseBufferSubData IS ALWAYS FALSE in this code.
// We are keeping the old code path for a while in case people finding new issues may want to test the bd->UseBufferSubData path.
// - See https://github.com/ocornut/imgui/issues/4468 and please report any corruption issues.
nint vtx_buffer_size = cmd_list->VtxBuffer.Size * sizeof(ImDrawVert);
nint idx_buffer_size = cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx);
if (bd->UseBufferSubData)
{
if (bd->VertexBufferSize < vtx_buffer_size)
{
bd->VertexBufferSize = vtx_buffer_size;
/*GL_CALL*/
GL.BufferData(GLBufferTargetARB.ArrayBuffer, bd->VertexBufferSize, null, GLBufferUsageARB.StreamDraw);
}
if (bd->IndexBufferSize < idx_buffer_size)
{
bd->IndexBufferSize = idx_buffer_size;
/*GL_CALL*/
GL.BufferData(GLBufferTargetARB.ElementArrayBuffer, bd->IndexBufferSize, null, GLBufferUsageARB.StreamDraw);
}
/*GL_CALL*/
GL.BufferSubData(GLBufferTargetARB.ArrayBuffer, 0, vtx_buffer_size, cmd_list->VtxBuffer.Data);
/*GL_CALL*/
GL.BufferSubData(GLBufferTargetARB.ElementArrayBuffer, 0, idx_buffer_size, cmd_list->IdxBuffer.Data);
}
else
{
/*GL_CALL*/
GL.BufferData(GLBufferTargetARB.ArrayBuffer, vtx_buffer_size, cmd_list->VtxBuffer.Data, GLBufferUsageARB.StreamDraw);
/*GL_CALL*/
GL.BufferData(GLBufferTargetARB.ElementArrayBuffer, idx_buffer_size, cmd_list->IdxBuffer.Data, GLBufferUsageARB.StreamDraw);
}
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
{
ImDrawCmd pcmd = cmd_list->CmdBuffer[cmd_i];
if (pcmd.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)pcmd.UserCallback == ImGui.ImDrawCallbackResetRenderState)
{
SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object);
}
else
{
((delegate*<ImDrawList*, ImDrawCmd*, void>)pcmd.UserCallback)(cmd_list, &pcmd);
}
}
else
{
// Project scissor/clipping rectangles into framebuffer space
Vector2 clip_min = new((pcmd.ClipRect.X - clip_off.X) * clip_scale.X, (pcmd.ClipRect.Y - clip_off.Y) * clip_scale.Y);
Vector2 clip_max = new((pcmd.ClipRect.Z - clip_off.X) * clip_scale.X, (pcmd.ClipRect.W - clip_off.Y) * clip_scale.Y);
if (clip_max.X <= clip_min.X || clip_max.Y <= clip_min.Y)
{
continue;
}
// Apply scissor/clipping rectangle (Y is inverted in OpenGL)
/*GL_CALL*/
GL.Scissor((int)clip_min.X, (int)(fb_height - clip_max.Y), (int)(clip_max.X - clip_min.X), (int)(clip_max.Y - clip_min.Y));
// Bind texture, Draw
/*GL_CALL*/
GL.BindTexture(GLTextureTarget.Texture2D, (uint)pcmd.GetTexID().Handle);
#if IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET
if (bd->GlVersion >= 320)
{
/*GL_CALL*/
GL.DrawElementsBaseVertex(GLPrimitiveType.Triangles, (int)pcmd.ElemCount, sizeof(ImDrawIdx) == 2 ? GLDrawElementsType.UnsignedShort : GLDrawElementsType.UnsignedInt, (void*)(nint)(pcmd.IdxOffset * sizeof(ImDrawIdx)), (int)pcmd.VtxOffset);
}
else
#endif
/*GL_CALL*/
GL.DrawElements(GLPrimitiveType.Triangles, (int)pcmd.ElemCount, sizeof(ImDrawIdx) == 2 ? GLDrawElementsType.UnsignedShort : GLDrawElementsType.UnsignedInt, (void*)(nint)(pcmd.IdxOffset * sizeof(ImDrawIdx)));
}
}
}
// Destroy the temporary VAO
#if IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
/*GL_CALL*/
GL.DeleteVertexArrays(1, &vertex_array_object);
#endif
// Restore modified GL state
// This "glIsProgram()" check is required because if the program is "pending deletion" at the time of binding backup, it will have been deleted by now and will cause an OpenGL error. See #6220.
if (last_program == 0 || GL.IsProgram(last_program))
{
GL.UseProgram(last_program);
}
GL.BindTexture(GLTextureTarget.Texture2D, last_texture);
#if IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER
if (bd->GlVersion >= 330 || bd->GlProfileIsES3)
{
GL.BindSampler(0, last_sampler);
}
#endif
GL.ActiveTexture(last_active_texture);
#if IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
GL.BindVertexArray(last_vertex_array_object);
#endif
GL.BindBuffer(GLBufferTargetARB.ArrayBuffer, last_array_buffer);
#if !IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
GL.BindBuffer(GLBufferTargetARB.ElementArrayBuffer, (uint)last_element_array_buffer);
//last_vtx_attrib_state_pos.SetState(bd->AttribLocationVtxPos);
//last_vtx_attrib_state_uv.SetState(bd->AttribLocationVtxUV);
//last_vtx_attrib_state_color.SetState(bd->AttribLocationVtxColor);
#endif
GL.BlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha);
GL.BlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha);
if (last_enable_blend)
{
GL.Enable(GLEnableCap.Blend);
}
else
{
GL.Disable(GLEnableCap.Blend);
}
if (last_enable_cull_face)
{
GL.Enable(GLEnableCap.CullFace);
}
else
{
GL.Disable(GLEnableCap.CullFace);
}
if (last_enable_depth_test)
{
GL.Enable(GLEnableCap.DepthTest);
}
else
{
GL.Disable(GLEnableCap.DepthTest);
}
if (last_enable_stencil_test)
{
GL.Enable(GLEnableCap.StencilTest);
}
else
{
GL.Disable(GLEnableCap.StencilTest);
}
if (last_enable_scissor_test)
{
GL.Enable(GLEnableCap.ScissorTest);
}
else
{
GL.Disable(GLEnableCap.ScissorTest);
}
#if IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART
if (bd->GlVersion >= 310)
{
if (last_enable_primitive_restart)
{
GL.Enable(GLEnableCap.PrimitiveRestart);
}
else
{
GL.Disable(GLEnableCap.PrimitiveRestart);
}
}
#endif
#if IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE
// Desktop OpenGL 3.0 and OpenGL 3.1 had separate polygon draw modes for front-facing and back-facing faces of polygons
if (bd->HasPolygonMode) { if (bd->GlVersion <= 310 || bd->GlProfileIsCompat) { GL.PolygonMode(GLTriangleFace.Front, (GLPolygonMode)last_polygon_mode[0]); GL.PolygonMode(GLTriangleFace.Back, (GLPolygonMode)last_polygon_mode[1]); } else { GL.PolygonMode(GLTriangleFace.FrontAndBack, (GLPolygonMode)last_polygon_mode[0]); } }
#endif // IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE
GL.Viewport(last_viewport[0], last_viewport[1], (int)last_viewport[2], (int)last_viewport[3]);
GL.Scissor(last_scissor_box[0], last_scissor_box[1], (int)last_scissor_box[2], (int)last_scissor_box[3]);
}
private static bool CreateFontsTexture()
{
ImGuiIOPtr io = ImGui.GetIO();
RenderData* bd = GetBackendData();
// Build texture atlas
byte* pixels;
int width, height;
io.Fonts.GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory.
// Upload texture to graphics system
// (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
uint last_texture;
/*GL_CALL*/
GL.GetIntegerv(GLGetPName.TextureBinding2D, (int*)&last_texture);
/*GL_CALL*/
GL.GenTextures(1, &bd->FontTexture);
/*GL_CALL*/
GL.BindTexture(GLTextureTarget.Texture2D, bd->FontTexture);
/*GL_CALL*/
GL.TexParameteri(GLTextureTarget.Texture2D, GLTextureParameterName.MinFilter, (int)GLEnum.Linear);
/*GL_CALL*/
GL.TexParameteri(GLTextureTarget.Texture2D, GLTextureParameterName.MagFilter, (int)GLEnum.Linear);
#if GL_UNPACK_ROW_LENGTH // Not on WebGL/ES
/*GL_CALL*/(GL.PixelStore(GLEnum.UnpackRowLength, 0));
#endif
/*GL_CALL*/
GL.TexImage2D(GLTextureTarget.Texture2D, 0, GLInternalFormat.Rgba, width, height, 0, GLPixelFormat.Rgba, GLPixelType.UnsignedByte, pixels);
// Store our identifier
io.Fonts.SetTexID((ImTextureID)bd->FontTexture);
// Restore state
/*GL_CALL*/
GL.BindTexture(GLTextureTarget.Texture2D, last_texture);
return true;
}
private static void DestroyFontsTexture()
{
ImGuiIOPtr io = ImGui.GetIO();
RenderData* bd = GetBackendData();
if (bd->FontTexture != 0)
{
GL.DeleteTextures(1, &bd->FontTexture);
io.Fonts.SetTexID(0);
bd->FontTexture = 0;
}
}
// If you get an error please report on github. You may try different GL context version or GLSL version. See GL<>GLSL version table at the top of this file.
private static bool CheckShader(uint handle, string desc)
{
RenderData* bd = GetBackendData();
int status = 0, log_length = 0;
GL.GetShaderiv(handle, GLShaderParameterName.CompileStatus, &status);
GL.GetShaderiv(handle, GLShaderParameterName.InfoLogLength, &log_length);
if (status == 0)
{
Debug.WriteLine($"ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile {desc}! With GLSL: {ToStringFromUTF8(bd->GlslVersionString)}");
}
if (log_length > 1)
{
UnsafeList<byte> buf = default;
buf.Resize((int)log_length + 1);
GL.GetShaderInfoLog(handle, log_length, (int*)null, buf.Data);
Debug.WriteLine(ToStringFromUTF8(buf.Data));
buf.Release();
}
return status != 0;
}
// If you get an error please report on GitHub. You may try different GL context version or GLSL version.
private static bool CheckProgram(uint handle, string desc)
{
RenderData* bd = GetBackendData();
int status = 0, log_length = 0;
GL.GetProgramiv(handle, GLProgramPropertyARB.LinkStatus, &status);
GL.GetProgramiv(handle, GLProgramPropertyARB.InfoLogLength, &log_length);
if (status == 0)
{
Debug.WriteLine($"ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to link {desc}! With GLSL {ToStringFromUTF8(bd->GlslVersionString)}");
}
if (log_length > 1)
{
UnsafeList<byte> buf = default;
buf.Resize((int)log_length + 1);
GL.GetShaderInfoLog(handle, log_length, (int*)null, buf.Data);
Debug.WriteLine(ToStringFromUTF8(buf.Data));
buf.Release();
}
return status != 0;
}
private static bool CreateDeviceObjects()
{
RenderData* bd = GetBackendData();
// Backup GL state
int last_texture, last_array_buffer;
GL.GetIntegerv(GLGetPName.TextureBinding2D, &last_texture);
GL.GetIntegerv(GLGetPName.ArrayBufferBinding, &last_array_buffer);
#if IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_BUFFER_PIXEL_UNPACK
int last_pixel_unpack_buffer = 0;
if (bd->GlVersion >= 210) { GL.GetIntegerv(GLGetPName.PixelUnpackBufferBinding, &last_pixel_unpack_buffer); GL.BindBuffer(GLBufferTargetARB.PixelUnpackBuffer, 0); }
#endif
#if IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
int last_vertex_array;
GL.GetIntegerv(GLGetPName.VertexArrayBinding, &last_vertex_array);
#endif
// Parse GLSL version string
int glsl_version = 130;
if (bd->GlslVersionString != null && int.TryParse(ToStringFromUTF8(bd->GlslVersionString + 9), out var version))
{
glsl_version = version;
}
const string vertex_shader_glsl_120 = @"
uniform mat4 ProjMtx;
attribute vec2 Position;
attribute vec2 UV;
attribute vec4 Color;
varying vec2 Frag_UV;
varying vec4 Frag_Color;
void main()
{
Frag_UV = UV;
Frag_Color = Color;
gl_Position = ProjMtx * vec4(Position.xy, 0, 1);
}
";
const string vertex_shader_glsl_130 = @"
uniform mat4 ProjMtx;
in vec2 Position;
in vec2 UV;
in vec4 Color;
out vec2 Frag_UV;
out vec4 Frag_Color;
void main()
{
Frag_UV = UV;
Frag_Color = Color;
gl_Position = ProjMtx * vec4(Position.xy, 0, 1);
}
";
const string vertex_shader_glsl_300_es = @"
precision highp float;
layout (location = 0) in vec2 Position;
layout (location = 1) in vec2 UV;
layout (location = 2) in vec4 Color;
uniform mat4 ProjMtx;
out vec2 Frag_UV;
out vec4 Frag_Color;
void main()
{
Frag_UV = UV;
Frag_Color = Color;
gl_Position = ProjMtx * vec4(Position.xy, 0, 1);
}
";
const string vertex_shader_glsl_410_core = @"
layout (location = 0) in vec2 Position;
layout (location = 1) in vec2 UV;
layout (location = 2) in vec4 Color;
uniform mat4 ProjMtx;
out vec2 Frag_UV;
out vec4 Frag_Color;
void main()
{
Frag_UV = UV;
Frag_Color = Color;
gl_Position = ProjMtx * vec4(Position.xy, 0, 1);
}
";
const string fragment_shader_glsl_120 = @"
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D Texture;
varying vec2 Frag_UV;
varying vec4 Frag_Color;
void main()
{
gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st);
}
";
const string fragment_shader_glsl_130 = @"
uniform sampler2D Texture;
in vec2 Frag_UV;
in vec4 Frag_Color;
out vec4 Out_Color;
void main()
{
Out_Color = Frag_Color * texture(Texture, Frag_UV.st);
}
";
const string fragment_shader_glsl_300_es = @"
precision mediump float;
uniform sampler2D Texture;
in vec2 Frag_UV;
in vec4 Frag_Color;
layout (location = 0) out vec4 Out_Color;
void main()
{
Out_Color = Frag_Color * texture(Texture, Frag_UV.st);
}
";
const string fragment_shader_glsl_410_core = @"
in vec2 Frag_UV;
in vec4 Frag_Color;
uniform sampler2D Texture;
layout (location = 0) out vec4 Out_Color;
void main()
{
Out_Color = Frag_Color * texture(Texture, Frag_UV.st);
}
";
// Select shaders matching our GLSL versions
string? vertex_shader = null;
string? fragment_shader = null;
if (glsl_version < 130)
{
vertex_shader = vertex_shader_glsl_120;
fragment_shader = fragment_shader_glsl_120;
}
else if (glsl_version >= 410)
{
vertex_shader = vertex_shader_glsl_410_core;
fragment_shader = fragment_shader_glsl_410_core;
}
else if (glsl_version == 300)
{
vertex_shader = vertex_shader_glsl_300_es;
fragment_shader = fragment_shader_glsl_300_es;
}
else
{
vertex_shader = vertex_shader_glsl_130;
fragment_shader = fragment_shader_glsl_130;
}
// Create shaders
string vertex_shader_with_version = $"{ToStringFromUTF8(bd->GlslVersionString)}\n{vertex_shader}";
uint vert_handle = GL.CreateShader(GLShaderType.VertexShader);
GL.ShaderSource(vert_handle, vertex_shader_with_version);
GL.CompileShader(vert_handle);
CheckShader(vert_handle, "vertex shader");
string fragment_shader_with_version = $"{ToStringFromUTF8(bd->GlslVersionString)}\n{fragment_shader}";
uint frag_handle = GL.CreateShader(GLShaderType.FragmentShader);
GL.ShaderSource(frag_handle, fragment_shader_with_version);
GL.CompileShader(frag_handle);
CheckShader(frag_handle, "fragment shader");
// Link
bd->ShaderHandle = GL.CreateProgram();
GL.AttachShader(bd->ShaderHandle, vert_handle);
GL.AttachShader(bd->ShaderHandle, frag_handle);
GL.LinkProgram(bd->ShaderHandle);
CheckProgram(bd->ShaderHandle, "shader program");
GL.DetachShader(bd->ShaderHandle, vert_handle);
GL.DetachShader(bd->ShaderHandle, frag_handle);
GL.DeleteShader(vert_handle);
GL.DeleteShader(frag_handle);
bd->AttribLocationTex = GL.GetUniformLocation(bd->ShaderHandle, "Texture");
bd->AttribLocationProjMtx = GL.GetUniformLocation(bd->ShaderHandle, "ProjMtx");
bd->AttribLocationVtxPos = (uint)GL.GetAttribLocation(bd->ShaderHandle, "Position");
bd->AttribLocationVtxUV = (uint)GL.GetAttribLocation(bd->ShaderHandle, "UV");
bd->AttribLocationVtxColor = (uint)GL.GetAttribLocation(bd->ShaderHandle, "Color");
// Create buffers
GL.GenBuffers(1, &bd->VboHandle);
GL.GenBuffers(1, &bd->ElementsHandle);
CreateFontsTexture();
// Restore modified GL state
GL.BindTexture(GLTextureTarget.Texture2D, (uint)last_texture);
GL.BindBuffer(GLBufferTargetARB.ArrayBuffer, (uint)last_array_buffer);
#if IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_BUFFER_PIXEL_UNPACK
if (bd->GlVersion >= 210) { GL.BindBuffer(GLBufferTargetARB.PixelUnpackBuffer, (uint)last_pixel_unpack_buffer); }
#endif
#if IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY
GL.BindVertexArray((uint)last_vertex_array);
#endif
return true;
}
private static void DestroyDeviceObjects()
{
RenderData* bd = GetBackendData();
if (bd->VboHandle != 0) { GL.DeleteBuffers(1, &bd->VboHandle); bd->VboHandle = 0; }
if (bd->ElementsHandle != 0) { GL.DeleteBuffers(1, &bd->ElementsHandle); bd->ElementsHandle = 0; }
if (bd->ShaderHandle != 0) { GL.DeleteProgram(bd->ShaderHandle); bd->ShaderHandle = 0; }
DestroyFontsTexture();
}
//--------------------------------------------------------------------------------------------------------
// 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..
//--------------------------------------------------------------------------------------------------------
private static void RenderWindow(ImGuiViewport* viewport, void* v)
{
if ((viewport->Flags & ImGuiViewportFlags.NoRendererClear) == 0)
{
Vector4 clear_color = new(0.0f, 0.0f, 0.0f, 1.0f);
GL.ClearColor(clear_color.X, clear_color.Y, clear_color.Z, clear_color.W);
GL.Clear(GLClearBufferMask.ColorBufferBit);
}
RenderDrawData(viewport->DrawData);
}
private static void InitPlatformInterface()
{
ImGuiPlatformIOPtr platform_io = ImGui.GetPlatformIO();
platform_io.RendererRenderWindow = (void*)Marshal.GetFunctionPointerForDelegate<RendererRenderWindow>(RenderWindow);
}
private static void ShutdownPlatformInterface()
{
ImGui.DestroyPlatformWindows();
}
//-----------------------------------------------------------------------------
}
}