glx.pp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. {
  2. Translation of the Mesa GLX headers for FreePascal
  3. Copyright (C) 1999 Sebastian Guenther
  4. Mesa 3-D graphics library
  5. Version: 3.0
  6. Copyright (C) 1995-1998 Brian Paul
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public
  9. License as published by the Free Software Foundation; either
  10. version 2 of the License, or (at your option) any later version.
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free
  17. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. }
  19. {$MODE delphi} // objfpc would not work because of direct proc var assignments
  20. {You have to enable Macros (compiler switch "-Sm") for compiling this unit!
  21. This is necessary for supporting different platforms with different calling
  22. conventions via a single unit.}
  23. unit GLX;
  24. interface
  25. {$MACRO ON}
  26. {$IFDEF Unix}
  27. uses
  28. X, XLib, XUtil;
  29. {$DEFINE HasGLX} // Activate GLX stuff
  30. {$ELSE}
  31. {$MESSAGE Unsupported platform.}
  32. {$ENDIF}
  33. {$IFNDEF HasGLX}
  34. {$MESSAGE GLX not present on this platform.}
  35. {$ENDIF}
  36. // =======================================================
  37. // Unit specific extensions
  38. // =======================================================
  39. // Note: Requires that the GL library has already been initialized
  40. function InitGLX: Boolean;
  41. var
  42. GLXDumpUnresolvedFunctions,
  43. GLXInitialized: Boolean;
  44. // =======================================================
  45. // GLX consts, types and functions
  46. // =======================================================
  47. // Tokens for glXChooseVisual and glXGetConfig:
  48. const
  49. GLX_USE_GL = 1;
  50. GLX_BUFFER_SIZE = 2;
  51. GLX_LEVEL = 3;
  52. GLX_RGBA = 4;
  53. GLX_DOUBLEBUFFER = 5;
  54. GLX_STEREO = 6;
  55. GLX_AUX_BUFFERS = 7;
  56. GLX_RED_SIZE = 8;
  57. GLX_GREEN_SIZE = 9;
  58. GLX_BLUE_SIZE = 10;
  59. GLX_ALPHA_SIZE = 11;
  60. GLX_DEPTH_SIZE = 12;
  61. GLX_STENCIL_SIZE = 13;
  62. GLX_ACCUM_RED_SIZE = 14;
  63. GLX_ACCUM_GREEN_SIZE = 15;
  64. GLX_ACCUM_BLUE_SIZE = 16;
  65. GLX_ACCUM_ALPHA_SIZE = 17;
  66. // GLX_EXT_visual_info extension
  67. GLX_X_VISUAL_TYPE_EXT = $22;
  68. GLX_TRANSPARENT_TYPE_EXT = $23;
  69. GLX_TRANSPARENT_INDEX_VALUE_EXT = $24;
  70. GLX_TRANSPARENT_RED_VALUE_EXT = $25;
  71. GLX_TRANSPARENT_GREEN_VALUE_EXT = $26;
  72. GLX_TRANSPARENT_BLUE_VALUE_EXT = $27;
  73. GLX_TRANSPARENT_ALPHA_VALUE_EXT = $28;
  74. // Error codes returned by glXGetConfig:
  75. GLX_BAD_SCREEN = 1;
  76. GLX_BAD_ATTRIBUTE = 2;
  77. GLX_NO_EXTENSION = 3;
  78. GLX_BAD_VISUAL = 4;
  79. GLX_BAD_CONTEXT = 5;
  80. GLX_BAD_VALUE = 6;
  81. GLX_BAD_ENUM = 7;
  82. // GLX 1.1 and later:
  83. GLX_VENDOR = 1;
  84. GLX_VERSION = 2;
  85. GLX_EXTENSIONS = 3;
  86. // GLX_visual_info extension
  87. GLX_TRUE_COLOR_EXT = $8002;
  88. GLX_DIRECT_COLOR_EXT = $8003;
  89. GLX_PSEUDO_COLOR_EXT = $8004;
  90. GLX_STATIC_COLOR_EXT = $8005;
  91. GLX_GRAY_SCALE_EXT = $8006;
  92. GLX_STATIC_GRAY_EXT = $8007;
  93. GLX_NONE_EXT = $8000;
  94. GLX_TRANSPARENT_RGB_EXT = $8008;
  95. GLX_TRANSPARENT_INDEX_EXT = $8009;
  96. type
  97. // From XLib:
  98. XPixmap = TXID;
  99. XFont = TXID;
  100. XColormap = TXID;
  101. GLXContext = Pointer;
  102. GLXPixmap = TXID;
  103. GLXDrawable = TXID;
  104. GLXContextID = TXID;
  105. var
  106. glXChooseVisual: function(dpy: PDisplay; screen: Integer; attribList: PInteger): PXVisualInfo; cdecl;
  107. glXCreateContext: function(dpy: PDisplay; vis: PXVisualInfo; shareList: GLXContext; direct: Boolean): GLXContext; cdecl;
  108. glXDestroyContext: procedure(dpy: PDisplay; ctx: GLXContext); cdecl;
  109. glXMakeCurrent: function(dpy: PDisplay; drawable: GLXDrawable; ctx: GLXContext): Boolean; cdecl;
  110. glXCopyContext: procedure(dpy: PDisplay; src, dst: GLXContext; mask: LongWord); cdecl;
  111. glXSwapBuffers: procedure(dpy: PDisplay; drawable: GLXDrawable); cdecl;
  112. glXCreateGLXPixmap: function(dpy: PDisplay; visual: PXVisualInfo; pixmap: XPixmap): GLXPixmap; cdecl;
  113. glXDestroyGLXPixmap: procedure(dpy: PDisplay; pixmap: GLXPixmap); cdecl;
  114. glXQueryExtension: function(dpy: PDisplay; var errorb, event: Integer): Boolean; cdecl;
  115. glXQueryVersion: function(dpy: PDisplay; var maj, min: Integer): Boolean; cdecl;
  116. glXIsDirect: function(dpy: PDisplay; ctx: GLXContext): Boolean; cdecl;
  117. glXGetConfig: function(dpy: PDisplay; visual: PXVisualInfo; attrib: Integer; var value: Integer): Integer; cdecl;
  118. glXGetCurrentContext: function: GLXContext; cdecl;
  119. glXGetCurrentDrawable: function: GLXDrawable; cdecl;
  120. glXWaitGL: procedure; cdecl;
  121. glXWaitX: procedure; cdecl;
  122. glXUseXFont: procedure(font: XFont; first, count, list: Integer); cdecl;
  123. // GLX 1.1 and later
  124. glXQueryExtensionsString: function(dpy: PDisplay; screen: Integer): PChar; cdecl;
  125. glXQueryServerString: function(dpy: PDisplay; screen, name: Integer): PChar; cdecl;
  126. glXGetClientString: function(dpy: PDisplay; name: Integer): PChar; cdecl;
  127. // Mesa GLX Extensions
  128. glXCreateGLXPixmapMESA: function(dpy: PDisplay; visual: PXVisualInfo; pixmap: XPixmap; cmap: XColormap): GLXPixmap; cdecl;
  129. glXReleaseBufferMESA: function(dpy: PDisplay; d: GLXDrawable): Boolean; cdecl;
  130. glXCopySubBufferMESA: procedure(dpy: PDisplay; drawbale: GLXDrawable; x, y, width, height: Integer); cdecl;
  131. glXGetVideoSyncSGI: function(var counter: LongWord): Integer; cdecl;
  132. glXWaitVideoSyncSGI: function(divisor, remainder: Integer; var count: LongWord): Integer; cdecl;
  133. // =======================================================
  134. //
  135. // =======================================================
  136. implementation
  137. {$LINKLIB m}
  138. function dlopen(AFile: PChar; mode: LongInt): Pointer; external 'dl';
  139. function dlclose(handle: Pointer): LongInt; external 'dl';
  140. function dlsym(handle: Pointer; name: PChar): Pointer; external 'dl';
  141. function LoadLibrary(name: PChar): Pointer;
  142. begin
  143. Result := dlopen(name, $101 {RTLD_GLOBAL or RTLD_LAZY});
  144. end;
  145. function GetProc(handle: Pointer; name: PChar): Pointer;
  146. begin
  147. Result := dlsym(handle, name);
  148. if (Result = nil) and GLXDumpUnresolvedFunctions then
  149. WriteLn('Unresolved: ', name);
  150. end;
  151. var
  152. libGLX: Pointer;
  153. function InitGLXFromLibrary(libname:pchar): Boolean;
  154. begin
  155. Result := False;
  156. libGLX := LoadLibrary(libname);
  157. if not Assigned(libGLX) then exit;
  158. glXChooseVisual := GetProc(libglx, 'glXChooseVisual');
  159. glXCreateContext := GetProc(libglx, 'glXCreateContext');
  160. glXDestroyContext := GetProc(libglx, 'glXDestroyContext');
  161. glXMakeCurrent := GetProc(libglx, 'glXMakeCurrent');
  162. glXCopyContext := GetProc(libglx, 'glXCopyContext');
  163. glXSwapBuffers := GetProc(libglx, 'glXSwapBuffers');
  164. glXCreateGLXPixmap := GetProc(libglx, 'glXCreateGLXPixmap');
  165. glXDestroyGLXPixmap := GetProc(libglx, 'glXDestroyGLXPixmap');
  166. glXQueryExtension := GetProc(libglx, 'glXQueryExtension');
  167. glXQueryVersion := GetProc(libglx, 'glXQueryVersion');
  168. glXIsDirect := GetProc(libglx, 'glXIsDirect');
  169. glXGetConfig := GetProc(libglx, 'glXGetConfig');
  170. glXGetCurrentContext := GetProc(libglx, 'glXGetCurrentContext');
  171. glXGetCurrentDrawable := GetProc(libglx, 'glXGetCurrentDrawable');
  172. glXWaitGL := GetProc(libglx, 'glXWaitGL');
  173. glXWaitX := GetProc(libglx, 'glXWaitX');
  174. glXUseXFont := GetProc(libglx, 'glXUseXFont');
  175. // GLX 1.1 and later
  176. glXQueryExtensionsString := GetProc(libglx, 'glXQueryExtensionsString');
  177. glXQueryServerString := GetProc(libglx, 'glXQueryServerString');
  178. glXGetClientString := GetProc(libglx, 'glXGetClientString');
  179. // Mesa GLX Extensions
  180. glXCreateGLXPixmapMESA := GetProc(libglx, 'glXCreateGLXPixmapMESA');
  181. glXReleaseBufferMESA := GetProc(libglx, 'glXReleaseBufferMESA');
  182. glXCopySubBufferMESA := GetProc(libglx, 'glXCopySubBufferMESA');
  183. glXGetVideoSyncSGI := GetProc(libglx, 'glXGetVideoSyncSGI');
  184. glXWaitVideoSyncSGI := GetProc(libglx, 'glXWaitVideoSyncSGI');
  185. GLXInitialized := True;
  186. Result := True;
  187. end;
  188. function InitGLX: Boolean;
  189. begin
  190. Result := InitGLXFromLibrary('libGL.so') or
  191. InitGLXFromLibrary('libGL.so.1') or
  192. InitGLXFromLibrary('libMesaGL.so') or
  193. InitGLXFromLibrary('libMesaGL.so.3');
  194. end;
  195. initialization
  196. InitGLX;
  197. finalization
  198. if Assigned(libGLX) then dlclose(libGLX);
  199. end.