PasImGui.Renderer.OpenGL3.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. Unit PasImGui.Renderer.OpenGL3;
  2. {$IFDEF FPC}
  3. {$mode Delphi}{$H+}
  4. {$modeswitch anonymousfunctions}
  5. {$ENDIF}
  6. {$POINTERMATH ON}
  7. // Debugging
  8. {$Define IMGUI_OPENGL_DEBUG}
  9. // Specific OpenGL ES versions
  10. //{$Define IMGUI_OPENGL_ES2}
  11. //{$Define IMGUI_OPENGL_ES3}
  12. {$If not Defined(IMGUI_OPENGL_ES2) and not Defined(IMGUI_OPENGL_ES3)}
  13. {$If (Defined(DARWIN) or Defined(IOS)) or Defined(Android)}
  14. {$Define IMGUI_OPENGL_ES3}
  15. {$ElseIf defined(AMIGA)}
  16. {$Define IMGUI_OPENGL_ES2}
  17. {$EndIf}
  18. {$EndIf}
  19. //{$Define IMGUI_OPENGL_LOADER_CUSTOM} // If you have your own loader :V
  20. // We already know we have 3.+ :V
  21. {$Define GL_VERSION_3_1}
  22. {$Define GL_VERSION_3_2}
  23. {$Define GL_VERSION_3_3}
  24. // Desktop GL 2.0+ has glPolygonMode() which GL ES and WebGL don't have.
  25. {$Define IMGUI_HAS_POLYGON_MODE}
  26. // Vertex arrays are not supported on ES2/WebGL1 unless Emscripten which uses an extension
  27. {$IfNDef IMGUI_OPENGL_ES2}
  28. {$Define IMGUI_OPENGL_USE_VERTEX_ARRAY}
  29. {$EndIf}
  30. // Desktop GL 3.2+ has glDrawElementsBaseVertex() which GL ES and WebGL don't have.
  31. {$If not Defined(IMGUI_OPENGL_ES2) and not Defined(IMGUI_OPENGL_ES3) and Defined(GL_VERSION_3_2) }
  32. {$Define IMGUI_OPENGL_MAY_HAVE_VTX_OFFSET}
  33. {$EndIf}
  34. // Desktop GL use extension detection
  35. {$If not Defined(IMGUI_OPENGL_ES2) and not Defined(IMGUI_OPENGL_ES3)}
  36. {$Define IMGUI_OPENGL_MAY_HAVE_EXTENSIONS}
  37. {$EndIf}
  38. // Desktop GL 3.3+ and GL ES 3.0+ have glBindSampler()
  39. {$If not Defined(IMGUI_OPENGL_ES2) and not Defined(IMGUI_OPENGL_ES3) or Defined(GL_VERSION_3_2) }
  40. {$Define IMGUI_OPENGL_MAY_HAVE_BIND_SAMPLER}
  41. {$EndIf}
  42. {$If not Defined(IMGUI_OPENGL_ES2) and not Defined(IMGUI_OPENGL_ES3) or Defined(GL_VERSION_3_1) }
  43. {$Define IMGUI_OPENGL_MAY_HAVE_PRIMITIVE_RESTART}
  44. {$EndIf}
  45. Interface
  46. Uses
  47. SysUtils,
  48. glad_gl,
  49. PasImGui,
  50. PasImGui.Apis,
  51. PasImGui.Enums,
  52. PasImGui.Types,
  53. OpenGl3.Loader;
  54. Type
  55. // OpenGL Data
  56. ImGui_ImplOpenGL3_Data = Record
  57. GlVersion: GLuint;
  58. // Extracted at runtime using GL_MAJOR_VERSION, GL_MINOR_VERSION queries (e.g. 320 for GL 3.2)
  59. GlslVersionString: Array[0..31] Of AnsiChar;
  60. // Specified by user or detected based on compile time GL settings.
  61. GlProfileIsES2: Boolean;
  62. GlProfileIsES3: Boolean;
  63. GlProfileIsCompat: Boolean;
  64. GlProfileMask: GLint;
  65. FontTexture: GLuint;
  66. ShaderHandle: GLuint;
  67. AttribLocationTex: GLint; // Uniforms location
  68. AttribLocationProjMtx: GLint;
  69. AttribLocationVtxPos: GLuint; // Vertex attributes location
  70. AttribLocationVtxUV: GLuint;
  71. AttribLocationVtxColor: GLuint;
  72. VboHandle: GLuint;
  73. ElementsHandle: GLuint;
  74. VertexBufferSize: GLsizeiptr;
  75. IndexBufferSize: GLsizeiptr;
  76. HasClipOrigin: Boolean;
  77. UseBufferSubData: Boolean;
  78. End;
  79. PImGui_ImplOpenGL3_Data = ^ImGui_ImplOpenGL3_Data;
  80. procedure ImGui_OpenGL3_RenderDrawData(draw_data: PImDrawData);
  81. Function ImGui_OpenGL3_Init(glsl_version: PAnsiChar): Boolean;
  82. Procedure ImGui_OpenGL3_NewFrame();
  83. Procedure ImGui_OpenGL3_Shutdown();
  84. type
  85. TGLProc = reference to procedure;
  86. TError = reference to procedure(msg : string);
  87. Implementation
  88. procedure OnAssert(const Message, Filename: {$IfDef FPC}ShortString{$ELSE}string{$EndIf}; LineNumber: Integer; ErrorAddr: Pointer);
  89. begin
  90. raise EAssertionFailed.Create(Format('%s (%s, line %d)', [Message, Filename, LineNumber]));
  91. end;
  92. procedure GL_CALL(ACall : TGLProc; AError: TError);
  93. {$IfDef IMGUI_OPENGL_DEBUG}
  94. var
  95. gl_err : GLenum;
  96. {$EndIf}
  97. begin
  98. ACall();
  99. {$IfDef IMGUI_OPENGL_DEBUG}
  100. gl_err := glGetError();
  101. if (gl_err <> 0) then
  102. begin
  103. AError(Format('GL error 0x%x', [gl_err]));
  104. end;
  105. {$EndIf}
  106. end;
  107. Function ImGui_ImplOpenGL3_GetBackendData(): PImGui_ImplOpenGL3_Data;
  108. Begin
  109. If ImGui.GetCurrentContext() <> nil Then
  110. Result := PImGui_ImplOpenGL3_Data(ImGui.GetIO()^.BackendRendererUserData)
  111. Else
  112. Result := nil;
  113. End;
  114. procedure ImGui_ImplOpenGL3_SetupRenderState(draw_data: PImDrawData; fb_width: Integer; fb_height: Integer; vertex_array_object: GLuint);
  115. var
  116. bd: PImGui_ImplOpenGL3_Data;
  117. clip_origin_lower_left: Boolean;
  118. current_clip_origin: GLenum;
  119. L, R, T, B, tmp: Single;
  120. ortho_projection: array[0..3] of array[0..3] of Single;
  121. begin
  122. bd := ImGui_ImplOpenGL3_GetBackendData();
  123. // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill
  124. glEnable(GL_BLEND);
  125. glBlendEquation(GL_FUNC_ADD);
  126. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  127. glDisable(GL_CULL_FACE);
  128. glDisable(GL_DEPTH_TEST);
  129. glDisable(GL_STENCIL_TEST);
  130. glEnable(GL_SCISSOR_TEST);
  131. {$IfDef IMGUI_OPENGL_MAY_HAVE_PRIMITIVE_RESTART}
  132. if (bd^.GlVersion >= 310) then
  133. glDisable(GL_PRIMITIVE_RESTART);
  134. {$EndIf}
  135. {$IfDef IMGUI_HAS_POLYGON_MODE}
  136. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  137. {$EndIf}
  138. // Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT)
  139. if GLAD_GL_VERSION_4_5 then
  140. begin
  141. clip_origin_lower_left := True;
  142. if (bd^.HasClipOrigin) then
  143. begin
  144. current_clip_origin := GLenum(0);
  145. glGetIntegerv(GL_CLIP_ORIGIN, @GLint(current_clip_origin));
  146. if (current_clip_origin = GL_UPPER_LEFT) then
  147. begin
  148. clip_origin_lower_left := false;
  149. end;
  150. end;
  151. end;
  152. // Setup viewport, orthographic projection matrix
  153. // Our visible imgui space lies from draw_data^.DisplayPos (top left) to
  154. // draw_data^.DisplayPos+data_data^.DisplaySize (bottom right).
  155. // DisplayPos is (0,0) for single viewport apps.
  156. GL_CALL(procedure begin
  157. glViewport(0, 0, GLsizei(fb_width), GLsizei(fb_height));
  158. end, procedure(msg : string) begin Assert(False, msg); end);
  159. L := draw_data^.DisplayPos.x;
  160. R := draw_data^.DisplayPos.x + draw_data^.DisplaySize.x;
  161. T := draw_data^.DisplayPos.y;
  162. B := draw_data^.DisplayPos.y + draw_data^.DisplaySize.y;
  163. if GLAD_GL_VERSION_4_5 then
  164. begin
  165. if (not clip_origin_lower_left) then
  166. begin
  167. tmp := T;
  168. T := B;
  169. B := tmp;
  170. end; // Swap top and bottom if origin is upper left
  171. end;
  172. FillChar(ortho_projection, SizeOf(ortho_projection), 0);
  173. // Initialize the ortho_projection matrix
  174. ortho_projection[0][0] := 2.0 / (R - L);
  175. ortho_projection[1][1] := 2.0 / (T - B);
  176. ortho_projection[2][2] := -1.0;
  177. ortho_projection[3][0] := (R + L) / (L - R);
  178. ortho_projection[3][1] := (T + B) / (B - T);
  179. ortho_projection[3][3] := 1.0;
  180. glUseProgram(bd^.ShaderHandle);
  181. glUniform1i(bd^.AttribLocationTex, 0);
  182. glUniformMatrix4fv(bd^.AttribLocationProjMtx, 1, Boolean(GL_FALSE), @ortho_projection[0][0]);
  183. {$IfDef IMGUI_OPENGL_MAY_HAVE_BIND_SAMPLER}
  184. if (bd^.GlVersion >= 330) or (bd^.GlProfileIsES3) then
  185. glBindSampler(0, 0);
  186. // We use combined texture/sampler state. Applications using GL 3.3 and GL ES 3.0 may set that otherwise.
  187. {$EndIf}
  188. {$IfDef IMGUI_OPENGL_USE_VERTEX_ARRAY}
  189. glBindVertexArray(vertex_array_object);
  190. {$EndIf}
  191. // Bind vertex/index buffers and setup attributes for ImDrawVert
  192. GL_CALL(procedure begin glBindBuffer(GL_ARRAY_BUFFER, bd^.VboHandle); end, procedure(msg : string) begin Assert(False, msg); end);
  193. GL_CALL(procedure begin glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bd^.ElementsHandle); end, procedure(msg : string) begin Assert(False, msg); end);
  194. GL_CALL(procedure begin glEnableVertexAttribArray(bd^.AttribLocationVtxPos); end, procedure(msg : string) begin Assert(False, msg); end);
  195. GL_CALL(procedure begin glEnableVertexAttribArray(bd^.AttribLocationVtxUV); end, procedure(msg : string) begin Assert(False, msg); end);
  196. GL_CALL(procedure begin glEnableVertexAttribArray(bd^.AttribLocationVtxColor); end, procedure(msg : string) begin Assert(False, msg); end);
  197. glVertexAttribPointer(bd^.AttribLocationVtxPos, 2, GL_FLOAT, Boolean(GL_FALSE), sizeof(ImDrawVert), Pointer(IntPtr(@PImDrawVert(nil)^.pos)));
  198. glVertexAttribPointer(bd^.AttribLocationVtxUV, 2, GL_FLOAT, Boolean(GL_FALSE), sizeof(ImDrawVert), Pointer(IntPtr(@PImDrawVert(nil)^.uv)));
  199. glVertexAttribPointer(bd^.AttribLocationVtxColor, 4, GL_UNSIGNED_BYTE, Boolean(GL_TRUE), sizeof(ImDrawVert), Pointer(IntPtr(@PImDrawVert(nil)^.col)));
  200. end;
  201. // OpenGL3 Render function.
  202. // Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly.
  203. // This is in order to be able to run within an OpenGL engine that doesn't do so.
  204. procedure ImGui_OpenGL3_RenderDrawData(draw_data: PImDrawData);
  205. var
  206. fb_width, fb_height , n, cmd_i: Integer;
  207. bd: PImGui_ImplOpenGL3_Data;
  208. last_program, last_texture,
  209. last_sampler, last_array_buffer,
  210. last_vertex_array_object : GLuint;
  211. last_polygon_mode : Array [0..1] of GLint;
  212. last_viewport : Array [0..3] of GLint;
  213. last_scissor_box : Array [0..3] of GLint;
  214. last_active_texture, last_blend_src_rgb,
  215. last_blend_dst_rgb, last_blend_src_alpha,
  216. last_blend_dst_alpha, last_blend_equation_rgb,
  217. last_blend_equation_alpha : GLenum;
  218. last_enable_blend,
  219. last_enable_cull_face,
  220. last_enable_depth_test,
  221. last_enable_stencil_test,
  222. last_enable_scissor_test,
  223. last_enable_primitive_restart: GLboolean;
  224. vertex_array_object : GLuint;
  225. clip_off, clip_scale, clip_min, clip_max: ImVec2;
  226. cmd_list_ptr : ImDrawList;
  227. vtx_buffer_size, idx_buffer_size: GLsizeiptr;
  228. pcmd: ImDrawCmd;
  229. temp_callback : ImDrawCallback;
  230. Begin
  231. // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
  232. fb_width := Trunc(draw_data^.DisplaySize.x * draw_data^.FramebufferScale.x);
  233. fb_height := Trunc(draw_data^.DisplaySize.y * draw_data^.FramebufferScale.y);
  234. if (fb_width <= 0) or (fb_height <= 0) then
  235. Exit;
  236. bd := ImGui_ImplOpenGL3_GetBackendData();
  237. // Backup GL state
  238. glGetIntegerv(GL_ACTIVE_TEXTURE, @GLint(last_active_texture));
  239. glActiveTexture(GL_TEXTURE0);
  240. glGetIntegerv(GL_CURRENT_PROGRAM, @GLint(last_program));
  241. glGetIntegerv(GL_TEXTURE_BINDING_2D, @GLint(last_texture));
  242. {$IfDef IMGUI_OPENGL_MAY_HAVE_BIND_SAMPLER}
  243. if (bd^.GlVersion >= 330) or (bd^.GlProfileIsES3) then
  244. glGetIntegerv(GL_SAMPLER_BINDING, @GLint(last_sampler))
  245. else
  246. last_sampler := 0;
  247. {$EndIf}
  248. glGetIntegerv(GL_ARRAY_BUFFER_BINDING, @GLint(last_array_buffer));
  249. {$IfNDef IMGUI_OPENGL_USE_VERTEX_ARRAY}
  250. // This is part of VAO on OpenGL 3.0+ and OpenGL ES 3.0+.
  251. { TODO: Get Back to this later - Time : 11/17/2023 1:45:47 AM }
  252. {$EndIf}
  253. {$IfDef IMGUI_OPENGL_USE_VERTEX_ARRAY}
  254. glGetIntegerv(GL_VERTEX_ARRAY_BINDING, @GLint(last_vertex_array_object));
  255. {$EndIf}
  256. {$IfDef IMGUI_HAS_POLYGON_MODE}
  257. glGetIntegerv(GL_POLYGON_MODE, @last_polygon_mode);
  258. {$EndIf}
  259. glGetIntegerv(GL_VIEWPORT, @last_viewport);
  260. glGetIntegerv(GL_SCISSOR_BOX, @last_scissor_box);
  261. glGetIntegerv(GL_BLEND_SRC_RGB, @GLint(last_blend_src_rgb));
  262. glGetIntegerv(GL_BLEND_DST_RGB, @GLint(last_blend_dst_rgb));
  263. glGetIntegerv(GL_BLEND_SRC_ALPHA, @GLint(last_blend_src_alpha));
  264. glGetIntegerv(GL_BLEND_DST_ALPHA, @GLint(last_blend_dst_alpha));
  265. glGetIntegerv(GL_BLEND_EQUATION_RGB, @GLint(last_blend_equation_rgb));
  266. glGetIntegerv(GL_BLEND_EQUATION_ALPHA, @GLint(last_blend_equation_alpha));
  267. last_enable_blend := glIsEnabled(GL_BLEND);
  268. last_enable_cull_face := glIsEnabled(GL_CULL_FACE);
  269. last_enable_depth_test := glIsEnabled(GL_DEPTH_TEST);
  270. last_enable_stencil_test := glIsEnabled(GL_STENCIL_TEST);
  271. last_enable_scissor_test := glIsEnabled(GL_SCISSOR_TEST);
  272. {$IfDef IMGUI_OPENGL_MAY_HAVE_PRIMITIVE_RESTART}
  273. if (bd^.GlVersion >= 310) or (bd^.GlProfileIsES3) then
  274. last_enable_primitive_restart := glIsEnabled(GL_PRIMITIVE_RESTART)
  275. else
  276. last_enable_primitive_restart := Boolean(GL_FALSE);
  277. {$EndIf}
  278. // Setup desired GL state
  279. // Recreate the VAO every time (this is to easily allow multiple GL contexts
  280. // to be rendered to. VAO are not shared among GL contexts).
  281. // The renderer would actually work without any VAO bound, but then our
  282. // VertexAttrib calls would overwrite the default one currently bound.
  283. vertex_array_object := 0;
  284. {$IfDef IMGUI_OPENGL_USE_VERTEX_ARRAY}
  285. GL_CALL(procedure begin glGenVertexArrays(1, @vertex_array_object); end, procedure(msg : string) begin Assert(False, msg); end);
  286. {$EndIf}
  287. ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object);
  288. // Will project scissor/clipping rectangles into framebuffer space
  289. clip_off := draw_data^.DisplayPos; // (0,0) unless using multi-viewports
  290. clip_scale := draw_data^.FramebufferScale; // (1,1) unless using retina display which are often (2,2)
  291. // Render command lists
  292. for n := 0 to Pred(draw_data^.CmdListsCount) do
  293. begin
  294. //asm
  295. // int3
  296. //end;
  297. cmd_list_ptr := draw_data^.CmdLists.Data[n]^;
  298. // Upload vertex/index buffers
  299. // - OpenGL drivers are in a very sorry state nowadays....
  300. // During 2021 we attempted to switch from glBufferData() to orphaning+glBufferSubData() following reports
  301. // of leaks on Intel GPU when using multi-viewports on Windows.
  302. // - After this we kept hearing of various display corruptions issues. We started disabling on non-Intel GPU, but issues still got reported on Intel.
  303. // - We are now back to using exclusively glBufferData(). So bd^.UseBufferSubData IS ALWAYS FALSE in this code.
  304. // We are keeping the old code path for a while in case people finding new issues may want to test the bd^.UseBufferSubData path.
  305. // - See https://github.com/ocornut/imgui/issues/4468 and please report any corruption issues.
  306. vtx_buffer_size := GLsizeiptr(cmd_list_ptr.VtxBuffer.Size * Integer(SizeOf(ImDrawVert)));
  307. idx_buffer_size := GLsizeiptr(cmd_list_ptr.IdxBuffer.Size * Integer(sizeof(ImDrawIdx)));
  308. if (bd^.UseBufferSubData) then
  309. begin
  310. if (bd^.VertexBufferSize < vtx_buffer_size) then
  311. begin
  312. bd^.VertexBufferSize := vtx_buffer_size;
  313. GL_CALL(procedure begin glBufferData(GL_ARRAY_BUFFER, bd^.VertexBufferSize, nil, GL_STREAM_DRAW); end, procedure(msg : string) begin Assert(False, msg); end);
  314. end;
  315. if (bd^.IndexBufferSize < idx_buffer_size) then
  316. begin
  317. bd^.IndexBufferSize := idx_buffer_size;
  318. GL_CALL(procedure begin glBufferData(GL_ELEMENT_ARRAY_BUFFER, bd^.IndexBufferSize, nil, GL_STREAM_DRAW); end, procedure(msg : string) begin Assert(False, msg); end);
  319. end;
  320. GL_CALL(procedure begin glBufferSubData(GL_ARRAY_BUFFER, 0, vtx_buffer_size, cmd_list_ptr.VtxBuffer.Data); end, procedure(msg : string) begin Assert(False, msg); end);
  321. GL_CALL(procedure begin glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, idx_buffer_size, cmd_list_ptr.IdxBuffer.Data); end, procedure(msg : string) begin Assert(False, msg); end);
  322. end
  323. else
  324. begin
  325. GL_CALL(procedure begin glBufferData(GL_ARRAY_BUFFER, vtx_buffer_size, cmd_list_ptr.VtxBuffer.Data, GL_STREAM_DRAW); end, procedure(msg : string) begin Assert(False, msg); end);
  326. GL_CALL(procedure begin glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx_buffer_size, cmd_list_ptr.IdxBuffer.Data,GL_STREAM_DRAW); end, procedure(msg : string) begin Assert(False, msg); end);
  327. end;
  328. for cmd_i := 0 to Pred(cmd_list_ptr.CmdBuffer.Size) do
  329. begin
  330. pcmd := cmd_list_ptr.CmdBuffer.Data[cmd_i];
  331. temp_callback := pcmd.UserCallback;
  332. if Assigned(temp_callback) then
  333. begin
  334. // User callback, registered via ImDrawList::AddCallback()
  335. // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
  336. if (@pcmd.UserCallback = @ImDrawCallback_ResetRenderState) then
  337. ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object)
  338. else
  339. pcmd.UserCallback(@cmd_list_ptr, @pcmd);
  340. end
  341. else
  342. begin
  343. // Project scissor/clipping rectangles into framebuffer space
  344. clip_min := ImVec2.New((pcmd.ClipRect.x - clip_off.x) * clip_scale.x,
  345. (pcmd.ClipRect.y - clip_off.y) * clip_scale.y);
  346. clip_max := ImVec2.New((pcmd.ClipRect.z - clip_off.x) * clip_scale.x,
  347. (pcmd.ClipRect.w - clip_off.y) * clip_scale.y);
  348. if (clip_max.x <= clip_min.x) or (clip_max.y <= clip_min.y) then
  349. Continue;
  350. // Apply scissor/clipping rectangle (Y is inverted in OpenGL)
  351. GL_CALL(procedure begin glScissor(Trunc(clip_min.x), Trunc(Single(fb_height - clip_max.y)), Trunc(clip_max.x - clip_min.x), Trunc(clip_max.y - clip_min.y)); end, procedure(msg : string) begin Assert(False, msg); end);
  352. // Bind texture, Draw
  353. GL_CALL(procedure begin glBindTexture(GL_TEXTURE_2D, {%H-}GLuint(pcmd.GetTexID())); end, procedure(msg : string) begin Assert(False, msg); end);
  354. {$IfDef IMGUI_OPENGL_MAY_HAVE_VTX_OFFSET}
  355. if (bd^.GlVersion >= 320) then
  356. begin
  357. GL_CALL(
  358. procedure begin glDrawElementsBaseVertex(GL_TRIANGLES, GLsizei(pcmd.ElemCount), {$IfDef ImDrawIdx_32}GL_UNSIGNED_INT{$ELSE}GL_UNSIGNED_SHORT{$EndIf} ,Pointer(IntPtr(pcmd.IdxOffset * sizeof(ImDrawIdx))), GLint(pcmd.VtxOffset)); end,procedure(msg : string)begin Assert(False, msg); end);
  359. end
  360. else
  361. {$EndIf}
  362. GL_CALL(procedure begin glDrawElements(GL_TRIANGLES, GLsizei(pcmd.ElemCount), {$IfDef ImDrawIdx_32}GL_UNSIGNED_INT{$ELSE}GL_UNSIGNED_SHORT{$EndIf},Pointer(IntPtr(pcmd.IdxOffset * sizeof(ImDrawIdx)))); end,procedure(msg : string)begin Assert(False, msg);end);
  363. end;
  364. end;
  365. end;
  366. // Destroy the temporary VAO
  367. {$IfDef IMGUI_OPENGL_USE_VERTEX_ARRAY}
  368. GL_CALL(procedure begin glDeleteVertexArrays(1, @vertex_array_object); end, procedure(msg : string) begin Assert(False, msg); end);
  369. {$EndIf}
  370. // Restore modified GL state
  371. // This "glIsProgram()" check is required because if the program is
  372. // "pending deletion" at the time of binding backup, it will have been deleted by now and will cause an OpenGL error. See #6220.
  373. if (last_program = 0) or (glIsProgram(last_program)) then
  374. glUseProgram(last_program);
  375. glBindTexture(GL_TEXTURE_2D, last_texture);
  376. {$IfDef IMGUI_OPENGL_MAY_HAVE_BIND_SAMPLER}
  377. if (bd^.GlVersion >= 330) or (bd^.GlProfileIsES3) then
  378. glBindSampler(0, last_sampler);
  379. {$EndIf}
  380. glActiveTexture(last_active_texture);
  381. {$IfDef IMGUI_OPENGL_USE_VERTEX_ARRAY}
  382. glBindVertexArray(last_vertex_array_object);
  383. {$EndIf}
  384. glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
  385. {$IfnDef IMGUI_OPENGL_USE_VERTEX_ARRAY}
  386. { TODO: implement this - Time : 11/17/2023 5:18:38 AM }
  387. {$EndIf}
  388. glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha);
  389. glBlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha);
  390. if (last_enable_blend)then
  391. glEnable(GL_BLEND)
  392. else
  393. glDisable(GL_BLEND);
  394. if (last_enable_cull_face) then
  395. glEnable(GL_CULL_FACE)
  396. else
  397. glDisable(GL_CULL_FACE);
  398. if (last_enable_depth_test) then
  399. glEnable(GL_DEPTH_TEST)
  400. else
  401. glDisable(GL_DEPTH_TEST);
  402. if (last_enable_stencil_test) then
  403. glEnable(GL_STENCIL_TEST)
  404. else
  405. glDisable(GL_STENCIL_TEST);
  406. if (last_enable_scissor_test) then
  407. glEnable(GL_SCISSOR_TEST)
  408. else
  409. glDisable(GL_SCISSOR_TEST);
  410. {$IfDef IMGUI_OPENGL_MAY_HAVE_PRIMITIVE_RESTART}
  411. if (bd^.GlVersion >= 310) then
  412. if (last_enable_primitive_restart) then
  413. glEnable(GL_PRIMITIVE_RESTART)
  414. else
  415. glDisable(GL_PRIMITIVE_RESTART);
  416. {$EndIf}
  417. {$IfDef IMGUI_HAS_POLYGON_MODE}
  418. // Desktop OpenGL 3.0 and OpenGL 3.1 had separate polygon draw modes for front-facing and back-facing faces of polygons
  419. if (bd^.GlVersion <= 310) or (bd^.GlProfileIsCompat) then
  420. begin
  421. glPolygonMode(GL_FRONT, GLenum(last_polygon_mode[0]));
  422. glPolygonMode(GL_BACK, GLenum(last_polygon_mode[1]));
  423. end
  424. else
  425. glPolygonMode(GL_FRONT_AND_BACK, GLenum(last_polygon_mode[0]));
  426. {$EndIf}
  427. glViewport(last_viewport[0], last_viewport[1], GLsizei(last_viewport[2]), GLsizei(last_viewport[3]));
  428. glScissor(last_scissor_box[0], last_scissor_box[1], GLsizei(last_scissor_box[2]), GLsizei(last_scissor_box[3]));
  429. end;
  430. procedure ImGui_ImplOpenGL3_RenderWindow(viewport: PImGuiViewport; render_arg: Pointer); Cdecl;
  431. var
  432. clear_color : ImVec4;
  433. begin
  434. if not (viewport^.Flags and ImGuiViewportFlags_NoRendererClear <> 0) then
  435. begin
  436. clear_color := ImVec4.New(0.0, 0.0, 0.0, 1.0);
  437. glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
  438. glClear(GL_COLOR_BUFFER_BIT);
  439. end;
  440. ImGui_OpenGL3_RenderDrawData(viewport^.DrawData);
  441. end;
  442. procedure ImGui_ImplOpenGL3_InitPlatformInterface;
  443. var
  444. platform_io: PImGuiPlatformIO;
  445. begin
  446. platform_io := ImGui.GetPlatformIO();
  447. platform_io^.Renderer_RenderWindow := @ImGui_ImplOpenGL3_RenderWindow;
  448. end;
  449. function ImGui_OpenGL3_Init(glsl_version: PAnsiChar): Boolean;
  450. Var
  451. io: PImGuiIO;
  452. bd: PImGui_ImplOpenGL3_Data;
  453. num_extensions: GLint;
  454. major, minor, current_texture : GLint;
  455. gl_version_str, extension: PAnsiChar;
  456. i: Integer;
  457. Const
  458. VersionInfo =
  459. 'GlVersion = %d '#10 + 'GlProfileIsCompat = %d '#10 +
  460. 'GlProfileMask = 0x%X'#10 + 'GlProfileIsES2 = %d, GlProfileIsES3 = %d'#10 +
  461. 'GL_VENDOR = "%s"'#10 + 'GL_RENDERER = "%s"';
  462. Begin
  463. io := ImGui.GetIO();
  464. Assert(io^.BackendRendererUserData = nil, 'Already initialized a renderer backend!');
  465. // Initialize our loader
  466. {$If not Defined(IMGUI_OPENGL_ES2) and not Defined(IMGUI_OPENGL_ES3) and not Defined(IMGUI_OPENGL_LOADER_CUSTOM) }
  467. If Not ImGLInit() Then
  468. Begin
  469. If IsConsole Then
  470. WriteLn('Failed to initialize OpenGL loader!');
  471. Exit(False);
  472. End;
  473. {$EndIf}
  474. // Setup backend capabilities flags
  475. bd := AllocMem(SizeOf(ImGui_ImplOpenGL3_Data));
  476. io^.BackendRendererUserData := bd;
  477. io^.BackendRendererName := 'Pas_imgui_opengl3';
  478. // Query for GL version (e.g. 320 for GL 3.2)
  479. {$IFDEF IMGUI_OPENGL_ES2}
  480. // GLES 2
  481. bd^.GlVersion := 200;
  482. bd^.GlProfileIsES2 := True;
  483. {$ELSE}
  484. // Desktop or GLES 3
  485. major := 0;
  486. minor := 0;
  487. glGetIntegerv(GL_MAJOR_VERSION, @major);
  488. glGetIntegerv(GL_MINOR_VERSION, @minor);
  489. If (major = 0) And (minor = 0) Then
  490. Begin
  491. gl_version_str := PAnsiChar(glGetString(GL_VERSION));
  492. If IsConsole Then
  493. WriteLn(Format('%s %d.%d', [gl_version_str, major, minor]));
  494. End;
  495. bd^.GlVersion := GLuint(major * 100 + minor * 10);
  496. If (bd^.GlVersion >= 320) Then
  497. glGetIntegerv(GL_CONTEXT_PROFILE_MASK, @bd^.GlProfileMask);
  498. bd^.GlProfileIsCompat := (bd^.GlProfileMask And
  499. GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) <> 0;
  500. {$IfDef IMGUI_OPENGL_ES3}
  501. bd^.GlProfileIsES3 := true;
  502. {$EndIf}
  503. bd^.UseBufferSubData := False;
  504. {$ENDIF}
  505. {$IfDef IMGUI_OPENGL_DEBUG}
  506. if IsConsole then
  507. WriteLn(Format(VersionInfo, [bd^.GlVersion, Integer(bd^.GlProfileIsCompat),
  508. bd^.GlProfileMask, Integer(bd^.GlProfileIsES2), Integer(bd^.GlProfileIsES3),
  509. PAnsiChar(glGetString(GL_VENDOR)), PAnsiChar(glGetString(GL_RENDERER))])); // [DEBUG]
  510. {$EndIf}
  511. {$IfDef IMGUI_OPENGL_MAY_HAVE_VTX_OFFSET}
  512. // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
  513. if (bd^.GlVersion >= 320) then
  514. io^.BackendFlags := io^.BackendFlags or ImGuiBackendFlags_RendererHasVtxOffset;
  515. {$EndIf}
  516. // We can create multi-viewports on the Renderer side (optional)
  517. io^.BackendFlags := io^.BackendFlags or ImGuiBackendFlags_RendererHasViewports;
  518. // Store GLSL version string so we can refer to it later in case we recreate shaders.
  519. // Note: GLSL version is NOT the same as GL version. Leave this to nullptr if unsure.
  520. if glsl_version = nil then
  521. begin
  522. {$IFDEF IMGUI_OPENGL_ES2}
  523. glsl_version := '#version 100';
  524. {$ELSEIF DEFINED(IMGUI_OPENGL_ES3)}
  525. glsl_version := '#version 300 es';
  526. {$ELSEIF DEFINED(DARWIN)}
  527. glsl_version := '#version 150';
  528. {$ELSE}
  529. glsl_version := '#version 130';
  530. {$ENDIF}
  531. end;
  532. Assert(strlen(glsl_version) + 2 < Length(bd^.GlslVersionString), 'Ops');
  533. StrPCopy(bd^.GlslVersionString, glsl_version);
  534. StrCat(bd^.GlslVersionString, #10); // 1 = 1 as in C++ code :P
  535. // Make an arbitrary GL call (we don't actually need the result)
  536. // IF YOU GET A CRASH HERE: it probably means the OpenGL function loader didn't do its job. Let us know!
  537. glGetIntegerv(GL_TEXTURE_BINDING_2D, @current_texture);
  538. // Detect extensions we support
  539. bd^.HasClipOrigin := (bd^.GlVersion >= 450);
  540. {$IfDef IMGUI_OPENGL_MAY_HAVE_EXTENSIONS}
  541. {$IfDef IMGUI_OPENGL_DEBUG}
  542. if IsConsole then
  543. WriteLn('EXTENSIONS :');
  544. {$EndIf}
  545. glGetIntegerv(GL_NUM_EXTENSIONS, @num_extensions);
  546. for i := 0 to num_extensions - 1 do
  547. begin
  548. extension := PAnsiChar(glGetStringi(GL_EXTENSIONS, i));
  549. {$IfDef IMGUI_OPENGL_DEBUG}
  550. if IsConsole then
  551. WriteLn(' [+] ', extension);
  552. {$EndIf}
  553. if (extension <> nil) and (StrComp(extension, 'GL_ARB_clip_control') = 0) then
  554. bd^.HasClipOrigin := True;
  555. end;
  556. {$EndIf}
  557. If (io^.ConfigFlags And ImGuiConfigFlags_ViewportsEnable) <> 0 Then
  558. Begin
  559. ImGui_ImplOpenGL3_InitPlatformInterface();
  560. End;
  561. Result := True;
  562. End;
  563. function GetGLVersion(GlslVersionString : AnsiString; GLVersion : Integer = 130) : Integer;
  564. var
  565. Code: Integer;
  566. begin
  567. Result := GLVersion;
  568. // Extract the integer following '#version '
  569. if Pos('#version ', GlslVersionString) = 1 then
  570. begin
  571. // Delete the '#version ' part to isolate the number
  572. Delete(GlslVersionString, 1, Length('#version '));
  573. // Convert the remaining string to an integer
  574. Val(GlslVersionString, Result, Code);
  575. if Code <> 0 then
  576. Result := GLVersion;
  577. end;
  578. end;
  579. // 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.
  580. function CheckShader(handle: GLuint; desc: PAnsiChar) : Boolean;
  581. Var
  582. bd: PImGui_ImplOpenGL3_Data;
  583. status, log_length: GLint;
  584. buf : AnsiString;
  585. begin
  586. bd := ImGui_ImplOpenGL3_GetBackendData();
  587. status := 0; log_length := 0;
  588. glGetShaderiv(handle, GL_COMPILE_STATUS, @status);
  589. glGetShaderiv(handle, GL_INFO_LOG_LENGTH, @log_length);
  590. if boolean(status) = boolean(GL_FALSE) then
  591. begin
  592. if IsConsole then
  593. WriteLn(Format('ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile %s! With GLSL: %s', [desc, bd^.GlslVersionString]));
  594. end;
  595. if (log_length > 1) then
  596. begin
  597. buf := '';
  598. SetLength(buf, log_length);
  599. glGetShaderInfoLog(handle, log_length, nil, @buf);
  600. if IsConsole then
  601. WriteLn(Format('%s', [buf]));
  602. buf := '';
  603. end;
  604. Result := boolean(status) = boolean(GL_TRUE)
  605. end;
  606. // If you get an error please report on GitHub. You may try different GL context version or GLSL version.
  607. function CheckProgram(handle: GLuint; desc: PAnsiChar) : Boolean;
  608. Var
  609. bd: PImGui_ImplOpenGL3_Data;
  610. status, log_length: GLint;
  611. buf : AnsiString;
  612. begin
  613. bd := ImGui_ImplOpenGL3_GetBackendData();
  614. glGetProgramiv(handle, GL_LINK_STATUS, @status);
  615. glGetProgramiv(handle, GL_INFO_LOG_LENGTH, @log_length);
  616. if boolean(status) = boolean(GL_FALSE) then
  617. begin
  618. if IsConsole then
  619. WriteLn(Format('ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to link %s! With GLSL %s', [desc, bd^.GlslVersionString]));
  620. end;
  621. if (log_length > 1) then
  622. begin
  623. buf := '';
  624. SetLength(buf, log_length);
  625. glGetProgramInfoLog(handle, log_length, nil, @buf);
  626. if IsConsole then
  627. WriteLn(Format('%s', [buf]));
  628. buf := '';
  629. end;
  630. Result := boolean(status) = boolean(GL_TRUE)
  631. end;
  632. function ImGui_ImplOpenGL3_CreateFontsTexture() : Boolean;
  633. Var
  634. bd: PImGui_ImplOpenGL3_Data;
  635. io: PImGuiIO;
  636. pixels : PImU8;
  637. last_texture : GLint;
  638. width, height : Integer;
  639. begin
  640. io := ImGui.GetIO();
  641. bd := ImGui_ImplOpenGL3_GetBackendData();
  642. // Build texture atlas
  643. io^.Fonts^.GetTexDataAsRGBA32(@pixels, @width, @height);
  644. // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small)
  645. // 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.
  646. // Upload texture to graphics system
  647. // (Bilinear sampling is required by default.
  648. // Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling)
  649. glGetIntegerv(GL_TEXTURE_BINDING_2D, @last_texture);
  650. glGenTextures(1, @bd^.FontTexture);
  651. glBindTexture(GL_TEXTURE_2D, bd^.FontTexture);
  652. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  653. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  654. // Not on WebGL/ES
  655. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  656. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  657. // Store our identifier
  658. io.Fonts^.SetTexID(ImTextureID(IntPtr(bd^.FontTexture)));
  659. // Restore state
  660. glBindTexture(GL_TEXTURE_2D, last_texture);
  661. Result := True;
  662. end;
  663. function ImGui_ImplOpenGL3_CreateDeviceObjects : Boolean;
  664. Var
  665. bd: PImGui_ImplOpenGL3_Data;
  666. vert_handle, frag_handle : GLuint;
  667. last_texture, last_array_buffer, last_vertex_array : GLint;
  668. glsl_version : Integer;
  669. vertex_shader, fragment_shader : PGLchar;
  670. vertex_shader_with_version: array[0..1] of PAnsiChar;
  671. fragment_shader_with_version : array[0..1] of PAnsiChar;
  672. const
  673. vertex_shader_glsl_120: PGLchar =
  674. 'uniform mat4 ProjMtx;' + #10 +
  675. 'attribute vec2 Position;' + #10 +
  676. 'attribute vec2 UV;' + #10 +
  677. 'attribute vec4 Color;' + #10 +
  678. 'varying vec2 Frag_UV;' + #10 +
  679. 'varying vec4 Frag_Color;' + #10 +
  680. 'void main()' + #10 +
  681. '{' + #10 +
  682. ' Frag_UV = UV;' + #10 +
  683. ' Frag_Color = Color;' + #10 +
  684. ' gl_Position = ProjMtx * vec4(Position.xy,0,1);' + #10 +
  685. '}' + #10;
  686. vertex_shader_glsl_130: PGLchar =
  687. 'uniform mat4 ProjMtx;' + #10 +
  688. 'in vec2 Position;' + #10 +
  689. 'in vec2 UV;' + #10 +
  690. 'in vec4 Color;' + #10 +
  691. 'out vec2 Frag_UV;' + #10 +
  692. 'out vec4 Frag_Color;' + #10 +
  693. 'void main()' + #10 +
  694. '{' + #10 +
  695. ' Frag_UV = UV;' + #10 +
  696. ' Frag_Color = Color;' + #10 +
  697. ' gl_Position = ProjMtx * vec4(Position.xy,0,1);' + #10 +
  698. '}' + #10;
  699. vertex_shader_glsl_300_es: PGLchar =
  700. 'precision highp float;' + #10 +
  701. 'layout (location = 0) in vec2 Position;' + #10 +
  702. 'layout (location = 1) in vec2 UV;' + #10 +
  703. 'layout (location = 2) in vec4 Color;' + #10 +
  704. 'uniform mat4 ProjMtx;' + #10 +
  705. 'out vec2 Frag_UV;' + #10 +
  706. 'out vec4 Frag_Color;' + #10 +
  707. 'void main()' + #10 +
  708. '{' + #10 +
  709. ' Frag_UV = UV;' + #10 +
  710. ' Frag_Color = Color;' + #10 +
  711. ' gl_Position = ProjMtx * vec4(Position.xy,0,1);' + #10 +
  712. '}' + #10;
  713. vertex_shader_glsl_410_core: PGLchar =
  714. 'layout (location = 0) in vec2 Position;' + #10 +
  715. 'layout (location = 1) in vec2 UV;' + #10 +
  716. 'layout (location = 2) in vec4 Color;' + #10 +
  717. 'uniform mat4 ProjMtx;' + #10 +
  718. 'out vec2 Frag_UV;' + #10 +
  719. 'out vec4 Frag_Color;' + #10 +
  720. 'void main()' + #10 +
  721. '{' + #10 +
  722. ' Frag_UV = UV;' + #10 +
  723. ' Frag_Color = Color;' + #10 +
  724. ' gl_Position = ProjMtx * vec4(Position.xy,0,1);' + #10 +
  725. '}' + #10;
  726. fragment_shader_glsl_120: PGLchar =
  727. '#ifdef GL_ES' + #10 +
  728. ' precision mediump float;' + #10 +
  729. '#endif' + #10 +
  730. 'uniform sampler2D Texture;' + #10 +
  731. 'varying vec2 Frag_UV;' + #10 +
  732. 'varying vec4 Frag_Color;' + #10 +
  733. 'void main()' + #10 +
  734. '{' + #10 +
  735. ' gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st);' + #10 +
  736. '}' + #10;
  737. fragment_shader_glsl_130: PGLchar =
  738. 'uniform sampler2D Texture;' + #10 +
  739. 'in vec2 Frag_UV;' + #10 +
  740. 'in vec4 Frag_Color;' + #10 +
  741. 'out vec4 Out_Color;' + #10 +
  742. 'void main()' + #10 +
  743. '{' + #10 +
  744. ' Out_Color = Frag_Color * texture(Texture, Frag_UV.st);' + #10 +
  745. '}' + #10;
  746. fragment_shader_glsl_300_es: PGLchar =
  747. 'precision mediump float;' + #10 +
  748. 'uniform sampler2D Texture;' + #10 +
  749. 'in vec2 Frag_UV;' + #10 +
  750. 'in vec4 Frag_Color;' + #10 +
  751. 'layout (location = 0) out vec4 Out_Color;' + #10 +
  752. 'void main()' + #10 +
  753. '{' + #10 +
  754. ' Out_Color = Frag_Color * texture(Texture, Frag_UV.st);' + #10 +
  755. '}' + #10;
  756. fragment_shader_glsl_410_core: PGLchar =
  757. 'in vec2 Frag_UV;' + #10 +
  758. 'in vec4 Frag_Color;' + #10 +
  759. 'uniform sampler2D Texture;' + #10 +
  760. 'layout (location = 0) out vec4 Out_Color;' + #10 +
  761. 'void main()' + #10 +
  762. '{' + #10 +
  763. ' Out_Color = Frag_Color * texture(Texture, Frag_UV.st);' + #10 +
  764. '}' + #10;
  765. Begin
  766. bd := ImGui_ImplOpenGL3_GetBackendData();
  767. // Backup GL state
  768. glGetIntegerv(GL_TEXTURE_BINDING_2D, @last_texture);
  769. glGetIntegerv(GL_ARRAY_BUFFER_BINDING, @last_array_buffer);
  770. {$IfDef IMGUI_OPENGL_USE_VERTEX_ARRAY}
  771. glGetIntegerv(GL_VERTEX_ARRAY_BINDING, @last_vertex_array);
  772. {$EndIf}
  773. // Parse GLSL version string
  774. glsl_version := 130;
  775. glsl_version := GetGLVersion(AnsiString(bd^.GlslVersionString), glsl_version);
  776. // Select shaders matching our GLSL versions
  777. if glsl_version < 130 then
  778. begin
  779. vertex_shader := vertex_shader_glsl_120;
  780. fragment_shader := fragment_shader_glsl_120;
  781. end
  782. else if glsl_version >= 410 then
  783. begin
  784. vertex_shader := vertex_shader_glsl_410_core;
  785. fragment_shader := fragment_shader_glsl_410_core;
  786. end
  787. else if glsl_version = 300 then
  788. begin
  789. vertex_shader := vertex_shader_glsl_300_es;
  790. fragment_shader := fragment_shader_glsl_300_es;
  791. end
  792. else
  793. begin
  794. vertex_shader := vertex_shader_glsl_130;
  795. fragment_shader := fragment_shader_glsl_130;
  796. end;
  797. // Create shaders
  798. vertex_shader_with_version[0] := @bd^.GlslVersionString[0];
  799. vertex_shader_with_version[1] := vertex_shader;
  800. vert_handle := glCreateShader(GL_VERTEX_SHADER);
  801. glShaderSource(vert_handle, 2, @vertex_shader_with_version, nil);
  802. glCompileShader(vert_handle);
  803. CheckShader(vert_handle, 'vertex shader');
  804. fragment_shader_with_version[0] := @bd^.GlslVersionString[0];
  805. fragment_shader_with_version[1] := fragment_shader;
  806. frag_handle := glCreateShader(GL_FRAGMENT_SHADER);
  807. glShaderSource(frag_handle, 2, @fragment_shader_with_version, nil);
  808. glCompileShader(frag_handle);
  809. CheckShader(frag_handle, 'fragment shader');
  810. // Link
  811. bd^.ShaderHandle := glCreateProgram();
  812. glAttachShader(bd^.ShaderHandle, vert_handle);
  813. glAttachShader(bd^.ShaderHandle, frag_handle);
  814. glLinkProgram(bd^.ShaderHandle);
  815. CheckProgram(bd^.ShaderHandle, 'shader program');
  816. glDetachShader(bd^.ShaderHandle, vert_handle);
  817. glDetachShader(bd^.ShaderHandle, frag_handle);
  818. glDeleteShader(vert_handle);
  819. glDeleteShader(frag_handle);
  820. bd^.AttribLocationTex := glGetUniformLocation(bd^.ShaderHandle, 'Texture');
  821. bd^.AttribLocationProjMtx := glGetUniformLocation(bd^.ShaderHandle, 'ProjMtx');
  822. bd^.AttribLocationVtxPos := GLuint(glGetAttribLocation(bd^.ShaderHandle, 'Position'));
  823. bd^.AttribLocationVtxUV := GLuint(glGetAttribLocation(bd^.ShaderHandle, 'UV'));
  824. bd^.AttribLocationVtxColor := GLuint(glGetAttribLocation(bd^.ShaderHandle, 'Color'));
  825. // Create buffers
  826. glGenBuffers(1, @bd^.VboHandle);
  827. glGenBuffers(1, @bd^.ElementsHandle);
  828. ImGui_ImplOpenGL3_CreateFontsTexture();
  829. // Restore modified GL state
  830. glBindTexture(GL_TEXTURE_2D, last_texture);
  831. glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer);
  832. {$IfDef IMGUI_OPENGL_USE_VERTEX_ARRAY}
  833. glBindVertexArray(last_vertex_array);
  834. {$EndIf}
  835. Result := True;
  836. end;
  837. procedure ImGui_OpenGL3_NewFrame;
  838. Var
  839. bd: PImGui_ImplOpenGL3_Data;
  840. Begin
  841. bd := ImGui_ImplOpenGL3_GetBackendData();
  842. Assert(bd <> nil, 'Did you call ImGui_ImplOpenGL3_Init()?');
  843. if (bd^.ShaderHandle = 0) then
  844. ImGui_ImplOpenGL3_CreateDeviceObjects();
  845. End;
  846. procedure ImGui_ImplOpenGL3_ShutdownPlatformInterface();
  847. begin
  848. ImGui.DestroyPlatformWindows();
  849. end;
  850. procedure ImGui_ImplOpenGL3_DestroyFontsTexture;
  851. Var
  852. bd: PImGui_ImplOpenGL3_Data;
  853. io: PImGuiIO;
  854. Begin
  855. bd := ImGui_ImplOpenGL3_GetBackendData();
  856. io := imgui.GetIO();
  857. if (bd^.FontTexture > 0) then
  858. begin
  859. glDeleteTextures(1, @bd^.FontTexture);
  860. io^.Fonts^.SetTexID(nil);
  861. bd^.FontTexture := 0;
  862. end;
  863. end;
  864. procedure ImGui_ImplOpenGL3_DestroyDeviceObjects;
  865. Var
  866. bd: PImGui_ImplOpenGL3_Data;
  867. Begin
  868. bd := ImGui_ImplOpenGL3_GetBackendData();
  869. if (bd^.VboHandle > 0) then
  870. begin
  871. glDeleteBuffers(1, @bd^.VboHandle);
  872. bd^.VboHandle := 0;
  873. end;
  874. if (bd^.ElementsHandle > 0) then
  875. begin
  876. glDeleteBuffers(1, @bd^.ElementsHandle);
  877. bd^.ElementsHandle := 0;
  878. end;
  879. if (bd^.ShaderHandle > 0) then
  880. begin
  881. glDeleteProgram(bd^.ShaderHandle);
  882. bd^.ShaderHandle := 0;
  883. end;
  884. ImGui_ImplOpenGL3_DestroyFontsTexture();
  885. end;
  886. procedure ImGui_OpenGL3_Shutdown;
  887. Var
  888. bd: PImGui_ImplOpenGL3_Data;
  889. io: PImGuiIO;
  890. Begin
  891. bd := ImGui_ImplOpenGL3_GetBackendData();
  892. Assert(bd <> nil, 'No renderer backend to shutdown, or already shutdown?');
  893. io := imgui.GetIO();
  894. ImGui_ImplOpenGL3_ShutdownPlatformInterface();
  895. ImGui_ImplOpenGL3_DestroyDeviceObjects();
  896. io^.BackendRendererName := nil;
  897. io^.BackendRendererUserData := nil;
  898. io^.BackendFlags := io^.BackendFlags and not(ImGuiBackendFlags_RendererHasVtxOffset or ImGuiBackendFlags_RendererHasViewports);
  899. // Free Allocated data
  900. Freemem(bd);
  901. End;
  902. {$IfDef IMGUI_OPENGL_DEBUG}
  903. initialization
  904. AssertErrorProc := @OnAssert;
  905. {$EndIf}
  906. End.