PasImGui.Renderer.OpenGL3.pas 40 KB

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