A little toolkit for single-quad fragment shader demos
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
8.7 KiB

  1. #!/usr/bin/env python
  2. # This file is part of gl3w, hosted at https://github.com/skaslev/gl3w
  3. #
  4. # This is free and unencumbered software released into the public domain.
  5. #
  6. # Anyone is free to copy, modify, publish, use, compile, sell, or
  7. # distribute this software, either in source code form or as a compiled
  8. # binary, for any purpose, commercial or non-commercial, and by any
  9. # means.
  10. #
  11. # In jurisdictions that recognize copyright laws, the author or authors
  12. # of this software dedicate any and all copyright interest in the
  13. # software to the public domain. We make this dedication for the benefit
  14. # of the public at large and to the detriment of our heirs and
  15. # successors. We intend this dedication to be an overt act of
  16. # relinquishment in perpetuity of all present and future rights to this
  17. # software under copyright law.
  18. #
  19. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  22. # IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23. # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. # OTHER DEALINGS IN THE SOFTWARE.
  26. # Allow Python 2.6+ to use the print() function
  27. from __future__ import print_function
  28. import argparse
  29. import os
  30. import re
  31. # Try to import Python 3 library urllib.request
  32. # and if it fails, fall back to Python 2 urllib2
  33. try:
  34. import urllib.request as urllib2
  35. except ImportError:
  36. import urllib2
  37. # UNLICENSE copyright header
  38. UNLICENSE = r'''/*
  39. This file was generated with gl3w_gen.py, part of gl3w
  40. (hosted at https://github.com/skaslev/gl3w)
  41. This is free and unencumbered software released into the public domain.
  42. Anyone is free to copy, modify, publish, use, compile, sell, or
  43. distribute this software, either in source code form or as a compiled
  44. binary, for any purpose, commercial or non-commercial, and by any
  45. means.
  46. In jurisdictions that recognize copyright laws, the author or authors
  47. of this software dedicate any and all copyright interest in the
  48. software to the public domain. We make this dedication for the benefit
  49. of the public at large and to the detriment of our heirs and
  50. successors. We intend this dedication to be an overt act of
  51. relinquishment in perpetuity of all present and future rights to this
  52. software under copyright law.
  53. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  54. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  55. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  56. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  57. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  58. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  59. OTHER DEALINGS IN THE SOFTWARE.
  60. */
  61. '''
  62. EXT_SUFFIX = ['ARB', 'EXT', 'KHR', 'OVR', 'NV', 'AMD', 'INTEL']
  63. def is_ext(proc):
  64. return any(proc.endswith(suffix) for suffix in EXT_SUFFIX)
  65. def proc_t(proc):
  66. return {
  67. 'p': proc,
  68. 'p_s': proc[2:],
  69. 'p_t': 'PFN{0}PROC'.format(proc.upper())
  70. }
  71. def write(f, s):
  72. f.write(s.encode('utf-8'))
  73. parser = argparse.ArgumentParser(description='gl3w generator script')
  74. parser.add_argument('--ext', action='store_true', help='Load extensions')
  75. parser.add_argument('--root', type=str, default='', help='Root directory')
  76. args = parser.parse_args()
  77. # Create directories
  78. if not os.path.exists(os.path.join(args.root, 'include/GL')):
  79. os.makedirs(os.path.join(args.root, 'include/GL'))
  80. if not os.path.exists(os.path.join(args.root, 'src')):
  81. os.makedirs(os.path.join(args.root, 'src'))
  82. # Download glcorearb.h
  83. if not os.path.exists(os.path.join(args.root, 'include/GL/glcorearb.h')):
  84. print('Downloading glcorearb.h to {0}...'.format(os.path.join(args.root, 'include/GL/glcorearb.h')))
  85. web = urllib2.urlopen('http://www.opengl.org/registry/api/GL/glcorearb.h')
  86. with open(os.path.join(args.root, 'include/GL/glcorearb.h'), 'wb') as f:
  87. f.writelines(web.readlines())
  88. else:
  89. print('Reusing glcorearb.h from {0}...'.format(os.path.join(args.root, 'include/GL')))
  90. # Parse function names from glcorearb.h
  91. print('Parsing glcorearb.h header...')
  92. procs = []
  93. p = re.compile(r'GLAPI.*APIENTRY\s+(\w+)')
  94. with open(os.path.join(args.root, 'include/GL/glcorearb.h'), 'r') as f:
  95. for line in f:
  96. m = p.match(line)
  97. if not m:
  98. continue
  99. proc = m.group(1)
  100. if args.ext or not is_ext(proc):
  101. procs.append(proc)
  102. procs.sort()
  103. # Generate gl3w.h
  104. print('Generating gl3w.h in {0}...'.format(os.path.join(args.root, 'include/GL')))
  105. with open(os.path.join(args.root, 'include/GL/gl3w.h'), 'wb') as f:
  106. write(f, UNLICENSE)
  107. write(f, r'''#ifndef __gl3w_h_
  108. #define __gl3w_h_
  109. #include <GL/glcorearb.h>
  110. #ifndef __gl_h_
  111. #define __gl_h_
  112. #endif
  113. #ifdef __cplusplus
  114. extern "C" {
  115. #endif
  116. #define GL3W_OK 0
  117. #define GL3W_ERROR_INIT -1
  118. #define GL3W_ERROR_LIBRARY_OPEN -2
  119. #define GL3W_ERROR_OPENGL_VERSION -3
  120. typedef void (*GL3WglProc)(void);
  121. typedef GL3WglProc (*GL3WGetProcAddressProc)(const char *proc);
  122. /* gl3w api */
  123. int gl3wInit(void);
  124. int gl3wInit2(GL3WGetProcAddressProc proc);
  125. int gl3wIsSupported(int major, int minor);
  126. GL3WglProc gl3wGetProcAddress(const char *proc);
  127. /* gl3w internal state */
  128. ''')
  129. write(f, 'union GL3WProcs {\n')
  130. write(f, '\tGL3WglProc ptr[{0}];\n'.format(len(procs)))
  131. write(f, '\tstruct {\n')
  132. for proc in procs:
  133. write(f, '\t\t{0[p_t]: <55} {0[p_s]};\n'.format(proc_t(proc)))
  134. write(f, r''' } gl;
  135. };
  136. extern union GL3WProcs gl3wProcs;
  137. /* OpenGL functions */
  138. ''')
  139. for proc in procs:
  140. write(f, '#define {0[p]: <48} gl3wProcs.gl.{0[p_s]}\n'.format(proc_t(proc)))
  141. write(f, r'''
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif
  146. ''')
  147. # Generate gl3w.c
  148. print('Generating gl3w.c in {0}...'.format(os.path.join(args.root, 'src')))
  149. with open(os.path.join(args.root, 'src/gl3w.c'), 'wb') as f:
  150. write(f, UNLICENSE)
  151. write(f, r'''#include <GL/gl3w.h>
  152. #include <stdlib.h>
  153. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  154. #if defined(_WIN32)
  155. #define WIN32_LEAN_AND_MEAN 1
  156. #include <windows.h>
  157. static HMODULE libgl;
  158. static int open_libgl(void)
  159. {
  160. libgl = LoadLibraryA("opengl32.dll");
  161. if (!libgl)
  162. return GL3W_ERROR_LIBRARY_OPEN;
  163. return GL3W_OK;
  164. }
  165. static void close_libgl(void)
  166. {
  167. FreeLibrary(libgl);
  168. }
  169. static GL3WglProc get_proc(const char *proc)
  170. {
  171. GL3WglProc res;
  172. res = (GL3WglProc)wglGetProcAddress(proc);
  173. if (!res)
  174. res = (GL3WglProc)GetProcAddress(libgl, proc);
  175. return res;
  176. }
  177. #elif defined(__APPLE__)
  178. #include <dlfcn.h>
  179. static void *libgl;
  180. static int open_libgl(void)
  181. {
  182. libgl = dlopen("/System/Library/Frameworks/OpenGL.framework/OpenGL", RTLD_LAZY | RTLD_GLOBAL);
  183. if (!libgl)
  184. return GL3W_ERROR_LIBRARY_OPEN;
  185. return GL3W_OK;
  186. }
  187. static void close_libgl(void)
  188. {
  189. dlclose(libgl);
  190. }
  191. static GL3WglProc get_proc(const char *proc)
  192. {
  193. GL3WglProc res;
  194. *(void **)(&res) = dlsym(libgl, proc);
  195. return res;
  196. }
  197. #else
  198. #include <dlfcn.h>
  199. #include <GL/glx.h>
  200. static void *libgl;
  201. static PFNGLXGETPROCADDRESSPROC glx_get_proc_address;
  202. static int open_libgl(void)
  203. {
  204. libgl = dlopen("libGL.so", RTLD_LAZY | RTLD_GLOBAL);
  205. if (!libgl)
  206. return GL3W_ERROR_LIBRARY_OPEN;
  207. *(void **)(&glx_get_proc_address) = dlsym(libgl, "glXGetProcAddressARB");
  208. return GL3W_OK;
  209. }
  210. static void close_libgl(void)
  211. {
  212. dlclose(libgl);
  213. }
  214. static GL3WglProc get_proc(const char *proc)
  215. {
  216. GL3WglProc res;
  217. res = glx_get_proc_address((const GLubyte *)proc);
  218. if (!res)
  219. *(void **)(&res) = dlsym(libgl, proc);
  220. return res;
  221. }
  222. #endif
  223. static struct {
  224. int major, minor;
  225. } version;
  226. static int parse_version(void)
  227. {
  228. if (!glGetIntegerv)
  229. return GL3W_ERROR_INIT;
  230. glGetIntegerv(GL_MAJOR_VERSION, &version.major);
  231. glGetIntegerv(GL_MINOR_VERSION, &version.minor);
  232. if (version.major < 3)
  233. return GL3W_ERROR_OPENGL_VERSION;
  234. return GL3W_OK;
  235. }
  236. static void load_procs(GL3WGetProcAddressProc proc);
  237. int gl3wInit(void)
  238. {
  239. return gl3wInit2(get_proc);
  240. }
  241. int gl3wInit2(GL3WGetProcAddressProc proc)
  242. {
  243. int res = open_libgl();
  244. if (res)
  245. return res;
  246. atexit(close_libgl);
  247. load_procs(proc);
  248. return parse_version();
  249. }
  250. int gl3wIsSupported(int major, int minor)
  251. {
  252. if (major < 3)
  253. return 0;
  254. if (version.major == major)
  255. return version.minor >= minor;
  256. return version.major >= major;
  257. }
  258. GL3WglProc gl3wGetProcAddress(const char *proc)
  259. {
  260. return get_proc(proc);
  261. }
  262. static const char *proc_names[] = {
  263. ''')
  264. for proc in procs:
  265. write(f, '\t"{0}",\n'.format(proc))
  266. write(f, r'''};
  267. union GL3WProcs gl3wProcs;
  268. static void load_procs(GL3WGetProcAddressProc proc)
  269. {
  270. size_t i;
  271. for (i = 0; i < ARRAY_SIZE(proc_names); i++)
  272. gl3wProcs.ptr[i] = proc(proc_names[i]);
  273. }
  274. ''')